2 * Copyright 2004 The Apache Software Foundation
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
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.
29 /// PerFieldAnalyzerWrapper aWrapper =
30 /// new PerFieldAnalyzerWrapper(new StandardAnalyzer());
31 /// aWrapper.addAnalyzer("firstname", new KeywordAnalyzer());
32 /// aWrapper.addAnalyzer("lastname", new KeywordAnalyzer());
35 /// <p>In this example, StandardAnalyzer will be used for all fields except "firstname"
36 /// and "lastname", for which KeywordAnalyzer will be used.
38 /// <p>A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing
39 /// and query parsing.
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.
50 /// <param name="defaultAnalyzer">Any fields not specifically
51 /// defined to use a different analyzer will use the one provided here.
53 public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer
)
55 this.defaultAnalyzer
= defaultAnalyzer
;
58 /// <summary> Defines an analyzer to use for the specified field.
61 /// <param name="fieldName">field name requiring a non-default analyzer
63 /// <param name="analyzer">non-default analyzer to use for field
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
];
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
+ ")";