|
| 1 | +/* |
| 2 | + * Licensed to Elasticsearch under one or more contributor |
| 3 | + * license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright |
| 5 | + * ownership. Elasticsearch licenses this file to you under |
| 6 | + * the Apache License, Version 2.0 (the "License"); you may |
| 7 | + * 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, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.elasticsearch.indices.analysis.smartcn; |
| 21 | + |
| 22 | +import org.apache.lucene.analysis.TokenStream; |
| 23 | +import org.apache.lucene.analysis.Tokenizer; |
| 24 | +import org.apache.lucene.analysis.cn.smart.SentenceTokenizer; |
| 25 | +import org.apache.lucene.analysis.cn.smart.SmartChineseAnalyzer; |
| 26 | +import org.apache.lucene.analysis.cn.smart.WordTokenFilter; |
| 27 | +import org.elasticsearch.common.component.AbstractComponent; |
| 28 | +import org.elasticsearch.common.inject.Inject; |
| 29 | +import org.elasticsearch.common.lucene.Lucene; |
| 30 | +import org.elasticsearch.common.settings.Settings; |
| 31 | +import org.elasticsearch.index.analysis.*; |
| 32 | +import org.elasticsearch.indices.analysis.IndicesAnalysisService; |
| 33 | + |
| 34 | +import java.io.Reader; |
| 35 | + |
| 36 | +/** |
| 37 | + * Registers indices level analysis components so, if not explicitly configured, will be shared |
| 38 | + * among all indices. |
| 39 | + */ |
| 40 | +public class SmartChineseIndicesAnalysis extends AbstractComponent { |
| 41 | + |
| 42 | + @Inject |
| 43 | + public SmartChineseIndicesAnalysis(Settings settings, IndicesAnalysisService indicesAnalysisService) { |
| 44 | + super(settings); |
| 45 | + |
| 46 | + // Register smartcn analyzer |
| 47 | + indicesAnalysisService.analyzerProviderFactories().put("smartcn", new PreBuiltAnalyzerProviderFactory("smartcn", AnalyzerScope.INDICES, new SmartChineseAnalyzer(Lucene.ANALYZER_VERSION))); |
| 48 | + |
| 49 | + // Register smartcn_word token filter |
| 50 | + indicesAnalysisService.tokenFilterFactories().put("smartcn_word", new PreBuiltTokenFilterFactoryFactory(new TokenFilterFactory() { |
| 51 | + @Override public String name() { |
| 52 | + return "smartcn_word"; |
| 53 | + } |
| 54 | + |
| 55 | + @Override public TokenStream create(TokenStream tokenStream) { |
| 56 | + return new WordTokenFilter(tokenStream); |
| 57 | + } |
| 58 | + })); |
| 59 | + |
| 60 | + // Register smartcn_sentence tokenizer |
| 61 | + indicesAnalysisService.tokenizerFactories().put("smartcn_sentence", new PreBuiltTokenizerFactoryFactory(new TokenizerFactory() { |
| 62 | + @Override |
| 63 | + public String name() { |
| 64 | + return "smartcn_sentence"; |
| 65 | + } |
| 66 | + |
| 67 | + @Override |
| 68 | + public Tokenizer create(Reader reader) { |
| 69 | + return new SentenceTokenizer(reader); |
| 70 | + } |
| 71 | + })); |
| 72 | + |
| 73 | + |
| 74 | + } |
| 75 | +} |
0 commit comments