Lucene 6.0中BooleanQuery创建和之前的不太一样,4.3版本的创建BooleanQuery:
BooleanQuery bQuery=new BooleanQuery();
bQuery.add(new TermQuery(new Term("title", "lucene")), Occur.MUST);
bQuery.add(new TermQuery(new Term("content", "基于")), Occur.SHOULD);
bQuery.add(new TermQuery(new Term("name", "java")), Occur.MUST_NOT);
6.0版本的创建BooleanQuery,搜索title中包含”美国”,”content”中不包含”日本”的文档:
Query query1 = new TermQuery(new Term("title", "美国"));
Query query2 = new TermQuery(new Term("content", "日本"));
BooleanClause bc1 = new BooleanClause(query1, Occur.MUST);
BooleanClause bc2 = new BooleanClause(query2, Occur.MUST_NOT);
BooleanQuery boolQuery = new BooleanQuery.Builder().add(bc1).add(bc2).build();
System.out.println(boolQuery.toString());
// 返回前10条
TopDocs topDocs = searcher.search(boolQuery, 10);
打印结果:
+title:美国 -content:日本