public class BooleanQueryTest extends TestCase {
public void testAnd() throws Exception {
TermQuery searchingBooks =
new TermQuery(new Term("subject","search")); //#1
Query books2010 = //#2
NumericRangeQuery.newIntRange("pubmonth", 201001, //#2
201012, //#2
true, true); //#2
BooleanQuery searchingBooks2010 = new BooleanQuery(); //#3
searchingBooks2010.add(searchingBooks, BooleanClause.Occur.MUST); //#3
searchingBooks2010.add(books2010, BooleanClause.Occur.MUST); //#3
Directory dir = TestUtil.getBookIndexDirectory();
IndexSearcher searcher = new IndexSearcher(dir);
TopDocs matches = searcher.search(searchingBooks2010, 10);
assertTrue(TestUtil.hitsIncludeTitle(searcher, matches,
"Lucene in Action, Second Edition"));
searcher.close();
dir.close();
}
/*
#1 Match books with subject 鈥渟earch鈥�#2 Match books in 2004
#3 Combines two queries
*/
public void testOr() throws Exception {
TermQuery methodologyBooks = new TermQuery( // #1
new Term("category", // #1
"/technology/computers/programming/methodology")); // #1
TermQuery easternPhilosophyBooks = new TermQuery( // #2
new Term("category", // #2
"/philosophy/eastern")); // #2
BooleanQuery enlightenmentBooks = new BooleanQuery(); // #3
enlightenmentBooks.add(methodologyBooks, // #3
BooleanClause.Occur.SHOULD); // #3
enlightenmentBooks.add(easternPhilosophyBooks, // #3
BooleanClause.Occur.SHOULD); // #3
Directory dir = TestUtil.getBookIndexDirectory();
IndexSearcher searcher = new IndexSearcher(dir);
TopDocs matches = searcher.search(enlightenmentBooks, 10);
System.out.println("or = " + enlightenmentBooks);
assertTrue(TestUtil.hitsIncludeTitle(searcher, matches,
"Extreme Programming Explained"));
assertTrue(TestUtil.hitsIncludeTitle(searcher, matches,
"Tao Te Ching \u9053\u5FB7\u7D93"));
searcher.close();
dir.close();
}
/*
#1 Match 1st category
#2 Match 2nd category
#3 Combine
*/
}
lucene学习之二boolean搜索(相当于SQL中的or、and等)
最新推荐文章于 2023-01-30 10:22:16 发布