Skip to content

Reset Token position on reuse in predicate_token_filter #47424

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

Merged
Merged
Show file tree
Hide file tree
Changes from all 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 @@ -61,6 +61,10 @@ public Token(AttributeSource source) {
this.keywordAtt = source.addAttribute(KeywordAttribute.class);
}

public void reset() {
this.pos = -1;
}

public void updatePosition() {
this.pos = this.pos + posIncAtt.getPositionIncrement();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,11 @@ protected boolean accept() throws IOException {
token.updatePosition();
return script.execute(token);
}

@Override
public void reset() throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think ScriptedConditionTokenFilterFactory has the same issue ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

++, will update and add a test

super.reset();
this.token.reset();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.elasticsearch.script.ScriptService;
import org.elasticsearch.script.ScriptType;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
Expand Down Expand Up @@ -119,6 +120,12 @@ protected boolean shouldFilter() {
token.updatePosition();
return script.execute(token);
}

@Override
public void reset() throws IOException {
super.reset();
token.reset();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void testSimpleFilter() throws IOException {
Settings indexSettings = Settings.builder()
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
.put("index.analysis.filter.f.type", "predicate_token_filter")
.put("index.analysis.filter.f.script.source", "token.getTerm().length() > 5")
.put("index.analysis.filter.f.script.source", "my_script")
.put("index.analysis.analyzer.myAnalyzer.type", "custom")
.put("index.analysis.analyzer.myAnalyzer.tokenizer", "standard")
.putList("index.analysis.analyzer.myAnalyzer.filter", "f")
Expand All @@ -56,7 +56,7 @@ public void testSimpleFilter() throws IOException {
AnalysisPredicateScript.Factory factory = () -> new AnalysisPredicateScript() {
@Override
public boolean execute(Token token) {
return token.getTerm().length() > 5;
return token.getPosition() < 2 || token.getPosition() > 4;
}
};

Expand All @@ -65,7 +65,7 @@ public boolean execute(Token token) {
@Override
public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
assertEquals(context, AnalysisPredicateScript.CONTEXT);
assertEquals(new Script("token.getTerm().length() > 5"), script);
assertEquals(new Script("my_script"), script);
return (FactoryType) factory;
}
};
Expand All @@ -79,8 +79,8 @@ public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryTyp

try (NamedAnalyzer analyzer = analyzers.get("myAnalyzer")) {
assertNotNull(analyzer);
assertAnalyzesTo(analyzer, "Vorsprung Durch Technik", new String[]{
"Vorsprung", "Technik"
assertAnalyzesTo(analyzer, "Oh what a wonderful thing to be", new String[]{
"Oh", "what", "to", "be"
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void testSimpleCondition() throws Exception {
Settings indexSettings = Settings.builder()
.put(IndexMetaData.SETTING_VERSION_CREATED, Version.CURRENT)
.put("index.analysis.filter.cond.type", "condition")
.put("index.analysis.filter.cond.script.source", "token.getTerm().length() > 5")
.put("index.analysis.filter.cond.script.source", "token.getPosition() > 1")
.putList("index.analysis.filter.cond.filter", "uppercase")
.put("index.analysis.analyzer.myAnalyzer.type", "custom")
.put("index.analysis.analyzer.myAnalyzer.tokenizer", "standard")
Expand All @@ -56,7 +56,7 @@ public void testSimpleCondition() throws Exception {
AnalysisPredicateScript.Factory factory = () -> new AnalysisPredicateScript() {
@Override
public boolean execute(Token token) {
return token.getTerm().length() > 5;
return token.getPosition() > 1;
}
};

Expand All @@ -65,7 +65,7 @@ public boolean execute(Token token) {
@Override
public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryType> context) {
assertEquals(context, AnalysisPredicateScript.CONTEXT);
assertEquals(new Script("token.getTerm().length() > 5"), script);
assertEquals(new Script("token.getPosition() > 1"), script);
return (FactoryType) factory;
}
};
Expand All @@ -80,7 +80,7 @@ public <FactoryType> FactoryType compile(Script script, ScriptContext<FactoryTyp
try (NamedAnalyzer analyzer = analyzers.get("myAnalyzer")) {
assertNotNull(analyzer);
assertAnalyzesTo(analyzer, "Vorsprung Durch Technik", new String[]{
"VORSPRUNG", "Durch", "TECHNIK"
"Vorsprung", "Durch", "TECHNIK"
});
}

Expand Down