DLESE Tools
v1.6.0

org.dlese.dpc.index.analysis
Class PerFieldAnalyzer

java.lang.Object
  extended by org.apache.lucene.analysis.Analyzer
      extended by org.dlese.dpc.index.analysis.PerFieldAnalyzer
All Implemented Interfaces:
Closeable

public class PerFieldAnalyzer
extends org.apache.lucene.analysis.Analyzer

This Analyzer is used to facilitate scenarios where different fields require different analysis techniques. Use #addAnalyzer to add a non-default Analyzer or addAnalyzersInBundle(java.util.ResourceBundle) to provide a ResourceBundle to configure Analyzers on a field name basis. The ResourceBundle should contain className=field1,field2,... pairs, where the field names are a comma-separated list, for example:

org.dlese.dpc.index.analysis.SnowballAnalyzer=stems,titlestems

Author:
John Weatherley

Field Summary
static String KEYWORD_ANALYZER
           
static String STEMS_ANALYZER
           
static String TEXT_ANALYZER
           
 
Fields inherited from class org.apache.lucene.analysis.Analyzer
overridesTokenStreamMethod
 
Constructor Summary
PerFieldAnalyzer()
          Constructs using a StandardAnalyzer as the default for fields not otherwise configured.
PerFieldAnalyzer(org.apache.lucene.analysis.Analyzer defaultAnalyzer)
          Constructs with the given Analyzer to use as a default for fields not otherwise configured.
 
Method Summary
 void addAnalyzersInBundle(ResourceBundle fieldAnalyzerBundle)
          Adds the Analyzers to use for given fields, using the field=className pairs provided in the ResourceBundle, overrridding any previous ones if they existed.
 boolean containsAnalyzer(String fieldName)
          Determines if an Analyzer is configured for the given field.
 org.apache.lucene.analysis.Analyzer getAnalyzer(String fieldName)
          Gets the Analyzer configured for the given field, or null if none exists.
 org.apache.lucene.analysis.Analyzer getDefaultAnalyzer()
          Gets the default Analyzer being used.
 org.apache.lucene.analysis.Analyzer removeAnalyzer(String fieldName)
          Removes the Analyzer that is configured for the given field, if one exists.
 void setAnalyzer(String fieldName, String analyzerClassName)
          Sets the Analyzer to use for the specified search field, overridding the previous one if it existed.
 void setDefaultAnalyzer(org.apache.lucene.analysis.Analyzer analyzer)
          Sets the default Analyzer to use from here forth.
 org.apache.lucene.analysis.TokenStream tokenStream(String fieldName, Reader reader)
          Generates a token stream for the given field.
 String toString()
           
 
Methods inherited from class org.apache.lucene.analysis.Analyzer
close, getOffsetGap, getPositionIncrementGap, getPreviousTokenStream, reusableTokenStream, setOverridesTokenStreamMethod, setPreviousTokenStream
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

TEXT_ANALYZER

public static final String TEXT_ANALYZER
See Also:
Constant Field Values

KEYWORD_ANALYZER

public static final String KEYWORD_ANALYZER
See Also:
Constant Field Values

STEMS_ANALYZER

public static final String STEMS_ANALYZER
See Also:
Constant Field Values
Constructor Detail

PerFieldAnalyzer

public PerFieldAnalyzer(org.apache.lucene.analysis.Analyzer defaultAnalyzer)
Constructs with the given Analyzer to use as a default for fields not otherwise configured. If null, a StandardAnalyzer will be used as the default.

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

PerFieldAnalyzer

public PerFieldAnalyzer()
Constructs using a StandardAnalyzer as the default for fields not otherwise configured.

Method Detail

setAnalyzer

public void setAnalyzer(String fieldName,
                        String analyzerClassName)
                 throws ClassNotFoundException,
                        InstantiationException,
                        IllegalAccessException
Sets the Analyzer to use for the specified search field, overridding the previous one if it existed.

Parameters:
fieldName - field name requiring a non-default analyzer.
analyzerClassName - Name of Analyzer class to use for the field
Throws:
ClassNotFoundException - If error
InstantiationException - If error
IllegalAccessException - If error

toString

public String toString()
Overrides:
toString in class Object

addAnalyzersInBundle

public void addAnalyzersInBundle(ResourceBundle fieldAnalyzerBundle)
                          throws ClassNotFoundException,
                                 InstantiationException,
                                 IllegalAccessException
Adds the Analyzers to use for given fields, using the field=className pairs provided in the ResourceBundle, overrridding any previous ones if they existed. The ResourceBundle should contain className=field1,field2,... pairs, where the field names are a comma-separated list, for example:

org.dlese.dpc.index.analysis.SnowballAnalyzer=stems,titlestems

Parameters:
fieldAnalyzerBundle - A resource bundle containing className=field1,field2,etc. pairs
Throws:
ClassNotFoundException - If error
InstantiationException - If error
IllegalAccessException - If error

getAnalyzer

public org.apache.lucene.analysis.Analyzer getAnalyzer(String fieldName)
Gets the Analyzer configured for the given field, or null if none exists.

Parameters:
fieldName - The field name
Returns:
The Analyzer

getDefaultAnalyzer

public org.apache.lucene.analysis.Analyzer getDefaultAnalyzer()
Gets the default Analyzer being used.

Returns:
The default Analyzer

setDefaultAnalyzer

public void setDefaultAnalyzer(org.apache.lucene.analysis.Analyzer analyzer)
Sets the default Analyzer to use from here forth.

Parameters:
analyzer - The new default Analyzer

containsAnalyzer

public boolean containsAnalyzer(String fieldName)
Determines if an Analyzer is configured for the given field.

Parameters:
fieldName - The field name
Returns:
True if an Analyzer is configured for the given field

removeAnalyzer

public org.apache.lucene.analysis.Analyzer removeAnalyzer(String fieldName)
Removes the Analyzer that is configured for the given field, if one exists. After removing, the given will will use the default Analyzer.

Parameters:
fieldName - The field name
Returns:
The Analyzer that was configured, or null

tokenStream

public org.apache.lucene.analysis.TokenStream tokenStream(String fieldName,
                                                          Reader reader)
Generates a token stream for the given field.

Specified by:
tokenStream in class org.apache.lucene.analysis.Analyzer
Parameters:
fieldName - The field name
reader - The Reader
Returns:
The TokenStream appropriate for this field

DLESE Tools
v1.6.0