FileDocCategorySizeDatePackage
PerFieldAnalyzerWrapper.javaAPI DocApache Lucene 2.1.03107Wed Feb 14 10:46:38 GMT 2007org.apache.lucene.analysis

PerFieldAnalyzerWrapper

public class PerFieldAnalyzerWrapper extends Analyzer
This analyzer is used to facilitate scenarios where different fields require different analysis techniques. Use {@link #addAnalyzer} to add a non-default analyzer on a field name basis.

Example usage:

PerFieldAnalyzerWrapper aWrapper =
new PerFieldAnalyzerWrapper(new StandardAnalyzer());
aWrapper.addAnalyzer("firstname", new KeywordAnalyzer());
aWrapper.addAnalyzer("lastname", new KeywordAnalyzer());

In this example, StandardAnalyzer will be used for all fields except "firstname" and "lastname", for which KeywordAnalyzer will be used.

A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing and query parsing.

Fields Summary
private Analyzer
defaultAnalyzer
private Map
analyzerMap
Constructors Summary
public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer)
Constructs with default analyzer.

param
defaultAnalyzer Any fields not specifically defined to use a different analyzer will use the one provided here.



                           
     
    this.defaultAnalyzer = defaultAnalyzer;
  
Methods Summary
public voidaddAnalyzer(java.lang.String fieldName, org.apache.lucene.analysis.Analyzer analyzer)
Defines an analyzer to use for the specified field.

param
fieldName field name requiring a non-default analyzer
param
analyzer non-default analyzer to use for field

    analyzerMap.put(fieldName, analyzer);
  
public intgetPositionIncrementGap(java.lang.String fieldName)
Return the positionIncrementGap from the analyzer assigned to fieldName

    Analyzer analyzer = (Analyzer) analyzerMap.get(fieldName);
    if (analyzer == null)
      analyzer = defaultAnalyzer;
    return analyzer.getPositionIncrementGap(fieldName);
  
public java.lang.StringtoString()

    return "PerFieldAnalyzerWrapper(" + analyzerMap + ", default=" + defaultAnalyzer + ")";
  
public org.apache.lucene.analysis.TokenStreamtokenStream(java.lang.String fieldName, java.io.Reader reader)

    Analyzer analyzer = (Analyzer) analyzerMap.get(fieldName);
    if (analyzer == null) {
      analyzer = defaultAnalyzer;
    }

    return analyzer.tokenStream(fieldName, reader);