當我用短語搜索「PH1 PH2」找到包含「PH1」或「PH2」文本。Lucene的示例查詢
String line = "ph1 ph2";
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, field, analyzer);
Query query = parser.parse(line);
任何人都知道如何通過1)短語(「ph1 ph2」)進行搜索。示例:這是一句PH1 PH2。 2)用短語最大距離( 「PH1 PH2〜3」)。示例這個ph1是句子ph2。
P.S我用標準的Lucene索引索引我的文件。如果這個例子是沒有明確的說法http://www.lucenetutorial.com/lucene-query-syntax.html
這裏是全碼:
String index = "C:/programs/lucenedemo/index";
String field = "contents";
IndexReader reader = DirectoryReader.open(FSDirectory.open(new File(index)));
IndexSearcher searcher = new IndexSearcher(reader);
Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_40);
//QueryParser parser = new QueryParser(Version.LUCENE_40, field, analyzer);
String line = "ph1 ph2";
QueryParser parser = new QueryParser(Version.LUCENE_CURRENT, field, analyzer);
Query query = parser.parse(line);
//doPagingSearch(searcher, query, hitsPerPage, raw, queries == null && queryString == null);
//doPagingSearch
TopDocs results = searcher.search(query, 300000);
ScoreDoc[] hits = results.scoreDocs;
System.out.println(results.totalHits);
for (int i=0;i<10;i++) {
Document doc = searcher.doc(hits[i].doc);
String path = doc.get("path");
if (path != null) System.out.println((i+1) + ". " + path);
}
//end of doPagingSearch
reader.close();
感謝上查詢語法分析進一步的選擇中, 「PH1 PH2」 istead我不得不寫 「\」 PH1 PH2 \ 「〜3」 –