2011-03-05 73 views
3

我有我使用的是StandardAnalyzer在Lucene的地方索引的文本字符串如下的情況:哪些工作得很好在Lucene中結合分析器的最佳實踐是什麼?

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

誰能告訴我如何最好與上面的代碼來實現這一目標?

謝謝

摩根先生。

回答

2

Lucene提供org.apache.lucene.analysis.Analyzer基類,如果您想編寫自己的分析器,可以使用它。
您可以檢出org.apache.lucene.analysis.standard.StandardAnalyzer擴展Analyzer的類。

然後,在YourAnalyzer,你會鏈StandardAnalyzer和SnowballAnalyzer通過使用這些分析儀使用的過濾器,像這樣:

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

然後,在你現有的代碼,你就能夠建立的IndexWriter與您自己的分析器實施,鏈接標準和雪球過濾器。

完全題外話:
我想你最終需要設置的處理請求您的自定義方式。這已經在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,只是包括額外的參數QT = YourRequestHandlerName,這將導致您的請求處理程序被用於該請求。

More about SearchComponents.
More about RequestHandlers.

1

由Lucene的提供的SnowballAnalyzer已經使用了StandardTokenizer,StandardFilter,LowerCaseFilter,的StopFilter和SnowballFilter。所以它聽起來像是你想要的東西(StandardAnalyzer所做的一切,加上雪球的起源)。

如果沒有,你可以通過組合你想要的任何標記器和TokenStream來輕鬆地構建你自己的分析器。

0

最後,我重新安排了程序代碼來調用SnowBallAnalyzer作爲選項。然後通過StandardAnalyzer對輸出進行索引。

它的工作原理和速度很快,但如果我只用一臺分析儀即可完成所有工作,我將重新訪問我的代碼。

感謝mbonaci和Avi。