查询选择所有lucene java

时间:2016-11-02 18:59:29

标签: java lucene full-text-search document query-parser

我试图用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

1 个答案:

答案 0 :(得分:0)

您使用的是使用StandardTokenizer的StandardAnalyzer。在标准的Toeknizer中,“@”字符是令牌分裂标点符号的集合。

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

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