Class PerFieldAnalyzer

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

public class PerFieldAnalyzer extends org.apache.lucene.analysis.Analyzer
This Analyzer is used to facilitate scenarios where different fields require different analysis techniques. Use
invalid reference
#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

    Fields
    Modifier and Type
    Field
    Description
    static final String
     
    static final String
     
    static final String
     

    Fields inherited from class org.apache.lucene.analysis.Analyzer

    overridesTokenStreamMethod
  • Constructor Summary

    Constructors
    Constructor
    Description
    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

    Modifier and Type
    Method
    Description
    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
    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
    Gets the default Analyzer being used.
    org.apache.lucene.analysis.Analyzer
    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.
     

    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 Details

  • Constructor Details

    • 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 Details

    • 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