Throw ESIA if required field 'patterns' is not present in filter config.

If 'pattern_capture' tokenfilter is create / mapped without a 'patterns'
settings we now throw an exception since this is a misconfiguration and
likely due to the similar settings on related token filters.

Closes #3808
This commit is contained in:
Simon Willnauer 2013-09-30 20:25:32 +02:00
parent 9cb55138c7
commit 20423a43e7
2 changed files with 18 additions and 11 deletions

View File

@ -19,12 +19,10 @@
package org.elasticsearch.index.analysis;
import org.apache.lucene.analysis.pattern.PatternCaptureGroupTokenFilter;
import org.apache.lucene.analysis.TokenFilter;
import org.apache.lucene.analysis.TokenStream;
import org.elasticsearch.common.Strings;
import org.apache.lucene.analysis.pattern.PatternCaptureGroupTokenFilter;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.inject.assistedinject.Assisted;
import org.elasticsearch.common.settings.Settings;
@ -35,22 +33,25 @@ import java.util.regex.Pattern;
@AnalysisSettingsRequired
public class PatternCaptureGroupTokenFilterFactory extends AbstractTokenFilterFactory {
private Pattern[] patterns;
private boolean preserveOriginal;
private final Pattern[] patterns;
private final boolean preserveOriginal;
private static final String PATTERNS_KEY = "patterns";
private static final String PRESERVE_ORIG_KEY = "preserve_original";
@Inject
public PatternCaptureGroupTokenFilterFactory(Index index, @IndexSettings Settings indexSettings, @Assisted String name,
@Assisted Settings settings) {
super(index, indexSettings, name, settings);
String[] regexes = settings.getAsArray("patterns",Strings.EMPTY_ARRAY,false);
String[] regexes = settings.getAsArray(PATTERNS_KEY, null, false);
if (regexes == null) {
throw new ElasticSearchIllegalArgumentException("required setting '" + PATTERNS_KEY + "' is missing for token filter [" + name + "]");
}
patterns = new Pattern[regexes.length];
for (int i = 0; i < regexes.length; i++) {
patterns[i] = Pattern.compile(regexes[i]);
}
preserveOriginal = settings.getAsBoolean("preserve_original", true);
preserveOriginal = settings.getAsBoolean(PRESERVE_ORIG_KEY, true);
}
@Override

View File

@ -19,6 +19,7 @@
package org.elasticsearch.index.analysis;
import org.elasticsearch.ElasticSearchIllegalArgumentException;
import org.elasticsearch.common.inject.Injector;
import org.elasticsearch.common.inject.ModulesBuilder;
import org.elasticsearch.common.settings.Settings;
@ -61,7 +62,12 @@ public class PatternCaptureTokenFilterTests extends ElasticSearchTokenStreamTest
NamedAnalyzer analyzer3 = analysisService.analyzer("preserve");
assertTokenStreamContents(analyzer3.tokenStream("test", "foobarbaz"), new String[]{"foobar","foo"});
}
@Test(expected=ElasticSearchIllegalArgumentException.class)
public void testNoPatterns() {
new PatternCaptureGroupTokenFilterFactory(new Index("test"), settingsBuilder().build(), "pattern_capture", settingsBuilder().put("pattern", "foobar").build());
}
}