Skip to content

Commit 2d8dcf8

Browse files
Correct rewritting of script_score query (#48425)
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
1 parent 31fc615 commit 2d8dcf8

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

server/src/main/java/org/elasticsearch/index/query/functionscore/ScriptScoreQueryBuilder.java

+3-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws
183183
QueryBuilder newQuery = this.query.rewrite(queryRewriteContext);
184184
if (newQuery != query) {
185185
ScriptScoreQueryBuilder newQueryBuilder = new ScriptScoreQueryBuilder(newQuery, script);
186-
newQueryBuilder.setMinScore(minScore);
186+
if (minScore != null) {
187+
newQueryBuilder.setMinScore(minScore);
188+
}
187189
return newQueryBuilder;
188190
}
189191
return this;

server/src/test/java/org/elasticsearch/search/query/ScriptScoreQueryIT.java

+24
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
package org.elasticsearch.search.query;
2121

2222
import org.elasticsearch.action.search.SearchResponse;
23+
import org.elasticsearch.common.settings.Settings;
2324
import org.elasticsearch.index.fielddata.ScriptDocValues;
25+
import org.elasticsearch.index.query.RangeQueryBuilder;
2426
import org.elasticsearch.plugins.Plugin;
2527
import org.elasticsearch.script.MockScriptPlugin;
2628
import org.elasticsearch.script.Script;
@@ -101,4 +103,26 @@ public void testScriptScore() {
101103
assertNoFailures(resp);
102104
assertOrderedSearchHits(resp, "10", "8", "6");
103105
}
106+
107+
// test that when the internal query is rewritten script_score works well
108+
public void testRewrittenQuery() {
109+
assertAcked(
110+
prepareCreate("test-index2")
111+
.setSettings(Settings.builder().put("index.number_of_shards", 1))
112+
.addMapping("_doc", "field1", "type=date", "field2", "type=double")
113+
);
114+
client().prepareIndex("test-index2", "_doc", "1").setSource("field1", "2019-09-01", "field2", 1).get();
115+
client().prepareIndex("test-index2", "_doc", "2").setSource("field1", "2019-10-01", "field2", 2).get();
116+
client().prepareIndex("test-index2", "_doc", "3").setSource("field1", "2019-11-01", "field2", 3).get();
117+
refresh();
118+
119+
RangeQueryBuilder rangeQB = new RangeQueryBuilder("field1").from("2019-01-01"); // the query should be rewritten to from:null
120+
Script script = new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "doc['field2'].value * param1", Map.of("param1", 0.1));
121+
SearchResponse resp = client()
122+
.prepareSearch("test-index2")
123+
.setQuery(scriptScoreQuery(rangeQB, script))
124+
.get();
125+
assertNoFailures(resp);
126+
assertOrderedSearchHits(resp, "3", "2", "1");
127+
}
104128
}

0 commit comments

Comments
 (0)