Package org.apache.lucene.analysis
Class PerFieldAnalyzerWrapper
- java.lang.Object
-
- org.apache.lucene.analysis.Analyzer
-
- org.apache.lucene.analysis.PerFieldAnalyzerWrapper
-
- All Implemented Interfaces:
Closeable
,AutoCloseable
public final class PerFieldAnalyzerWrapper extends Analyzer
This analyzer is used to facilitate scenarios where different fields require different analysis techniques. Use the Map argument inPerFieldAnalyzerWrapper(Analyzer, java.util.Map)
to add non-default analyzers for fields.Example usage:
Map analyzerPerField = new HashMap(); analyzerPerField.put("firstname", new KeywordAnalyzer()); analyzerPerField.put("lastname", new KeywordAnalyzer()); PerFieldAnalyzerWrapper aWrapper = new PerFieldAnalyzerWrapper(new StandardAnalyzer(), analyzerPerField);
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.
-
-
Constructor Summary
Constructors Constructor Description PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer)
Constructs with default analyzer.PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer, Map<String,Analyzer> fieldAnalyzers)
Constructs with default analyzer and a map of analyzers to use for specific fields.
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description void
addAnalyzer(String fieldName, Analyzer analyzer)
Deprecated.Changing the Analyzer for a field after instantiation prevents reusability.int
getOffsetGap(Fieldable field)
Return the offsetGap from the analyzer assigned to fieldint
getPositionIncrementGap(String fieldName)
Return the positionIncrementGap from the analyzer assigned to fieldNameTokenStream
reusableTokenStream(String fieldName, Reader reader)
Creates a TokenStream that is allowed to be re-used from the previous time that the same thread called this method.TokenStream
tokenStream(String fieldName, Reader reader)
Creates a TokenStream which tokenizes all the text in the provided Reader.String
toString()
-
Methods inherited from class org.apache.lucene.analysis.Analyzer
close, getPreviousTokenStream, setPreviousTokenStream
-
-
-
-
Constructor Detail
-
PerFieldAnalyzerWrapper
public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer)
Constructs with default analyzer.- Parameters:
defaultAnalyzer
- Any fields not specifically defined to use a different analyzer will use the one provided here.
-
PerFieldAnalyzerWrapper
public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer, Map<String,Analyzer> fieldAnalyzers)
Constructs with default analyzer and a map of analyzers to use for specific fields.- Parameters:
defaultAnalyzer
- Any fields not specifically defined to use a different analyzer will use the one provided here.fieldAnalyzers
- a Map (String field name to the Analyzer) to be used for those fields
-
-
Method Detail
-
addAnalyzer
@Deprecated public void addAnalyzer(String fieldName, Analyzer analyzer)
Deprecated.Changing the Analyzer for a field after instantiation prevents reusability. Analyzers for fields should be set during construction.Defines an analyzer to use for the specified field.- Parameters:
fieldName
- field name requiring a non-default analyzeranalyzer
- non-default analyzer to use for field
-
tokenStream
public TokenStream tokenStream(String fieldName, Reader reader)
Description copied from class:Analyzer
Creates a TokenStream which tokenizes all the text in the provided Reader. Must be able to handle null field name for backward compatibility.- Specified by:
tokenStream
in classAnalyzer
-
reusableTokenStream
public TokenStream reusableTokenStream(String fieldName, Reader reader) throws IOException
Description copied from class:Analyzer
Creates a TokenStream that is allowed to be re-used from the previous time that the same thread called this method. Callers that do not need to use more than one TokenStream at the same time from this analyzer should use this method for better performance.- Overrides:
reusableTokenStream
in classAnalyzer
- Throws:
IOException
-
getPositionIncrementGap
public int getPositionIncrementGap(String fieldName)
Return the positionIncrementGap from the analyzer assigned to fieldName- Overrides:
getPositionIncrementGap
in classAnalyzer
- Parameters:
fieldName
- Fieldable name being indexed.- Returns:
- position increment gap, added to the next token emitted from
Analyzer.tokenStream(String,Reader)
-
getOffsetGap
public int getOffsetGap(Fieldable field)
Return the offsetGap from the analyzer assigned to field- Overrides:
getOffsetGap
in classAnalyzer
- Parameters:
field
- the field just indexed- Returns:
- offset gap, added to the next token emitted from
Analyzer.tokenStream(String,Reader)
-
-