|
| 1 | +package org.elasticsearch.index.analysis; |
| 2 | +/* |
| 3 | + * Licensed to ElasticSearch and Shay Banon under one |
| 4 | + * or more contributor license agreements. See the NOTICE file |
| 5 | + * distributed with this work for additional information |
| 6 | + * regarding copyright ownership. ElasticSearch licenses this |
| 7 | + * file to you under the Apache License, Version 2.0 (the |
| 8 | + * "License"); you may not use this file except in compliance |
| 9 | + * with 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, |
| 14 | + * software distributed under the License is distributed on an |
| 15 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 16 | + * KIND, either express or implied. See the License for the |
| 17 | + * specific language governing permissions and limitations |
| 18 | + * under the License. |
| 19 | + */ |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Map; |
| 22 | + |
| 23 | +import org.apache.lucene.analysis.TokenStream; |
| 24 | +import org.apache.lucene.analysis.miscellaneous.KeepWordFilter; |
| 25 | +import org.apache.lucene.analysis.util.CharArraySet; |
| 26 | +import org.elasticsearch.ElasticSearchIllegalArgumentException; |
| 27 | +import org.elasticsearch.common.inject.Inject; |
| 28 | +import org.elasticsearch.common.inject.assistedinject.Assisted; |
| 29 | +import org.elasticsearch.common.settings.Settings; |
| 30 | +import org.elasticsearch.env.Environment; |
| 31 | +import org.elasticsearch.index.Index; |
| 32 | +import org.elasticsearch.index.settings.IndexSettings; |
| 33 | +import org.elasticsearch.indices.analysis.IndicesAnalysisService; |
| 34 | + |
| 35 | +/** |
| 36 | + * A {@link TokenFilterFactory} for {@link KeepWordFilter}. This filter only |
| 37 | + * keep tokens that are contained in the term set configured via |
| 38 | + * {@value #KEEP_WORDS_KEY} setting. This filter acts like an inverse stop |
| 39 | + * filter. |
| 40 | + * |
| 41 | + * Configuration options: |
| 42 | + * |
| 43 | + * <ul> |
| 44 | + * <li>{@value #KEEP_WORDS_KEY} the array of words / tokens to keep.</li> |
| 45 | + * |
| 46 | + * <li>{@value #KEEP_WORDS_PATH_KEY} an reference to a file containing the words |
| 47 | + * / tokens to keep. Note: this is an alternative to {@value #KEEP_WORDS_KEY} if |
| 48 | + * both are set an exception will be thrown.</li> |
| 49 | + * |
| 50 | + * <li>{@value #ENABLE_POS_INC_KEY} <code>true</code> iff the filter should |
| 51 | + * maintain position increments for dropped tokens. The default is |
| 52 | + * <code>true</code>.</li> |
| 53 | + * |
| 54 | + * <li>{@value #KEEP_WORDS_CASE_KEY} to use case sensitive keep words. The |
| 55 | + * default is <code>false</code> which corresponds to case-sensitive.</li> |
| 56 | + * </ul> |
| 57 | + * |
| 58 | + * @see StopTokenFilterFactory |
| 59 | + * |
| 60 | + */ |
| 61 | +@AnalysisSettingsRequired |
| 62 | +public class KeepWordFilterFactory extends AbstractTokenFilterFactory { |
| 63 | + private Boolean enablePositionIncrements; |
| 64 | + private CharArraySet keepWords; |
| 65 | + private static final String KEEP_WORDS_KEY = "keep_words"; |
| 66 | + private static final String KEEP_WORDS_PATH_KEY = KEEP_WORDS_KEY + "_path"; |
| 67 | + private static final String KEEP_WORDS_CASE_KEY = KEEP_WORDS_KEY + "_case"; // for javadoc |
| 68 | + private static final String ENABLE_POS_INC_KEY = "enable_position_increments"; |
| 69 | + |
| 70 | + @Inject |
| 71 | + public KeepWordFilterFactory(Index index, @IndexSettings Settings indexSettings, |
| 72 | + Environment env, IndicesAnalysisService indicesAnalysisService, Map<String, TokenizerFactoryFactory> tokenizerFactories, |
| 73 | + @Assisted String name, @Assisted Settings settings) { |
| 74 | + super(index, indexSettings, name, settings); |
| 75 | + |
| 76 | + final String[] arrayKeepWords = settings.getAsArray(KEEP_WORDS_KEY); |
| 77 | + final String keepWordsPath = settings.get(KEEP_WORDS_PATH_KEY, null); |
| 78 | + if (!(arrayKeepWords == null ^ keepWordsPath == null)) { |
| 79 | + // we don't allow both or non |
| 80 | + throw new ElasticSearchIllegalArgumentException("keep requires either `" + KEEP_WORDS_KEY + "` or `" |
| 81 | + + KEEP_WORDS_PATH_KEY + "` to be configured"); |
| 82 | + } |
| 83 | + this.enablePositionIncrements = settings.getAsBoolean(ENABLE_POS_INC_KEY, true); |
| 84 | + this.keepWords = Analysis.getWordSet(env, settings, KEEP_WORDS_KEY, version); |
| 85 | + |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public TokenStream create(TokenStream tokenStream) { |
| 90 | + return new KeepWordFilter(enablePositionIncrements, tokenStream, keepWords); |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | + |
| 95 | + |
| 96 | + |
| 97 | +} |
0 commit comments