|
20 | 20 | package org.elasticsearch.search.query;
|
21 | 21 |
|
22 | 22 | import org.elasticsearch.action.search.SearchResponse;
|
| 23 | +import org.elasticsearch.common.settings.Settings; |
23 | 24 | import org.elasticsearch.index.fielddata.ScriptDocValues;
|
| 25 | +import org.elasticsearch.index.query.RangeQueryBuilder; |
24 | 26 | import org.elasticsearch.plugins.Plugin;
|
25 | 27 | import org.elasticsearch.script.MockScriptPlugin;
|
26 | 28 | import org.elasticsearch.script.Script;
|
@@ -101,4 +103,26 @@ public void testScriptScore() {
|
101 | 103 | assertNoFailures(resp);
|
102 | 104 | assertOrderedSearchHits(resp, "10", "8", "6");
|
103 | 105 | }
|
| 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 | + } |
104 | 128 | }
|
0 commit comments