Skip to content

Commit

Permalink
Correct rewritting of script_score query (#48425)
Browse files Browse the repository at this point in the history
Previously there was a bug when an query inside script_score query
was rewritten. If min_score was not set and was equal to null,
we were converting it to float value which resulted to NPE.
This commit corrects this.

Closes #48081
  • Loading branch information
mayya-sharipova authored Oct 23, 2019
1 parent 31fc615 commit 2d8dcf8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
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");
}
}

0 comments on commit 2d8dcf8

Please sign in to comment.