2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 namespace Mono.Lucene.Net.Analysis
23 /// <summary> This analyzer is used to facilitate scenarios where different
24 /// fields require different analysis techniques. Use {@link #addAnalyzer}
25 /// to add a non-default analyzer on a field name basis.
27 /// <p/>Example usage:
30 /// PerFieldAnalyzerWrapper aWrapper =
31 /// new PerFieldAnalyzerWrapper(new StandardAnalyzer());
32 /// aWrapper.addAnalyzer("firstname", new KeywordAnalyzer());
33 /// aWrapper.addAnalyzer("lastname", new KeywordAnalyzer());
36 /// <p/>In this example, StandardAnalyzer will be used for all fields except "firstname"
37 /// and "lastname", for which KeywordAnalyzer will be used.
39 /// <p/>A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing
40 /// and query parsing.
42 public class PerFieldAnalyzerWrapper:Analyzer
44 private Analyzer defaultAnalyzer;
45 private System.Collections.IDictionary analyzerMap = new System.Collections.Hashtable();
48 /// <summary> Constructs with default analyzer.
51 /// <param name="defaultAnalyzer">Any fields not specifically
52 /// defined to use a different analyzer will use the one provided here.
54 public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer):this(defaultAnalyzer, null)
58 /// <summary> Constructs with default analyzer and a map of analyzers to use for
62 /// <param name="defaultAnalyzer">Any fields not specifically
63 /// defined to use a different analyzer will use the one provided here.
65 /// <param name="fieldAnalyzers">a Map (String field name to the Analyzer) to be
66 /// used for those fields
68 public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer, System.Collections.IDictionary fieldAnalyzers)
70 this.defaultAnalyzer = defaultAnalyzer;
71 if (fieldAnalyzers != null)
73 System.Collections.ArrayList keys = new System.Collections.ArrayList(fieldAnalyzers.Keys);
74 System.Collections.ArrayList values = new System.Collections.ArrayList(fieldAnalyzers.Values);
76 for (int i=0; i < keys.Count; i++)
77 analyzerMap[keys[i]] = values[i];
79 SetOverridesTokenStreamMethod(typeof(PerFieldAnalyzerWrapper));
83 /// <summary> Defines an analyzer to use for the specified field.
86 /// <param name="fieldName">field name requiring a non-default analyzer
88 /// <param name="analyzer">non-default analyzer to use for field
90 public virtual void AddAnalyzer(System.String fieldName, Analyzer analyzer)
92 analyzerMap[fieldName] = analyzer;
95 public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
97 Analyzer analyzer = (Analyzer) analyzerMap[fieldName];
100 analyzer = defaultAnalyzer;
103 return analyzer.TokenStream(fieldName, reader);
106 public override TokenStream ReusableTokenStream(System.String fieldName, System.IO.TextReader reader)
108 if (overridesTokenStreamMethod)
110 // LUCENE-1678: force fallback to tokenStream() if we
111 // have been subclassed and that subclass overrides
112 // tokenStream but not reusableTokenStream
113 return TokenStream(fieldName, reader);
115 Analyzer analyzer = (Analyzer) analyzerMap[fieldName];
116 if (analyzer == null)
117 analyzer = defaultAnalyzer;
119 return analyzer.ReusableTokenStream(fieldName, reader);
122 /// <summary>Return the positionIncrementGap from the analyzer assigned to fieldName </summary>
123 public override int GetPositionIncrementGap(System.String fieldName)
125 Analyzer analyzer = (Analyzer) analyzerMap[fieldName];
126 if (analyzer == null)
127 analyzer = defaultAnalyzer;
128 return analyzer.GetPositionIncrementGap(fieldName);
131 /// <summary> Return the offsetGap from the analyzer assigned to field </summary>
132 public override int GetOffsetGap(Mono.Lucene.Net.Documents.Fieldable field)
134 Analyzer analyzer = (Analyzer)analyzerMap[field.Name()];
135 if (analyzer == null)
136 analyzer = defaultAnalyzer;
137 return analyzer.GetOffsetGap(field);
140 public override System.String ToString()
142 // {{Aroush-2.9}} will 'analyzerMap.ToString()' work in the same way as Java's java.util.HashMap.toString()?
143 return "PerFieldAnalyzerWrapper(" + analyzerMap.ToString() + ", default=" + defaultAnalyzer + ")";