在创建索引的时候分为了许多Field列,在查询的时候可以同时查询这些列,然后使用布尔逻辑查询将这些查询返回的结果合并。
在没有使用布尔逻辑查询之前的代码:
public void seacher(String queryString) throws IOException, ParseException {
String field[] = { "title", "content", "author" };
for (int i = 0; i < field.length; i++) {
String defualtField = field[i];
TopDocs topDocs = null;
IndexReader reader = DirectoryReader.open(
FSDirectory.open(Paths.get(indexPath)));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = getAnalyzer();
//QueryParser是用来解析查询串
QueryParser queryParser
= new QueryParser(defualtField, analyzer);
Query query = queryParser.parse(queryString);
//返回最相关的10个文档
topDocs = searcher.search(query, 10);
ScoreDoc[] hits = topDocs.scoreDocs;
for (ScoreDoc hit : hits) {
Document hitDoc = searcher.doc(hit.doc);
System.out.println(hitDoc.get("title")
+ " " + hitDoc.get("url"));
}
}
}
得到的结果为:
改为布尔逻辑查询:
public void seacher(String queryString) throws IOException, ParseException {
TopDocs topDocs = null;
IndexReader reader = DirectoryReader.open(
FSDirectory.open(Paths.get(indexPath)));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = getAnalyzer();
// QueryParser是用来解析查询串
QueryParser queryParser = new QueryParser("title", analyzer);
Query titleQuery = queryParser.parse(queryString);// 查询标题列
queryParser = new QueryParser("content", analyzer);// 查询内容列
Query contentQuery = queryParser.parse(queryString);
queryParser = new QueryParser("autor", analyzer);// 查询作者列
Query authorQuery = queryParser.parse(queryString);
// 使用布尔逻辑查询
BooleanQuery.Builder queryBuild=new BooleanQuery.Builder();
queryBuild.add(titleQuery,BooleanClause.Occur.SHOULD);
queryBuild.add(contentQuery,BooleanClause.Occur.SHOULD);
queryBuild.add(authorQuery,BooleanClause.Occur.SHOULD);
BooleanQuery query=queryBuild.build();
// 返回最相关的10个文档
topDocs = searcher.search(query, 10);
ScoreDoc[] hits = topDocs.scoreDocs;
for (ScoreDoc hit : hits) {
Document hitDoc = searcher.doc(hit.doc);
System.out.println(hitDoc.get("title")
+ " " + hitDoc.get("url"));
}
}
在Lucene 6.1.0使用布尔逻辑查询可以先创建一个BooleanQuery.Builder对象,通过add方法将之前的创建的查询加入到该对象中,然后再调用build()方法转换为BooleanQuery 对象。
一共有4种Occur方式:
/** Use this operator for clauses that <i>must</i> appear in the matching documents. */
MUST { @Override public String toString() { return "+"; } },
/** Like {@link #MUST} except that these clauses do not participate in scoring. */
FILTER { @Override public String toString() { return "#"; } },
/** Use this operator for clauses that <i>should</i> appear in the
* matching documents. For a BooleanQuery with no <code>MUST</code>
* clauses one or more <code>SHOULD</code> clauses must match a document
* for the BooleanQuery to match.
* @see BooleanQuery.Builder#setMinimumNumberShouldMatch
*/
SHOULD { @Override public String toString() { return ""; } },
/** Use this operator for clauses that <i>must not</i> appear in the matching documents.
* Note that it is not possible to search for queries that only consist
* of a <code>MUST_NOT</code> clause. These clauses do not contribute to the
* score of documents. */
MUST_NOT { @Override public String toString() { return "-"; } };
SHOULD是析取查询,选择出包含任何一个查询词的文档;
MUST_NOT是否定查询,即不包括符合这个条件的文档。
BooleanQuery对象还可以设置最小匹配数量,只要设置minimumNumberShouldMatch属性即可(在构造函数中设置)。