0

我正在尝试使用 Lucene 进行查询,我想选择标题以“@”字符开头的文档。我查看了文档,但结果是零元素。这是代码和结果。谢谢你的帮助。

这是代码:

IndexWriter w = new IndexWriter(index, config);
addDoc(w, "@aa Lucene in Action", "193398817");
addDoc(w, "@ba Lucene for Dummies", "55320055Z");
addDoc(w, "prova Managing Gigabytes", "55063554A");
addDoc(w, "The Art of Computer Science", "9900333X");
w.close();
String querystring = "@";

Query q;
q = new QueryParser(LUCENE_41, "title", new StandardAnalyzer(LUCENE_41)).parse(querystring);
IndexReader reader = DirectoryReader.open(index);
IndexSearcher searcher = new IndexSearcher(reader);
TopDocs docs = searcher.search(q, 1000000);

ScoreDoc[] hits = docs.scoreDocs;

System.out.println("Found " + hits.length + " hits.");
for (int i = 0; i < hits.length; ++i) {
    int docId = hits[i].doc;
    Document d = searcher.doc(docId);
    System.out.println((i + 1) + ". " + d.get("isbn") + "\t" + d.get("title"));
}

reader.close();

这是输出

Building provaLucerne 1.0-SNAPSHOT
------------------------------------------------------------------------

--- exec-maven-plugin:1.2.1:exec (default-cli) @ provaLucerne ---
Found 0 hits.
------------------------------------------------------------------------
BUILD SUCCESS
------------------------------------------------------------------------
Total time: 1.505s
Finished at: Wed Nov 02 19:49:39 CET 2016
Final Memory: 5M/155M
4

1 回答 1

0

您正在使用使用 StandardTokenizer 的 StandardAnalyzer。在标准 Toeknizer 中,“@”字符是标记拆分标点符号集之一。

所以字符串 "@aa Lucene in Action" 被标记为 "aa","Lucene","in","Action" 标记。

您可以使用 KeywordAnalyzer 或 WhitespaceAnalyzer 看看是否能解决您的问题。

于 2016-11-02T20:19:41.377 回答