What are the best methods for combining analyzers in Lucene?

I have a situation where I use StandardAnalyzer in Lucene to index text strings as follows:

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);
}

This works fine, but I would like to combine this using SnowballAnalyzer.

This class also has two instance variables, listed below in the constructor:

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

Can someone tell me what is the best way to achieve this using the code above?

thank

Mr. Morgan.

+3
source share
3 answers

Lucene provides a base class org.apache.lucene.analysis.Analyzerthat you can use if you want to write your own parser.
You can check the class org.apache.lucene.analysis.standard.StandardAnalyzerthat extends Analyzer.

YourAnalyzer StandardAnalyzer SnowballAnalyzer, , , :

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

IndexWriter Analyzer, 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>

, url- Solr, qt = YourRequestHandlerName, , .

SearchComponents.
RequestHandlers.

+2

SnowballAnalyzer, Lucene, StandardTokenizer, StandardFilter, LowerCaseFilter, StopFilter SnowballFilter. , , (, StandardAnalyzer, ).

, , TokenStreams, .

+1

, SnowBallAnalyzer . StandardAnalyzer.

, , .

mbonaci Avi.

0

Source: https://habr.com/ru/post/1796532/


All Articles