From 5913e742864637e8aa6fb7e80babf9e409ed3c89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Luk=C3=A1=C5=A1=20Vl=C4=8Dek?= Date: Tue, 21 Jun 2011 14:57:04 +0200 Subject: [PATCH] Analysis: expose Lucene KeywordMarkerFilter. Closes #1052 --- .../index/analysis/AnalysisModule.java | 2 + .../KeywordMarkerTokenFilterFactory.java | 55 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/KeywordMarkerTokenFilterFactory.java diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/AnalysisModule.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/AnalysisModule.java index 121ac0414eb..61a712d9508 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/AnalysisModule.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/AnalysisModule.java @@ -384,6 +384,8 @@ public class AnalysisModule extends AbstractModule { tokenFiltersBindings.processTokenFilter("french_stem", FrenchStemTokenFilterFactory.class); tokenFiltersBindings.processTokenFilter("german_stem", GermanStemTokenFilterFactory.class); tokenFiltersBindings.processTokenFilter("russian_stem", RussianStemTokenFilterFactory.class); + + tokenFiltersBindings.processTokenFilter("keyword_marker", KeywordMarkerTokenFilterFactory.class); } @Override public void processTokenizers(TokenizersBindings tokenizersBindings) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/KeywordMarkerTokenFilterFactory.java b/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/KeywordMarkerTokenFilterFactory.java new file mode 100644 index 00000000000..996fb3af7e3 --- /dev/null +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/index/analysis/KeywordMarkerTokenFilterFactory.java @@ -0,0 +1,55 @@ +/* + * Licensed to Elastic Search and Shay Banon under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. Elastic Search licenses this + * file to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.elasticsearch.index.analysis; + +import org.apache.lucene.analysis.CharArraySet; +import org.apache.lucene.analysis.KeywordMarkerFilter; +import org.apache.lucene.analysis.TokenStream; +import org.elasticsearch.ElasticSearchIllegalArgumentException; +import org.elasticsearch.common.inject.Inject; +import org.elasticsearch.common.inject.assistedinject.Assisted; +import org.elasticsearch.common.settings.Settings; +import org.elasticsearch.env.Environment; +import org.elasticsearch.index.Index; +import org.elasticsearch.index.settings.IndexSettings; +import org.apache.lucene.util.Version; + +import java.util.Set; + +@AnalysisSettingsRequired +public class KeywordMarkerTokenFilterFactory extends AbstractTokenFilterFactory { + + private final CharArraySet keywordLookup; + + @Inject public KeywordMarkerTokenFilterFactory(Index index, @IndexSettings Settings indexSettings, Environment env, @Assisted String name, @Assisted Settings settings) { + super(index, indexSettings, name, settings); + + boolean ignoreCase = settings.getAsBoolean("ignore_case", false); + Set rules = Analysis.getWordSet(env, settings, "keywords"); + if (rules == null) { + throw new ElasticSearchIllegalArgumentException("keyword filter requires either `keywords` or `keywords_path` to be configured"); + } + keywordLookup = new CharArraySet(Version.LUCENE_32, rules, ignoreCase); + } + + @Override public TokenStream create(TokenStream tokenStream) { + return new KeywordMarkerFilter(tokenStream, keywordLookup); + } +}