lucene7.5 lucene查询之多条件查询BooleanQuery类使用

 Analyzer analyzer = new MMSegAnalyzer();
        BooleanQuery.Builder builderAll = new BooleanQuery.Builder();
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        Query company_name = new QueryParser("company_name", analyzer).parse(searchText);
        //  设置查询评分
        BoostQuery company_name_boost = new BoostQuery(company_name, 10l);
        Query credit_cd = new QueryParser("credit_cd", analyzer).parse(searchText);
        Query pollution_cd = new QueryParser("pollution_cd", analyzer).parse(searchText);
        Query entername = new QueryParser("entername", analyzer).parse(searchText);
        Query leagal_person = new QueryParser("leagal_person", analyzer).parse(searchText);
        Query address = new QueryParser("address", analyzer).parse(searchText);
        Query phone = new QueryParser("phone", analyzer).parse(searchText);
        Query pwxkzbm = new QueryParser("pwxkzbm", analyzer).parse(searchText);
        Query hyname = new QueryParser("hyname", analyzer).parse(searchText);
        BoostQuery hyname_boost = new BoostQuery(hyname, 100l);
        Query lx_person = new QueryParser("lx_person", analyzer).parse(searchText);
        Query filename = new QueryParser("filename", analyzer).parse(searchText);
        Query content = new QueryParser("content", analyzer).parse(searchText);
        Query subName = new QueryParser("subName", analyzer).parse(searchText);
        builder.add(company_name_boost, BooleanClause.Occur.SHOULD);
        //    builder.add(organ_cd, BooleanClause.Occur.SHOULD);
        builder.add(credit_cd, BooleanClause.Occur.SHOULD);
        builder.add(pollution_cd, BooleanClause.Occur.SHOULD);
        builder.add(entername, BooleanClause.Occur.SHOULD);
        builder.add(leagal_person, BooleanClause.Occur.SHOULD);
        builder.add(address, BooleanClause.Occur.SHOULD);
        builder.add(phone, BooleanClause.Occur.SHOULD);
        builder.add(pwxkzbm, BooleanClause.Occur.SHOULD);
        builder.add(hyname_boost, BooleanClause.Occur.SHOULD);
        builder.add(lx_person, BooleanClause.Occur.SHOULD);
        builder.add(filename, BooleanClause.Occur.SHOULD);
        builder.add(subName, BooleanClause.Occur.SHOULD);
        builder.add(content, BooleanClause.Occur.SHOULD);

        BooleanQuery booleanQuery = builder.build();
        builderAll.add(booleanQuery, BooleanClause.Occur.MUST);
		if (!StringUtils.isEmpty(luceneType)) {
		                Query type = new TermQuery(new Term("luceneType", luceneType));
		                builderAll.add(type, BooleanClause.Occur.MUST);
		            }
		            if (!StringUtils.isEmpty(timeLimit)) {
		                Query type = null;
		                LocalDate localDate = LocalDate.now();
		                DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
		                if ("1".equals(timeLimit)) {
		                    LocalDate oneMonthLocalDate = localDate.minus(3, ChronoUnit.MONTHS);
		                    type = LongPoint.newRangeQuery("date", Timestamp.valueOf(oneMonthLocalDate.atStartOfDay()).getTime(), Timestamp.valueOf(localDate.atStartOfDay()).getTime());
		
		                } else if ("2".equals(timeLimit)) {
		                    LocalDate sixMonthLocalDate = localDate.minus(6, ChronoUnit.MONTHS);
		
		                    type = LongPoint.newRangeQuery("date", Timestamp.valueOf(sixMonthLocalDate.atStartOfDay()).getTime(), Timestamp.valueOf(localDate.atStartOfDay()).getTime());
	
	                } else if ("3".equals(timeLimit)) {
	                    LocalDate oneYearsLocalDate = localDate.minus(1, ChronoUnit.YEARS);
	                    type = LongPoint.newRangeQuery("date", Timestamp.valueOf(oneYearsLocalDate.atStartOfDay()).getTime(), Timestamp.valueOf(localDate.atStartOfDay()).getTime());
	                }
	                builderAll.add(type, BooleanClause.Occur.MUST);
	            }
	       BooleanQuery booleanQueryAll = builderAll.build();
	      // 获取查询结果  
	       list = LuceneSearch.get(pageable, booleanQueryAll, new MapLuceneConvertor());

创建出查询对象booleanQueryAll

Occur.MUST:必须满足此条件,相当于and
Occur.SHOULD:应该满足,但是不满足也可以,相当于or
Occur.MUST_NOT:必须不满足。相当于not

你可能感兴趣的:(Lucene)