Skip to content

Correct rewritting of script_score query #48425

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 2 commits into from
Oct 23, 2019
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 @@ -183,7 +183,9 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
QueryBuilder newQuery = this.query.rewrite(queryRewriteContext);
if (newQuery != query) {
ScriptScoreQueryBuilder newQueryBuilder = new ScriptScoreQueryBuilder(newQuery, script);
newQueryBuilder.setMinScore(minScore);
if (minScore != null) {
newQueryBuilder.setMinScore(minScore);
}
return newQueryBuilder;
}
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@
package org.elasticsearch.search.query;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.fielddata.ScriptDocValues;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.plugins.Plugin;
import org.elasticsearch.script.MockScriptPlugin;
import org.elasticsearch.script.Script;
Expand Down Expand Up @@ -101,4 +103,26 @@ public void testScriptScore() {
assertNoFailures(resp);
assertOrderedSearchHits(resp, "10", "8", "6");
}

// test that when the internal query is rewritten script_score works well
public void testRewrittenQuery() {
assertAcked(
prepareCreate("test-index2")
.setSettings(Settings.builder().put("index.number_of_shards", 1))
.addMapping("_doc", "field1", "type=date", "field2", "type=double")
);
client().prepareIndex("test-index2", "_doc", "1").setSource("field1", "2019-09-01", "field2", 1).get();
client().prepareIndex("test-index2", "_doc", "2").setSource("field1", "2019-10-01", "field2", 2).get();
client().prepareIndex("test-index2", "_doc", "3").setSource("field1", "2019-11-01", "field2", 3).get();
refresh();

RangeQueryBuilder rangeQB = new RangeQueryBuilder("field1").from("2019-01-01"); // the query should be rewritten to from:null
Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['field2'].value * param1", Map.of("param1", 0.1));
SearchResponse resp = client()
.prepareSearch("test-index2")
.setQuery(scriptScoreQuery(rangeQB, script))
.get();
assertNoFailures(resp);
assertOrderedSearchHits(resp, "3", "2", "1");
}
}