【问题标题】:Lucene analyzer and dotsLucene 分析器和点
【发布时间】:2009-03-30 23:11:04
【问题描述】:

我是 Lucene 的新手。

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

【问题讨论】:

    标签: lucene lucene.net


    【解决方案1】:

    这完全取决于您使用的分析仪。 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);
    }
    

    【讨论】:

    • 嗨,谢谢...使用 whitespaceanalyzer,我如何将停用词输入到 lucene?
    • 基本上,您必须编写一个新的分析器,这并不难。如果您想了解更多详细信息,建议您打开一个新问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-09-08
    • 1970-01-01
    • 2011-07-25
    • 2013-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-11-24
    相关资源
    最近更新 更多