在Lucene中结合分析仪的最佳实践是什么?

时间:2011-03-05 18:46:05

标签: lucene analyzer

我有一种情况,我在Lucene中使用StandardAnalyzer来索引文本字符串,如下所示:

public void indexText(String suffix, boolean includeStopWords)  {        
    StandardAnalyzer analyzer = null;


    if (includeStopWords) {
        analyzer = new StandardAnalyzer(Version.LUCENE_30);
    }
    else {

        // Get Stop_Words to exclude them.
        Set<String> stopWords = (Set<String>) Stop_Word_Listener.getStopWords();      
        analyzer = new StandardAnalyzer(Version.LUCENE_30, stopWords);
    }

    try {

        // Index text.
        Directory index = new RAMDirectory();
        IndexWriter w = new IndexWriter(index, analyzer, true, IndexWriter.MaxFieldLength.UNLIMITED);            
        this.addTextToIndex(w, this.getTextToIndex());
        w.close();

        // Read index.
        IndexReader ir = IndexReader.open(index);
        Text_TermVectorMapper ttvm = new Text_TermVectorMapper();

        int docId = 0;

        ir.getTermFreqVector(docId, PropertiesFile.getProperty(text), ttvm);

        // Set output.
        this.setWordFrequencies(ttvm.getWordFrequencies());
        w.close();
    }
    catch(Exception ex) {
        logger.error("Error message\n", ex);
    }
}

private void addTextToIndex(IndexWriter w, String value) throws IOException {
    Document doc = new Document();
    doc.add(new Field(text), value, Field.Store.YES, Field.Index.ANALYZED, Field.TermVector.YES));
    w.addDocument(doc);
}

哪种方法效果很好,但我想将其与使用SnowballAnalyzer的词干结合起来。

此类还有两个实例变量,显示在下面的构造函数中:

public Text_Indexer(String textToIndex) {
    this.textToIndex = textToIndex;
    this.wordFrequencies = new HashMap<String, Integer>();
}

有人能告诉我如何使用上面的代码实现这一目标吗?

由于

摩根先生。

3 个答案:

答案 0 :(得分:2)

Lucene提供org.apache.lucene.analysis.Analyzer基类,如果您想编写自己的分析器,可以使用它 您可以查看扩展Analyzer的org.apache.lucene.analysis.standard.StandardAnalyzer类。

然后,在YourAnalyzer中,您将使用分析器使用的过滤器链接StandardAnalyzer和SnowballAnalyzer,如下所示:

TokenStream result = new StandardFilter(tokenStream);
result = new SnowballFilter(result, stopSet);

然后,在现有代码中,您将能够使用自己的Analyzer实现构建IndexWriter,该实现将Standard和Snowball过滤器链接起来。

完全偏离主题:
我想你最终需要设置处理请求的自定义方式。这已经在Solr中实现了。

首先通过扩展SearchComponent并在SolrConfig.xml中定义它来编写自己的搜索组件,如下所示:

<searchComponent name="yourQueryComponent" class="org.apache.solr.handler.component.YourQueryComponent"/>

然后通过扩展SearchHandler编写搜索处理程序(请求处理程序),并在SolrConfig.xml中定义它:

  <requestHandler name="YourRequestHandlerName" class="org.apache.solr.handler.component.YourRequestHandler" default="true">
    <!-- default values for query parameters -->
        <lst name="defaults">
            <str name="echoParams">explicit</str>       
            <int name="rows">1000</int>
            <str name="fl">*</str>
            <str name="version">2.1</str>
        </lst>

        <arr name="components">
            <str>yourQueryComponent</str>
            <str>facet</str>
            <str>mlt</str>
            <str>highlight</str>            
            <str>stats</str>
            <str>debug</str>

        </arr>

  </requestHandler>

然后,当您向Solr发送url查询时,只需包含其他参数qt = YourRequestHandlerName,这将导致您的请求处理程序用于该请求。

More about SearchComponents.
More about RequestHandlers.

答案 1 :(得分:1)

Lucene提供的SnowballAnalyzer已经使用StandardTokenizer,StandardFilter,LowerCaseFilter,StopFilter和SnowballFilter。所以听起来它完全符合你的要求(StandardAnalyzer所做的一切,再加上雪球)。

如果没有,你可以通过组合你想要的任何标记器和TokenStream来轻松构建自己的分析器。

答案 2 :(得分:0)

最后,我重新安排了程序代码,将SnowBallAnalyzer作为一个选项。然后通过StandardAnalyzer索引输出。

它的工作原理很快但是如果我只用一台分析仪就可以完成所有工作,我会重温我的代码。

感谢mbonaci和Avi。