Skip to content

Commit

Permalink
Reset Token position on reuse in scripted analysis (#47424)
Browse files Browse the repository at this point in the history
Most of the information in AnalysisPredicateScript.Token is pulled directly
from its underlying AttributeSource, but we also keep track of the token position,
and this state is held directly on the Token. This information needs to be reset when
the containing ScriptFilteringTokenFilter or ScriptedConditionTokenFilter is re-used.

Fixes #47197
  • Loading branch information
romseygeek committed Oct 2, 2019
1 parent 8117f8a commit f840112
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 9 deletions.
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 {
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

0 comments on commit f840112

Please sign in to comment.