cvsimport
[beagle.git] / beagled / Lucene.Net / Analysis / PerFieldAnalyzerWrapper.cs
blobd82724d55b15346d19157c7b44629f9a3d60dbe4
1 /*
2 * Copyright 2004 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 using System;
19 namespace Lucene.Net.Analysis
22 /// <summary> This analyzer is used to facilitate scenarios where different
23 /// fields require different analysis techniques. Use {@link #addAnalyzer}
24 /// to add a non-default analyzer on a field name basis.
25 ///
26 /// <p>Example usage:
27 ///
28 /// <pre>
29 /// PerFieldAnalyzerWrapper aWrapper =
30 /// new PerFieldAnalyzerWrapper(new StandardAnalyzer());
31 /// aWrapper.addAnalyzer("firstname", new KeywordAnalyzer());
32 /// aWrapper.addAnalyzer("lastname", new KeywordAnalyzer());
33 /// </pre>
34 ///
35 /// <p>In this example, StandardAnalyzer will be used for all fields except "firstname"
36 /// and "lastname", for which KeywordAnalyzer will be used.
37 ///
38 /// <p>A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing
39 /// and query parsing.
40 /// </summary>
41 public class PerFieldAnalyzerWrapper:Analyzer
43 private Analyzer defaultAnalyzer;
44 private System.Collections.IDictionary analyzerMap = new System.Collections.Hashtable();
47 /// <summary> Constructs with default analyzer.
48 ///
49 /// </summary>
50 /// <param name="defaultAnalyzer">Any fields not specifically
51 /// defined to use a different analyzer will use the one provided here.
52 /// </param>
53 public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer)
55 this.defaultAnalyzer = defaultAnalyzer;
58 /// <summary> Defines an analyzer to use for the specified field.
59 ///
60 /// </summary>
61 /// <param name="fieldName">field name requiring a non-default analyzer
62 /// </param>
63 /// <param name="analyzer">non-default analyzer to use for field
64 /// </param>
65 public virtual void AddAnalyzer(System.String fieldName, Analyzer analyzer)
67 analyzerMap[fieldName] = analyzer;
70 public override TokenStream TokenStream(System.String fieldName, System.IO.TextReader reader)
72 Analyzer analyzer = (Analyzer) analyzerMap[fieldName];
73 if (analyzer == null)
75 analyzer = defaultAnalyzer;
78 return analyzer.TokenStream(fieldName, reader);
81 public override System.String ToString()
83 // {{Aroush-1.9}} 'analyzerMap.ToString()' may return a different value then Java.
84 return "PerFieldAnalyzerWrapper(" + analyzerMap.ToString() + ", default=" + defaultAnalyzer + ")";