测试Query类型
class="java">import java.io.File; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.PhraseQuery; import org.apache.lucene.search.PrefixQuery; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TermRangeQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.WildcardQuery; import org.apache.lucene.store.FSDirectory; import org.junit.Test; import com.test.utils.File2DocumentUtil; public class QueryTest { String filePath = "F:\\eclipse\\LuceneTest\\luceneDatasource\\IndexWriter addDocument's a javadoc .txt"; String indexPath = "F:\\eclipse\\LuceneTest\\luceneIndex"; /** * 按词条搜索—TermQuery * * @throws Exception */ @Test public void testTermQuery() throws Exception { Term term = new Term("content", "document"); Query query = new TermQuery(term); queryAndPrintResult(query); } /** * 范围检索 看不懂 * * @throws Exception */ @Test public void testRangeQuery() throws Exception { Term lowerTerm = new Term("size", "0"); Term upperTerm = new Term("size", "4000"); // Query query = new TermRangeQuery("size", lowerTerm.bytes(), // upperTerm.bytes(), true, false); Query query = TermRangeQuery.newStringRange("size", "000", "500", true, true); queryAndPrintResult(query); } // 使用前缀搜索—PrefixQuery @Test public void testPrefixQuery() throws Exception { Term term = new Term("content", "房"); PrefixQuery query = new PrefixQuery(term); queryAndPrintResult(query); } /* * 通配符查询 * '?'代表一个字符。'*'代表0个或多个字符 */ @Test public void testWildCardQuery() throws Exception { Term term = new Term("content", "*间"); WildcardQuery query = new WildcardQuery(term); queryAndPrintResult(query); } // 短语搜索—PhraseQuery @Test public void testPhraseQuery() throws Exception { PhraseQuery query = new PhraseQuery(); //设定position位置 // query.add(new Term("content", "旅游"), 0); // query.add(new Term("content", "的"), 2); query.add(new Term("content", "旅游")); query.add(new Term("content", "的")); //指定多个未知短语 query.setSlop(1); queryAndPrintResult(query); } /** * 按“与或”搜索—BooleanQuery 1.和: MUST与MUST_NOT * * 2.或: SHOULD与SHOULD * * 3.A与B的并集-B MUST与MUST_NOT * * @throws Exception */ @Test public void testBooleanQuery() throws Exception { PhraseQuery query = new PhraseQuery(); query.add(new Term("content", "adds")); query.add(new Term("content", "room")); query.setSlop(2); Term term = new Term("content", "add1"); PrefixQuery query2 = new PrefixQuery(term); BooleanQuery booleanQuery = new BooleanQuery(); booleanQuery.add(query, Occur.SHOULD); booleanQuery.add(query2, Occur.SHOULD); queryAndPrintResult(booleanQuery); } public void queryAndPrintResult(Query query) throws Exception { System.out.println("对应的查询字符串:" + query); IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(indexPath))); IndexSearcher indexSearcher = new IndexSearcher(reader); TopDocs topDocs = indexSearcher.search(query, 10000); System.out.println("总共有【" + topDocs.totalHits + "】条匹配结果"); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { int docSn = scoreDoc.doc; Document doc = indexSearcher.doc(docSn); File2DocumentUtil.printDocumnetInfo(doc); } } }
?