0

我是 Lucene 的新手。

有什么办法可以让 Lucene 分析器不忽略字符串中的点?例如,如果我的搜索条件是:“ABCD”,Lucene 应该只给我搜索结果中包含“ABCD”而不是“ABCD”的那些文档......

4

1 回答 1

5

这完全取决于您使用的分析仪。他们用点名StandardAnalyzer了一些复杂的事情,试图“做你的意思”。也许这WhitespaceAnalyzer将更符合您的需求。

public static void main(String[] args) throws Exception {
    RAMDirectory dir = new RAMDirectory();
    IndexWriter iw = new IndexWriter(dir, new WhitespaceAnalyzer(), IndexWriter.MaxFieldLength.LIMITED);
    Document doc = new Document();
    doc.add(new Field("text", "A.B.C.D DEF", Field.Store.YES, Field.Index.ANALYZED));
    iw.addDocument(doc);
    iw.close();

    IndexSearcher searcher = new IndexSearcher(dir);
    QueryParser queryParser = new QueryParser("text", new WhitespaceAnalyzer());

    // prints 0 
    System.out.println(searcher.search(queryParser.parse("ABCD"), 1).totalHits);

    // prints 1
    System.out.println(searcher.search(queryParser.parse("A.B.C.D"), 1).totalHits);
}
于 2009-03-31T06:38:37.760 回答