Skip to content

Commit 05a7333

Browse files
committed
Require [articles] setting in elision filter (#43083)
We should throw an exception at construction time if a list of articles is not provided, otherwise we can get random NPEs during indexing. Relates to #43002
1 parent bed7e68 commit 05a7333

File tree

5 files changed

+64
-3
lines changed

5 files changed

+64
-3
lines changed

docs/reference/analysis/tokenfilters/elision-tokenfilter.asciidoc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
A token filter which removes elisions. For example, "l'avion" (the
55
plane) will tokenized as "avion" (plane).
66

7-
Accepts `articles` parameter which is a set of stop words articles. Also accepts
8-
`articles_case`, which indicates whether the filter treats those articles as
7+
Requires either an `articles` parameter which is a set of stop word articles, or
8+
`articles_path` which points to a text file containing the stop set. Also optionally
9+
accepts `articles_case`, which indicates whether the filter treats those articles as
910
case sensitive.
1011

1112
For example:

modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/CommonAnalysisPlugin.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ public Map<String, AnalysisProvider<TokenFilterFactory>> getTokenFilters() {
239239
filters.put("dutch_stem", DutchStemTokenFilterFactory::new);
240240
filters.put("edge_ngram", EdgeNGramTokenFilterFactory::new);
241241
filters.put("edgeNGram", EdgeNGramTokenFilterFactory::new);
242-
filters.put("elision", ElisionTokenFilterFactory::new);
242+
filters.put("elision", requiresAnalysisSettings(ElisionTokenFilterFactory::new));
243243
filters.put("fingerprint", FingerprintTokenFilterFactory::new);
244244
filters.put("flatten_graph", FlattenGraphTokenFilterFactory::new);
245245
filters.put("french_stem", FrenchStemTokenFilterFactory::new);

modules/analysis-common/src/main/java/org/elasticsearch/analysis/common/ElisionTokenFilterFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ public class ElisionTokenFilterFactory extends AbstractTokenFilterFactory implem
3636
ElisionTokenFilterFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) {
3737
super(indexSettings, name, settings);
3838
this.articles = Analysis.parseArticles(env, settings);
39+
if (this.articles == null) {
40+
throw new IllegalArgumentException("elision filter requires [articles] or [articles_path] setting");
41+
}
3942
}
4043

4144
@Override
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.analysis.common;
21+
22+
import org.elasticsearch.common.settings.Settings;
23+
import org.elasticsearch.env.Environment;
24+
import org.elasticsearch.index.analysis.AnalysisTestsHelper;
25+
import org.elasticsearch.test.ESTokenStreamTestCase;
26+
27+
import java.io.IOException;
28+
29+
public class ElisionFilterFactoryTests extends ESTokenStreamTestCase {
30+
31+
public void testElisionFilterWithNoArticles() throws IOException {
32+
Settings settings = Settings.builder()
33+
.put("index.analysis.filter.elision.type", "elision")
34+
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
35+
.build();
36+
37+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class,
38+
() -> AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin()));
39+
40+
assertEquals("elision filter requires [articles] or [articles_path] setting", e.getMessage());
41+
}
42+
43+
}

modules/analysis-common/src/test/resources/rest-api-spec/test/analysis-common/40_token_filters.yml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,20 @@
587587
- length: { tokens: 1 }
588588
- match: { tokens.0.token: avion }
589589

590+
- do:
591+
catch: bad_request
592+
indices.create:
593+
index: test2
594+
body:
595+
settings:
596+
analysis:
597+
filter:
598+
my_elision:
599+
type: elision
600+
- match: { status: 400 }
601+
- match: { error.type: illegal_argument_exception }
602+
- match: { error.reason: "elision filter requires [articles] or [articles_path] setting" }
603+
590604
---
591605
"stemmer":
592606
- do:

0 commit comments

Comments
 (0)