1 package org.apache.lucene.analysis;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.document.Fieldable;
22 import java.io.Reader;
23 import java.io.IOException;
25 import java.util.HashMap;
28 * This analyzer is used to facilitate scenarios where different
29 * fields require different analysis techniques. Use the Map
30 * argument in {@link #PerFieldAnalyzerWrapper(Analyzer, java.util.Map)}
31 * to add non-default analyzers for fields.
36 * Map analyzerPerField = new HashMap();
37 * analyzerPerField.put("firstname", new KeywordAnalyzer());
38 * analyzerPerField.put("lastname", new KeywordAnalyzer());
40 * PerFieldAnalyzerWrapper aWrapper =
41 * new PerFieldAnalyzerWrapper(new StandardAnalyzer(), analyzerPerField);
44 * <p>In this example, StandardAnalyzer will be used for all fields except "firstname"
45 * and "lastname", for which KeywordAnalyzer will be used.
47 * <p>A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing
50 public final class PerFieldAnalyzerWrapper extends Analyzer {
51 private final Analyzer defaultAnalyzer;
52 private final Map<String,Analyzer> analyzerMap = new HashMap<String,Analyzer>();
56 * Constructs with default analyzer.
58 * @param defaultAnalyzer Any fields not specifically
59 * defined to use a different analyzer will use the one provided here.
61 public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer) {
62 this(defaultAnalyzer, null);
66 * Constructs with default analyzer and a map of analyzers to use for
69 * @param defaultAnalyzer Any fields not specifically
70 * defined to use a different analyzer will use the one provided here.
71 * @param fieldAnalyzers a Map (String field name to the Analyzer) to be
72 * used for those fields
74 public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer,
75 Map<String,Analyzer> fieldAnalyzers) {
76 this.defaultAnalyzer = defaultAnalyzer;
77 if (fieldAnalyzers != null) {
78 analyzerMap.putAll(fieldAnalyzers);
84 * Defines an analyzer to use for the specified field.
86 * @param fieldName field name requiring a non-default analyzer
87 * @param analyzer non-default analyzer to use for field
88 * @deprecated Changing the Analyzer for a field after instantiation prevents
89 * reusability. Analyzers for fields should be set during construction.
92 public void addAnalyzer(String fieldName, Analyzer analyzer) {
93 analyzerMap.put(fieldName, analyzer);
97 public TokenStream tokenStream(String fieldName, Reader reader) {
98 Analyzer analyzer = analyzerMap.get(fieldName);
99 if (analyzer == null) {
100 analyzer = defaultAnalyzer;
103 return analyzer.tokenStream(fieldName, reader);
107 public TokenStream reusableTokenStream(String fieldName, Reader reader) throws IOException {
108 Analyzer analyzer = analyzerMap.get(fieldName);
109 if (analyzer == null)
110 analyzer = defaultAnalyzer;
112 return analyzer.reusableTokenStream(fieldName, reader);
115 /** Return the positionIncrementGap from the analyzer assigned to fieldName */
117 public int getPositionIncrementGap(String fieldName) {
118 Analyzer analyzer = analyzerMap.get(fieldName);
119 if (analyzer == null)
120 analyzer = defaultAnalyzer;
121 return analyzer.getPositionIncrementGap(fieldName);
124 /** Return the offsetGap from the analyzer assigned to field */
126 public int getOffsetGap(Fieldable field) {
127 Analyzer analyzer = analyzerMap.get(field.name());
128 if (analyzer == null)
129 analyzer = defaultAnalyzer;
130 return analyzer.getOffsetGap(field);
134 public String toString() {
135 return "PerFieldAnalyzerWrapper(" + analyzerMap + ", default=" + defaultAnalyzer + ")";