QueryParser 類的使用java
IndexSearcher數據庫
實現搜索功能數組
使用TopDocs 類緩存
搜索結果分頁安全
近實時搜索性能
public class NearRealTimeTest extends TestCase { public void testNearRealTime() throws Exception { Directory dir = new RAMDirectory(); IndexWriter writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30), IndexWriter.MaxFieldLength.UNLIMITED); for(int i=0;i<10;i++) { Document doc = new Document(); doc.add(new Field("id", ""+i, Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); doc.add(new Field("text", "aaa", Field.Store.NO, Field.Index.ANALYZED)); writer.addDocument(doc); } // 會將緩存中全部變動,刷新到索引目錄,返回一個包含這些變動的新的reader IndexReader reader = writer.getReader(); // #1 建立近實時 reader IndexSearcher searcher = new IndexSearcher(reader); // #A Query query = new TermQuery(new Term("text", "aaa")); TopDocs docs = searcher.search(query, 1); assertEquals(10, docs.totalHits); // #B writer.deleteDocuments(new Term("id", "7")); // #2 Document doc = new Document(); // #3 doc.add(new Field("id", // #3 "11", // #3 Field.Store.NO, // #3 Field.Index.NOT_ANALYZED_NO_NORMS)); // #3 doc.add(new Field("text", // #3 "bbb", // #3 Field.Store.NO, // #3 Field.Index.ANALYZED)); // #3 writer.addDocument(doc); // #3 // reopen 完成更多變動,效率很高: // 只會打開上次open、reopen的文件 // 未發生任何改變的被共享(共享先前的reader) IndexReader newReader = reader.reopen(); // #4 assertFalse(reader == newReader); // #5 reader.close(); // #6 searcher = new IndexSearcher(newReader); TopDocs hits = searcher.search(query, 10); // #7 assertEquals(9, hits.totalHits); // #7 query = new TermQuery(new Term("text", "bbb")); // #8 hits = searcher.search(query, 1); // #8 assertEquals(1, hits.totalHits); // #8 newReader.close(); writer.close(); } } /* #1 Create near-real-time reader #A Wrap reader in IndexSearcher #B Search returns 10 hits #2 Delete 1 document #3 Add 1 document #4 Reopen reader #5 Confirm reader is new #6 Close old reader #7 Verify 9 hits now #8 Confirm new document matched */
理解Lucene的 評分機制優化
explain() 方法理解搜索結果評分:this
public class Explainer { public static void main(String[] args) throws Exception { if (args.length != 2) { System.err.println("Usage: Explainer <index dir> <query>"); System.exit(1); } String indexDir = args[0]; String queryExpression = args[1]; Directory directory = FSDirectory.open(new File(indexDir)); QueryParser parser = new QueryParser(Version.LUCENE_30, "contents", new SimpleAnalyzer()); Query query = parser.parse(queryExpression); System.out.println("Query: " + queryExpression); IndexSearcher searcher = new IndexSearcher(directory); TopDocs topDocs = searcher.search(query, 10); for (ScoreDoc match : topDocs.scoreDocs) { Explanation explanation = searcher.explain(query, match.doc); //#A System.out.println("----------"); Document doc = searcher.doc(match.doc); System.out.println(doc.get("title")); System.out.println(explanation.toString()); //#B } searcher.close(); directory.close(); } }
Lucene 多樣化查詢:操作系統
解析查詢表達式:QueryParser線程
TermQuery
項範圍查詢:
通配符查詢僅僅在末尾有一個*
布爾查詢::
短語查詢:
模糊查詢:
MatchAllDocsQuery
分組查詢:
域選擇:
爲子查詢加權
是否必定使用QueryParser