当我按短语“ph1 ph2”搜索时,它会找到包含“ph1”或“ph2”的文本。
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。
PS 我使用标准的 Lucene Indexer 来索引我的文件。如果这个例子不清楚查看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();