Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
PerFieldAnalyzerWrapper.cs
Go to the documentation of this file.
1 /*
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 using System.Collections.Generic;
19 using Lucene.Net.Support;
20 
21 namespace Lucene.Net.Analysis
22 {
23 
24  /// <summary> This analyzer is used to facilitate scenarios where different
25  /// fields require different analysis techniques. Use <see cref="AddAnalyzer" />
26  /// to add a non-default analyzer on a field name basis.
27  ///
28  /// <p/>Example usage:
29  ///
30  /// <code>
31  /// PerFieldAnalyzerWrapper aWrapper =
32  /// new PerFieldAnalyzerWrapper(new StandardAnalyzer());
33  /// aWrapper.addAnalyzer("firstname", new KeywordAnalyzer());
34  /// aWrapper.addAnalyzer("lastname", new KeywordAnalyzer());
35  /// </code>
36  ///
37  /// <p/>In this example, StandardAnalyzer will be used for all fields except "firstname"
38  /// and "lastname", for which KeywordAnalyzer will be used.
39  ///
40  /// <p/>A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing
41  /// and query parsing.
42  /// </summary>
44  {
45  private readonly Analyzer defaultAnalyzer;
46  private readonly IDictionary<string, Analyzer> analyzerMap = new HashMap<string, Analyzer>();
47 
48 
49  /// <summary> Constructs with default analyzer.
50  ///
51  /// </summary>
52  /// <param name="defaultAnalyzer">Any fields not specifically
53  /// defined to use a different analyzer will use the one provided here.
54  /// </param>
55  public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer)
56  : this(defaultAnalyzer, null)
57  {
58  }
59 
60  /// <summary> Constructs with default analyzer and a map of analyzers to use for
61  /// specific fields.
62  ///
63  /// </summary>
64  /// <param name="defaultAnalyzer">Any fields not specifically
65  /// defined to use a different analyzer will use the one provided here.
66  /// </param>
67  /// <param name="fieldAnalyzers">a Map (String field name to the Analyzer) to be
68  /// used for those fields
69  /// </param>
70  public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer, IEnumerable<KeyValuePair<string, Analyzer>> fieldAnalyzers)
71  {
72  this.defaultAnalyzer = defaultAnalyzer;
73  if (fieldAnalyzers != null)
74  {
75  foreach(var entry in fieldAnalyzers)
76  analyzerMap[entry.Key] = entry.Value;
77  }
78  SetOverridesTokenStreamMethod<PerFieldAnalyzerWrapper>();
79  }
80 
81 
82  /// <summary> Defines an analyzer to use for the specified field.
83  ///
84  /// </summary>
85  /// <param name="fieldName">field name requiring a non-default analyzer
86  /// </param>
87  /// <param name="analyzer">non-default analyzer to use for field
88  /// </param>
89  public virtual void AddAnalyzer(System.String fieldName, Analyzer analyzer)
90  {
91  analyzerMap[fieldName] = analyzer;
92  }
93 
94  public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
95  {
96  var analyzer = analyzerMap[fieldName] ?? defaultAnalyzer;
97 
98  return analyzer.TokenStream(fieldName, reader);
99  }
100 
101  public override TokenStream ReusableTokenStream(string fieldName, System.IO.TextReader reader)
102  {
103  if (overridesTokenStreamMethod)
104  {
105  // LUCENE-1678: force fallback to tokenStream() if we
106  // have been subclassed and that subclass overrides
107  // tokenStream but not reusableTokenStream
108  return TokenStream(fieldName, reader);
109  }
110  var analyzer = analyzerMap[fieldName] ?? defaultAnalyzer;
111 
112  return analyzer.ReusableTokenStream(fieldName, reader);
113  }
114 
115  /// <summary>Return the positionIncrementGap from the analyzer assigned to fieldName </summary>
116  public override int GetPositionIncrementGap(string fieldName)
117  {
118  var analyzer = analyzerMap[fieldName] ?? defaultAnalyzer;
119  return analyzer.GetPositionIncrementGap(fieldName);
120  }
121 
122  /// <summary> Return the offsetGap from the analyzer assigned to field </summary>
123  public override int GetOffsetGap(Documents.IFieldable field)
124  {
125  Analyzer analyzer = analyzerMap[field.Name] ?? defaultAnalyzer;
126  return analyzer.GetOffsetGap(field);
127  }
128 
129  public override System.String ToString()
130  {
131  // {{Aroush-2.9}} will 'analyzerMap.ToString()' work in the same way as Java's java.util.HashMap.toString()?
132  return "PerFieldAnalyzerWrapper(" + analyzerMap + ", default=" + defaultAnalyzer + ")";
133  }
134  }
135 }