01: package org.apache.lucene.analysis;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
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.
18: */
19:
20: import java.io.Reader;
21: import java.io.IOException;
22: import java.util.Map;
23: import java.util.HashMap;
24:
25: /**
26: * This analyzer is used to facilitate scenarios where different
27: * fields require different analysis techniques. Use {@link #addAnalyzer}
28: * to add a non-default analyzer on a field name basis.
29: *
30: * <p>Example usage:
31: *
32: * <pre>
33: * PerFieldAnalyzerWrapper aWrapper =
34: * new PerFieldAnalyzerWrapper(new StandardAnalyzer());
35: * aWrapper.addAnalyzer("firstname", new KeywordAnalyzer());
36: * aWrapper.addAnalyzer("lastname", new KeywordAnalyzer());
37: * </pre>
38: *
39: * <p>In this example, StandardAnalyzer will be used for all fields except "firstname"
40: * and "lastname", for which KeywordAnalyzer will be used.
41: *
42: * <p>A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing
43: * and query parsing.
44: */
45: public class PerFieldAnalyzerWrapper extends Analyzer {
46: private Analyzer defaultAnalyzer;
47: private Map analyzerMap = new HashMap();
48:
49: /**
50: * Constructs with default analyzer.
51: *
52: * @param defaultAnalyzer Any fields not specifically
53: * defined to use a different analyzer will use the one provided here.
54: */
55: public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer) {
56: this .defaultAnalyzer = defaultAnalyzer;
57: }
58:
59: /**
60: * Defines an analyzer to use for the specified field.
61: *
62: * @param fieldName field name requiring a non-default analyzer
63: * @param analyzer non-default analyzer to use for field
64: */
65: public void addAnalyzer(String fieldName, Analyzer analyzer) {
66: analyzerMap.put(fieldName, analyzer);
67: }
68:
69: public TokenStream tokenStream(String fieldName, Reader reader) {
70: Analyzer analyzer = (Analyzer) analyzerMap.get(fieldName);
71: if (analyzer == null) {
72: analyzer = defaultAnalyzer;
73: }
74:
75: return analyzer.tokenStream(fieldName, reader);
76: }
77:
78: public TokenStream reusableTokenStream(String fieldName,
79: Reader reader) throws IOException {
80: Analyzer analyzer = (Analyzer) analyzerMap.get(fieldName);
81: if (analyzer == null)
82: analyzer = defaultAnalyzer;
83:
84: return analyzer.reusableTokenStream(fieldName, reader);
85: }
86:
87: /** Return the positionIncrementGap from the analyzer assigned to fieldName */
88: public int getPositionIncrementGap(String fieldName) {
89: Analyzer analyzer = (Analyzer) analyzerMap.get(fieldName);
90: if (analyzer == null)
91: analyzer = defaultAnalyzer;
92: return analyzer.getPositionIncrementGap(fieldName);
93: }
94:
95: public String toString() {
96: return "PerFieldAnalyzerWrapper(" + analyzerMap + ", default="
97: + defaultAnalyzer + ")";
98: }
99: }
|