pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / analysis / PerFieldAnalyzerWrapper.java
1 package org.apache.lucene.analysis;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * 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 org.apache.lucene.document.Fieldable;
21
22 import java.io.Reader;
23 import java.io.IOException;
24 import java.util.Map;
25 import java.util.HashMap;
26
27 /**
28  * This analyzer is used to facilitate scenarios where different
29  * fields require different analysis techniques.  Use the Map
30  * argument in {@link #PerFieldAnalyzerWrapper(Analyzer, java.util.Map)}
31  * to add non-default analyzers for fields.
32  * 
33  * <p>Example usage:
34  * 
35  * <pre>
36  *   Map analyzerPerField = new HashMap();
37  *   analyzerPerField.put("firstname", new KeywordAnalyzer());
38  *   analyzerPerField.put("lastname", new KeywordAnalyzer());
39  *
40  *   PerFieldAnalyzerWrapper aWrapper =
41  *      new PerFieldAnalyzerWrapper(new StandardAnalyzer(), analyzerPerField);
42  * </pre>
43  * 
44  * <p>In this example, StandardAnalyzer will be used for all fields except "firstname"
45  * and "lastname", for which KeywordAnalyzer will be used.
46  * 
47  * <p>A PerFieldAnalyzerWrapper can be used like any other analyzer, for both indexing
48  * and query parsing.
49  */
50 public final class PerFieldAnalyzerWrapper extends Analyzer {
51   private final Analyzer defaultAnalyzer;
52   private final Map<String,Analyzer> analyzerMap = new HashMap<String,Analyzer>();
53
54
55   /**
56    * Constructs with default analyzer.
57    *
58    * @param defaultAnalyzer Any fields not specifically
59    * defined to use a different analyzer will use the one provided here.
60    */
61   public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer) {
62     this(defaultAnalyzer, null);
63   }
64   
65   /**
66    * Constructs with default analyzer and a map of analyzers to use for 
67    * specific fields.
68    *
69    * @param defaultAnalyzer Any fields not specifically
70    * defined to use a different analyzer will use the one provided here.
71    * @param fieldAnalyzers a Map (String field name to the Analyzer) to be 
72    * used for those fields 
73    */
74   public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer, 
75       Map<String,Analyzer> fieldAnalyzers) {
76     this.defaultAnalyzer = defaultAnalyzer;
77     if (fieldAnalyzers != null) {
78       analyzerMap.putAll(fieldAnalyzers);
79     }
80   }
81   
82
83   /**
84    * Defines an analyzer to use for the specified field.
85    *
86    * @param fieldName field name requiring a non-default analyzer
87    * @param analyzer non-default analyzer to use for field
88    * @deprecated Changing the Analyzer for a field after instantiation prevents
89    *             reusability.  Analyzers for fields should be set during construction.
90    */
91   @Deprecated
92   public void addAnalyzer(String fieldName, Analyzer analyzer) {
93     analyzerMap.put(fieldName, analyzer);
94   }
95
96   @Override
97   public TokenStream tokenStream(String fieldName, Reader reader) {
98     Analyzer analyzer = analyzerMap.get(fieldName);
99     if (analyzer == null) {
100       analyzer = defaultAnalyzer;
101     }
102
103     return analyzer.tokenStream(fieldName, reader);
104   }
105   
106   @Override
107   public TokenStream reusableTokenStream(String fieldName, Reader reader) throws IOException {
108     Analyzer analyzer = analyzerMap.get(fieldName);
109     if (analyzer == null)
110       analyzer = defaultAnalyzer;
111
112     return analyzer.reusableTokenStream(fieldName, reader);
113   }
114   
115   /** Return the positionIncrementGap from the analyzer assigned to fieldName */
116   @Override
117   public int getPositionIncrementGap(String fieldName) {
118     Analyzer analyzer = analyzerMap.get(fieldName);
119     if (analyzer == null)
120       analyzer = defaultAnalyzer;
121     return analyzer.getPositionIncrementGap(fieldName);
122   }
123
124   /** Return the offsetGap from the analyzer assigned to field */
125   @Override
126   public int getOffsetGap(Fieldable field) {
127     Analyzer analyzer = analyzerMap.get(field.name());
128     if (analyzer == null)
129       analyzer = defaultAnalyzer;
130     return analyzer.getOffsetGap(field);
131   }
132   
133   @Override
134   public String toString() {
135     return "PerFieldAnalyzerWrapper(" + analyzerMap + ", default=" + defaultAnalyzer + ")";
136   }
137 }