public long getSearchCount(String keyword, String webFlag) {
TopScoreDocCollector f = TopScoreDocCollector.create(5000, false);
IndexSearcher isearcher = getSearcher();
Analyzer analyzer = null;
QueryParser queryParser = null;
Query query;
try {
if (!"website".equals(webFlag)) {
// 在索引目录中查询的字段
String[] fields = { "BOOK_NAME", "BOOK_DES", "BOOK_PEN_NAME", "BOOK_STATUS" };
List list = new ArrayList();
analyzer = getAnalyzer();
queryParser = new MultiFieldQueryParser(version, fields, analyzer);
query = queryParser.parse(keyword);
} else {
query = new BooleanQuery();
BooleanQuery bquery1 = new BooleanQuery();
TermQuery tQuery1 = new TermQuery(new Term("BOOK_NAME", keyword));
TermQuery tQuery2 = new TermQuery(new Term("BOOK_DES", keyword));
TermQuery tQuery3 = new TermQuery(new Term("BOOK_PEN_NAME", keyword));
TermQuery tQuery4 = new TermQuery(new Term("BOOK_STATUS", "12"));
TermQuery tQuery5 = new TermQuery(new Term("BOOK_ONSALE_IDE", "true"));
bquery1.add(tQuery1, BooleanClause.Occur.SHOULD);
bquery1.add(tQuery2, BooleanClause.Occur.SHOULD);
bquery1.add(tQuery3, BooleanClause.Occur.SHOULD);
//查询条件通过BooleanQuery 嵌套使用。。
((BooleanQuery) query).add(bquery1, BooleanClause.Occur.MUST);**重点内容**
((BooleanQuery) query).add(tQuery4, BooleanClause.Occur.MUST);
((BooleanQuery) query).add(tQuery5, BooleanClause.Occur.MUST);
}
isearcher.search(query, f);
} catch (Exception e) {
e.printStackTrace();
}
TopDocs topDocs = f.topDocs(0, 5000);
ScoreDoc[] hits = topDocs.scoreDocs;
return hits.length;
}**重点内容**
Lucene BooleanQuery 嵌套使用
最新推荐文章于 2024-06-16 00:23:27 发布