pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / icu / src / java / org / apache / lucene / collation / ICUCollationKeyAnalyzer.java
1 package org.apache.lucene.collation;
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
21 import com.ibm.icu.text.Collator;
22 import org.apache.lucene.analysis.Analyzer;
23 import org.apache.lucene.analysis.TokenStream;
24 import org.apache.lucene.analysis.KeywordTokenizer;
25 import org.apache.lucene.analysis.Tokenizer;
26
27 import org.apache.lucene.collation.CollationKeyAnalyzer; // javadocs
28
29 import java.io.Reader;
30 import java.io.IOException;
31
32
33 /**
34  * <p>
35  *   Filters {@link KeywordTokenizer} with {@link ICUCollationKeyFilter}.
36  * <p>
37  *   Converts the token into its {@link com.ibm.icu.text.CollationKey}, and
38  *   then encodes the CollationKey with 
39  *   {@link org.apache.lucene.util.IndexableBinaryStringTools}, to allow it to
40  *   be stored as an index term.
41  * </p>
42  * <p>
43  *   <strong>WARNING:</strong> Make sure you use exactly the same Collator at
44  *   index and query time -- CollationKeys are only comparable when produced by
45  *   the same Collator.  {@link com.ibm.icu.text.RuleBasedCollator}s are 
46  *   independently versioned, so it is safe to search against stored
47  *   CollationKeys if the following are exactly the same (best practice is
48  *   to store this information with the index and check that they remain the
49  *   same at query time):
50  * </p>
51  * <ol>
52  *   <li>
53  *     Collator version - see {@link Collator#getVersion()}
54  *   </li>
55  *   <li>
56  *     The collation strength used - see {@link Collator#setStrength(int)}
57  *   </li>
58  * </ol> 
59  * <p>
60  *   CollationKeys generated by ICU Collators are not compatible with those
61  *   generated by java.text.Collators.  Specifically, if you use 
62  *   ICUCollationKeyAnalyzer to generate index terms, do not use 
63  *   {@link CollationKeyAnalyzer} on the query side, or vice versa.
64  * </p>
65  * <p>
66  *   ICUCollationKeyAnalyzer is significantly faster and generates significantly
67  *   shorter keys than CollationKeyAnalyzer.  See
68  *   <a href="http://site.icu-project.org/charts/collation-icu4j-sun"
69  *   >http://site.icu-project.org/charts/collation-icu4j-sun</a> for key
70  *   generation timing and key length comparisons between ICU4J and
71  *   java.text.Collator over several languages.
72  * </p>
73  */
74 public final class ICUCollationKeyAnalyzer extends Analyzer {
75   private Collator collator;
76
77   public ICUCollationKeyAnalyzer(Collator collator) {
78     this.collator = collator;
79   }
80
81   @Override
82   public TokenStream tokenStream(String fieldName, Reader reader) {
83     TokenStream result = new KeywordTokenizer(reader);
84     result = new ICUCollationKeyFilter(result, collator);
85     return result;
86   }
87   
88   private class SavedStreams {
89     Tokenizer source;
90     TokenStream result;
91   }
92   
93   @Override
94   public TokenStream reusableTokenStream(String fieldName, Reader reader) 
95     throws IOException {
96     
97     SavedStreams streams = (SavedStreams)getPreviousTokenStream();
98     if (streams == null) {
99       streams = new SavedStreams();
100       streams.source = new KeywordTokenizer(reader);
101       streams.result = new ICUCollationKeyFilter(streams.source, collator);
102       setPreviousTokenStream(streams);
103     } else {
104       streams.source.reset(reader);
105     }
106     return streams.result;
107   }
108 }