我想进行一个查询,它会在日期范围之间给出数据,并在lucene 3.0.1中再提供一个AND条件。这是两个日期之间的查询代码:
IndexSearcher searcher = new IndexSearcher(directory);
String lowerDate = "2013-06-27";
String upperDate = "2013-06-29";
boolean includeLower = true;
boolean includeUpper = true;
TermRangeQuery query = new TermRangeQuery("created_at",lowerDate, upperDate, includeLower, includeUpper);
// display search results
TopDocs topDocs = searcher.search(query, 10);
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
Document doc = searcher.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
}
我还有一个索引列文本,如何在此查询中包含一个AND条件,我试图在日期范围内获得结果,该结果还包含测试列中的一些关键字。
答案 0 :(得分:2)
您需要使用BooleanQuery,例如:
TermRangeQuery dateQuery = new TermRangeQuery("created_at",lowerDate, upperDate, includeLower, includeUpper);
TermQuery keywordQuery = new TermQuery(new Term("keyword", "term"));
BooleanQuery bq = new BooleanQuery();
bq.add(new BooleanClause(dateQuery, BooleanClause.Occur.MUST))
bq.add(new BooleanClause(keywordQuery, BooleanClause.Occur.MUST))
// display search results
TopDocs topDocs = searcher.search(bq, 10);
将两个子句合并为BooleanClause.Occur.MUST
,相当于“AND”(请查看“MUST
”,“SHOULD
”和“{ {1}}“在BooleanClause.Occur documentation中使用Lucene的”布尔“逻辑更好地理解你的选择。