Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add exclusion option to keep_types token filter #32012

Merged
merged 5 commits into from
Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ contained in a predefined set.
[float]
=== Options
[horizontal]
types:: a list of types to keep

types:: a list of types to include (default mode) or exclude
mode:: if set to `include` (default) the specified token types will be kept,
if set to `exclude` the specified token types will be removed from the stream

[float]
=== Settings example
Expand Down Expand Up @@ -53,7 +54,7 @@ POST /keep_types_example/_analyze
// CONSOLE
// TEST[continued]

And it'd respond:
The response will be:

[source,js]
--------------------------------------------------
Expand All @@ -72,3 +73,70 @@ And it'd respond:
// TESTRESPONSE

Note how only the `<NUM>` token is in the output.

=== Exclude mode settings example

If the `mode` parameter is set to `exclude` like in the following example:

[source,js]
--------------------------------------------------
PUT /keep_types_exclude_example
{
"settings" : {
"analysis" : {
"analyzer" : {
"my_analyzer" : {
"tokenizer" : "standard",
"filter" : ["standard", "lowercase", "remove_numbers"]
}
},
"filter" : {
"remove_numbers" : {
"type" : "keep_types",
"mode" : "exclude",
"types" : [ "<NUM>" ]
}
}
}
}
}
--------------------------------------------------
// CONSOLE

And we test it like:

[source,js]
--------------------------------------------------
POST /keep_types_exclude_example/_analyze
{
"analyzer" : "my_analyzer",
"text" : "hello 101 world"
}
--------------------------------------------------
// CONSOLE
// TEST[continued]

The response will be:

[source,js]
--------------------------------------------------
{
"tokens": [
{
"token": "hello",
"start_offset": 0,
"end_offset": 5,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "world",
"start_offset": 10,
"end_offset": 15,
"type": "<ALPHANUM>",
"position": 2
}
]
}
--------------------------------------------------
// TESTRESPONSE
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;

/**
Expand All @@ -43,7 +44,12 @@
*/
public class KeepTypesFilterFactory extends AbstractTokenFilterFactory {
private final Set<String> keepTypes;
private static final String KEEP_TYPES_KEY = "types";
private final boolean includeMode;
static final String KEEP_TYPES_KEY = "types";
static final String KEEP_TYPES_MODE = "mode";
static final String KEEP_TYPES_MODE_INCLUDE = "include";
static final String KEEP_TYPES_MODE_EXCLUDE = "exclude";


KeepTypesFilterFactory(IndexSettings indexSettings, Environment env, String name, Settings settings) {
super(indexSettings, name, settings);
Expand All @@ -52,12 +58,18 @@ public class KeepTypesFilterFactory extends AbstractTokenFilterFactory {
if ((arrayKeepTypes == null)) {
throw new IllegalArgumentException("keep_types requires `" + KEEP_TYPES_KEY + "` to be configured");
}
final String modeParameter = settings.get(KEEP_TYPES_MODE, KEEP_TYPES_MODE_INCLUDE).toLowerCase(Locale.ROOT);
if (modeParameter.equals(KEEP_TYPES_MODE_INCLUDE) == false && modeParameter.equals(KEEP_TYPES_MODE_EXCLUDE) == false) {
throw new IllegalArgumentException(
"keep_types mode can only be `" + KEEP_TYPES_MODE_INCLUDE + "` or `" + KEEP_TYPES_MODE_INCLUDE + "`");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add the value of modeParameter to the error message?

}

this.keepTypes = new HashSet<>(arrayKeepTypes);
this.includeMode = modeParameter.equals(KEEP_TYPES_MODE_INCLUDE);
}

@Override
public TokenStream create(TokenStream tokenStream) {
return new TypeTokenFilter(tokenStream, keepTypes, true);
return new TypeTokenFilter(tokenStream, keepTypes, includeMode);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,43 @@
import static org.hamcrest.Matchers.instanceOf;

public class KeepTypesFilterFactoryTests extends ESTokenStreamTestCase {
public void testKeepTypes() throws IOException {

private static final String BASE_SETTING = "index.analysis.filter.keep_numbers";

public void testKeepTypesInclude() throws IOException {
Settings.Builder settingsBuilder = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
.put(BASE_SETTING + ".type", "keep_types")
.putList(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_KEY, new String[] { "<NUM>", "<SOMETHINGELSE>" });
// either use default mode or set "include" mode explicitly
if (random().nextBoolean()) {
settingsBuilder.put(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_MODE,
KeepTypesFilterFactory.KEEP_TYPES_MODE_INCLUDE);
}
Settings settings = settingsBuilder.build();
ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin());
TokenFilterFactory tokenFilter = analysis.tokenFilter.get("keep_numbers");
assertThat(tokenFilter, instanceOf(KeepTypesFilterFactory.class));
String source = "Hello 123 world";
String[] expected = new String[] { "123" };
Tokenizer tokenizer = new StandardTokenizer();
tokenizer.setReader(new StringReader(source));
assertTokenStreamContents(tokenFilter.create(tokenizer), expected, new int[] { 2 });
}

public void testKeepTypesExclude() throws IOException {
Settings settings = Settings.builder()
.put(Environment.PATH_HOME_SETTING.getKey(), createTempDir().toString())
.put("index.analysis.filter.keep_numbers.type", "keep_types")
.putList("index.analysis.filter.keep_numbers.types", new String[] {"<NUM>", "<SOMETHINGELSE>"})
.build();
.put(BASE_SETTING + ".type", "keep_types")
.putList(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_KEY, new String[] { "<NUM>", "<SOMETHINGELSE>" })
.put(BASE_SETTING + "." + KeepTypesFilterFactory.KEEP_TYPES_MODE, KeepTypesFilterFactory.KEEP_TYPES_MODE_EXCLUDE).build();
ESTestCase.TestAnalysis analysis = AnalysisTestsHelper.createTestAnalysisFromSettings(settings, new CommonAnalysisPlugin());
TokenFilterFactory tokenFilter = analysis.tokenFilter.get("keep_numbers");
assertThat(tokenFilter, instanceOf(KeepTypesFilterFactory.class));
String source = "Hello 123 world";
String[] expected = new String[]{"123"};
String[] expected = new String[] { "Hello", "world" };
Tokenizer tokenizer = new StandardTokenizer();
tokenizer.setReader(new StringReader(source));
assertTokenStreamContents(tokenFilter.create(tokenizer), expected, new int[]{2});
assertTokenStreamContents(tokenFilter.create(tokenizer), expected, new int[] { 1, 2 });
}
}