Added support for PatternReplaceCharFilter

PatternReplaceCharFilter allows the use of a regex to manipulate the characters in a string before analysis

Closes #3197
This commit is contained in:
Alexander Reelsen 2013-06-24 18:41:39 +02:00
parent 42b3f06a32
commit 7e55354f4a
5 changed files with 91 additions and 0 deletions

View File

@ -417,6 +417,7 @@ public class AnalysisModule extends AbstractModule {
@Override @Override
public void processCharFilters(CharFiltersBindings charFiltersBindings) { public void processCharFilters(CharFiltersBindings charFiltersBindings) {
charFiltersBindings.processCharFilter("html_strip", HtmlStripCharFilterFactory.class); charFiltersBindings.processCharFilter("html_strip", HtmlStripCharFilterFactory.class);
charFiltersBindings.processCharFilter("pattern_replace", PatternReplaceCharFilterFactory.class);
} }
@Override @Override

View File

@ -0,0 +1,66 @@
/*
* Licensed to ElasticSearch and Shay Banon under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch 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.pattern.PatternReplaceCharFilter;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.assistedinject.Assisted;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.Index;
import org.elasticsearch.index.settings.IndexSettings;
import java.io.Reader;
import java.util.regex.Pattern;
@AnalysisSettingsRequired
public class PatternReplaceCharFilterFactory extends AbstractCharFilterFactory {
private final Pattern pattern;
private final String replacement;
@Inject
public PatternReplaceCharFilterFactory(Index index, @IndexSettings Settings indexSettings, @Assisted String name, @Assisted Settings settings) {
super(index, indexSettings, name);
if (!Strings.hasLength(settings.get("pattern"))) {
throw new ElasticSearchIllegalArgumentException("pattern is missing for [" + name + "] char filter of type 'pattern_replace'");
}
pattern = Pattern.compile(settings.get("pattern"));
replacement = settings.get("replacement");
if (!Strings.hasLength(replacement)) {
throw new ElasticSearchIllegalArgumentException("replacement is missing for [" + name + "] char filter of type 'pattern_replace'");
}
}
public Pattern getPattern() {
return pattern;
}
public String getReplacement() {
return replacement;
}
@Override
public Reader create(Reader tokenStream) {
return new PatternReplaceCharFilter(pattern, replacement, tokenStream);
}
}

View File

@ -136,6 +136,14 @@ public class AnalysisModuleTests {
analyzer = analysisService.analyzer("alias1").analyzer(); analyzer = analysisService.analyzer("alias1").analyzer();
assertThat(analyzer, instanceOf(StandardAnalyzer.class)); assertThat(analyzer, instanceOf(StandardAnalyzer.class));
// check custom pattern replace filter
analyzer = analysisService.analyzer("custom3").analyzer();
assertThat(analyzer, instanceOf(CustomAnalyzer.class));
CustomAnalyzer custom3 = (CustomAnalyzer) analyzer;
PatternReplaceCharFilterFactory patternReplaceCharFilterFactory = (PatternReplaceCharFilterFactory) custom3.charFilters()[0];
assertThat(patternReplaceCharFilterFactory.getPattern().pattern(), equalTo("sample(.*)"));
assertThat(patternReplaceCharFilterFactory.getReplacement(), equalTo("replacedSample $1"));
// check custom class name (my) // check custom class name (my)
analyzer = analysisService.analyzer("custom4").analyzer(); analyzer = analysisService.analyzer("custom4").analyzer();
assertThat(analyzer, instanceOf(CustomAnalyzer.class)); assertThat(analyzer, instanceOf(CustomAnalyzer.class));

View File

@ -12,6 +12,11 @@
"escaped_tags":["xxx", "yyy"], "escaped_tags":["xxx", "yyy"],
"read_ahead":1024 "read_ahead":1024
}, },
"my_pattern":{
"type":"pattern_replace",
"pattern":"sample(.*)",
"replacement":"replacedSample $1"
},
"my_mapping":{ "my_mapping":{
"type":"mapping", "type":"mapping",
"mappings":["ph=>f", "qu=>q"] "mappings":["ph=>f", "qu=>q"]
@ -49,6 +54,10 @@
"tokenizer":"standard", "tokenizer":"standard",
"char_filter":["html_strip", "my_html"] "char_filter":["html_strip", "my_html"]
}, },
"custom3":{
"tokenizer":"standard",
"char_filter":["my_pattern"]
},
"custom4":{ "custom4":{
"tokenizer":"standard", "tokenizer":"standard",
"filter":["my"] "filter":["my"]

View File

@ -8,6 +8,10 @@ index :
type : html_strip type : html_strip
escaped_tags : [xxx, yyy] escaped_tags : [xxx, yyy]
read_ahead : 1024 read_ahead : 1024
my_pattern :
type: pattern_replace
pattern: sample(.*)
replacement: replacedSample $1
my_mapping : my_mapping :
type : mapping type : mapping
mappings : [ph=>f, qu=>q] mappings : [ph=>f, qu=>q]
@ -35,6 +39,9 @@ index :
custom2 : custom2 :
tokenizer : standard tokenizer : standard
char_filter : [html_strip, my_html] char_filter : [html_strip, my_html]
custom3 :
tokenizer : standard
char_filter : [my_pattern]
custom4 : custom4 :
tokenizer : standard tokenizer : standard
filter : [my] filter : [my]