Skip to content

Commit 8c0ec05

Browse files
authored
Expose lucene's RemoveDuplicatesTokenFilter (#31275)
1 parent 3378240 commit 8c0ec05

File tree

5 files changed

+112
-1
lines changed

5 files changed

+112
-1
lines changed

docs/reference/analysis/tokenfilters.asciidoc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,6 @@ include::tokenfilters/decimal-digit-tokenfilter.asciidoc[]
9595

9696
include::tokenfilters/fingerprint-tokenfilter.asciidoc[]
9797

98-
include::tokenfilters/minhash-tokenfilter.asciidoc[]
98+
include::tokenfilters/minhash-tokenfilter.asciidoc[]
99+
100+
include::tokenfilters/remove-duplicates-tokenfilter.asciidoc[]
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[[analysis-remove-duplicates-tokenfilter]]
2+
=== Remove Duplicates Token Filter
3+
4+
A token filter of type `remove_duplicates` that drops identical tokens at the
5+
same position.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ public Map<String, AnalysisProvider<TokenFilterFactory>> getTokenFilters() {
194194
filters.put("pattern_replace", requriesAnalysisSettings(PatternReplaceTokenFilterFactory::new));
195195
filters.put("persian_normalization", PersianNormalizationFilterFactory::new);
196196
filters.put("porter_stem", PorterStemTokenFilterFactory::new);
197+
filters.put("remove_duplicates", RemoveDuplicatesTokenFilterFactory::new);
197198
filters.put("reverse", ReverseTokenFilterFactory::new);
198199
filters.put("russian_stem", RussianStemTokenFilterFactory::new);
199200
filters.put("scandinavian_folding", ScandinavianFoldingFilterFactory::new);
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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.apache.lucene.analysis.TokenStream;
23+
import org.apache.lucene.analysis.miscellaneous.RemoveDuplicatesTokenFilter;
24+
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.env.Environment;
26+
import org.elasticsearch.index.IndexSettings;
27+
import org.elasticsearch.index.analysis.AbstractTokenFilterFactory;
28+
29+
/**
30+
* Filter factory for the lucene RemoveDuplicatesTokenFilter
31+
*/
32+
class RemoveDuplicatesTokenFilterFactory extends AbstractTokenFilterFactory {
33+
34+
RemoveDuplicatesTokenFilterFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) {
35+
super(indexSettings, name, settings);
36+
}
37+
38+
@Override
39+
public TokenStream create(TokenStream tokenStream) {
40+
return new RemoveDuplicatesTokenFilter(tokenStream);
41+
}
42+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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.apache.lucene.analysis.CannedTokenStream;
23+
import org.apache.lucene.analysis.Token;
24+
import org.elasticsearch.common.settings.Settings;
25+
import org.elasticsearch.env.Environment;
26+
import org.elasticsearch.index.analysis.AnalysisTestsHelper;
27+
import org.elasticsearch.index.analysis.TokenFilterFactory;
28+
import org.elasticsearch.test.ESTestCase;
29+
import org.elasticsearch.test.ESTokenStreamTestCase;
30+
31+
import java.io.IOException;
32+
33+
import static org.hamcrest.Matchers.instanceOf;
34+
35+
public class RemoveDuplicatesFilterFactoryTests extends ESTokenStreamTestCase {
36+
37+
public void testRemoveDuplicatesFilter() throws IOException {
38+
Settings settings = Settings.builder()
39+
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
40+
.put("index.analysis.filter.removedups.type", "remove_duplicates")
41+
.build();
42+
ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin());
43+
TokenFilterFactory tokenFilter = analysis.tokenFilter.get("removedups");
44+
assertThat(tokenFilter, instanceOf(RemoveDuplicatesTokenFilterFactory.class));
45+
46+
CannedTokenStream cts = new CannedTokenStream(
47+
new Token("a", 1, 0, 1),
48+
new Token("b", 1, 2, 3),
49+
new Token("c", 0, 2, 3),
50+
new Token("b", 0, 2, 3),
51+
new Token("d", 1, 4, 5)
52+
);
53+
54+
assertTokenStreamContents(tokenFilter.create(cts), new String[]{
55+
"a", "b", "c", "d"
56+
}, new int[]{
57+
1, 1, 0, 1
58+
});
59+
}
60+
61+
}

0 commit comments

Comments
 (0)