KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > analysis > PerFieldAnalyzerWrapper


1 package org.apache.lucene.analysis;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.Reader JavaDoc;
20 import java.util.Map JavaDoc;
21 import java.util.HashMap JavaDoc;
22
23 /**
24  * This analyzer is used to facilitate scenarios where different
25  * fields require different analysis techniques. Use {@link #addAnalyzer}
26  * to add a non-default analyzer on a field name basis.
27  *
28  * <p>Example usage:
29  *
30  * <pre>
31  * PerFieldAnalyzerWrapper aWrapper =
32  * new PerFieldAnalyzerWrapper(new StandardAnalyzer());
33  * aWrapper.addAnalyzer("firstname", new KeywordAnalyzer());
34  * aWrapper.addAnalyzer("lastname", new KeywordAnalyzer());
35  * </pre>
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  */

43 public class PerFieldAnalyzerWrapper extends Analyzer {
44   private Analyzer defaultAnalyzer;
45   private Map JavaDoc analyzerMap = new HashMap JavaDoc();
46
47
48   /**
49    * Constructs with default analyzer.
50    *
51    * @param defaultAnalyzer Any fields not specifically
52    * defined to use a different analyzer will use the one provided here.
53    */

54   public PerFieldAnalyzerWrapper(Analyzer defaultAnalyzer) {
55     this.defaultAnalyzer = defaultAnalyzer;
56   }
57
58   /**
59    * Defines an analyzer to use for the specified field.
60    *
61    * @param fieldName field name requiring a non-default analyzer
62    * @param analyzer non-default analyzer to use for field
63    */

64   public void addAnalyzer(String JavaDoc fieldName, Analyzer analyzer) {
65     analyzerMap.put(fieldName, analyzer);
66   }
67
68   public TokenStream tokenStream(String JavaDoc fieldName, Reader JavaDoc reader) {
69     Analyzer analyzer = (Analyzer) analyzerMap.get(fieldName);
70     if (analyzer == null) {
71       analyzer = defaultAnalyzer;
72     }
73
74     return analyzer.tokenStream(fieldName, reader);
75   }
76   
77   public String JavaDoc toString() {
78     return "PerFieldAnalyzerWrapper(" + analyzerMap + ", default=" + defaultAnalyzer + ")";
79   }
80 }
81
Popular Tags