【发布时间】:2009-03-30 23:11:04
【问题描述】:
我是 Lucene 的新手。
有什么方法可以让 Lucene 分析器不忽略字符串中的点? 例如,如果我的搜索条件是:“A.B.C.D”,Lucene 应该只给我搜索结果中包含“A.B.C.D”而不是“ABCD”的文档......
【问题讨论】:
标签: lucene lucene.net
我是 Lucene 的新手。
有什么方法可以让 Lucene 分析器不忽略字符串中的点? 例如,如果我的搜索条件是:“A.B.C.D”,Lucene 应该只给我搜索结果中包含“A.B.C.D”而不是“ABCD”的文档......
【问题讨论】:
标签: lucene lucene.net
这完全取决于您使用的分析仪。 StandardAnalyzer 用虚线命名some complicated things,试图“做你的意思”。也许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);
}
【讨论】: