1 package org.apache.lucene.collation;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
27 import org.apache.lucene.collation.CollationKeyAnalyzer; // javadocs
29 import java.io.Reader;
30 import java.io.IOException;
35 * Filters {@link KeywordTokenizer} with {@link ICUCollationKeyFilter}.
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.
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):
53 * Collator version - see {@link Collator#getVersion()}
56 * The collation strength used - see {@link Collator#setStrength(int)}
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.
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.
74 public final class ICUCollationKeyAnalyzer extends Analyzer {
75 private Collator collator;
77 public ICUCollationKeyAnalyzer(Collator collator) {
78 this.collator = collator;
82 public TokenStream tokenStream(String fieldName, Reader reader) {
83 TokenStream result = new KeywordTokenizer(reader);
84 result = new ICUCollationKeyFilter(result, collator);
88 private class SavedStreams {
94 public TokenStream reusableTokenStream(String fieldName, Reader reader)
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);
104 streams.source.reset(reader);
106 return streams.result;