2020import java .util .List ;
2121
2222import static org .elasticsearch .test .hamcrest .ElasticsearchAssertions .assertAcked ;
23+ import static org .elasticsearch .xpack .esql .EsqlTestUtils .getValuesList ;
2324import static org .hamcrest .CoreMatchers .containsString ;
25+ import static org .hamcrest .Matchers .closeTo ;
2426import static org .hamcrest .Matchers .equalTo ;
2527import static org .hamcrest .Matchers .greaterThan ;
2628import static org .hamcrest .Matchers .lessThan ;
@@ -275,7 +277,7 @@ public void testDisjunctionScoring() {
275277 try (var resp = run (query )) {
276278 assertColumnNames (resp .columns (), List .of ("id" , "_score" ));
277279 assertColumnTypes (resp .columns (), List .of ("integer" , "double" ));
278- List <List <Object >> values = EsqlTestUtils . getValuesList (resp );
280+ List <List <Object >> values = getValuesList (resp );
279281 assertThat (values .size (), equalTo (3 ));
280282
281283 assertThat (values .get (0 ).get (0 ), equalTo (1 ));
@@ -303,7 +305,7 @@ public void testDisjunctionScoringMultipleNonPushableFunctions() {
303305 try (var resp = run (query )) {
304306 assertColumnNames (resp .columns (), List .of ("id" , "_score" ));
305307 assertColumnTypes (resp .columns (), List .of ("integer" , "double" ));
306- List <List <Object >> values = EsqlTestUtils . getValuesList (resp );
308+ List <List <Object >> values = getValuesList (resp );
307309 assertThat (values .size (), equalTo (2 ));
308310
309311 assertThat (values .get (0 ).get (0 ), equalTo (1 ));
@@ -318,20 +320,54 @@ public void testDisjunctionScoringMultipleNonPushableFunctions() {
318320 }
319321 }
320322
323+ public void testScoresAreSimilarToQstr () {
324+ var queryMatch = """
325+ FROM test METADATA _score
326+ | WHERE match(content, "fox") OR (length(content) < 25 AND id > 3)
327+ | KEEP id, _score
328+ | SORT _score DESC
329+ """ ;
330+
331+ var queryQstr = """
332+ FROM test METADATA _score
333+ | WHERE qstr("content:fox OR (length < 25 AND id > 3)")
334+ | KEEP id, _score
335+ | SORT _score DESC
336+ """ ;
337+
338+ try (var respMatch = run (queryMatch ); var respQstr = run (queryQstr )) {
339+ assertEquals (respMatch .columns (), respQstr .columns ());
340+ var matchValues = getValuesList (respMatch );
341+ var qstrValues = getValuesList (respQstr );
342+ assertEquals (matchValues .size (), qstrValues .size ());
343+ for (int i = 0 ; i < matchValues .size (); i ++) {
344+ // Compare ids
345+ assertEquals (matchValues .get (i ).get (0 ), qstrValues .get (i ).get (0 ));
346+ // Compare scores
347+ assertThat ((Double )matchValues .get (i ).get (1 ), closeTo ((Double ) qstrValues .get (i ).get (1 ), 0.01 ));
348+ }
349+ }
350+ }
351+
321352 private void createAndPopulateIndex () {
322353 var indexName = "test" ;
323354 var client = client ().admin ().indices ();
324355 var CreateRequest = client .prepareCreate (indexName )
325356 .setSettings (Settings .builder ().put ("index.number_of_shards" , 1 ))
326- .setMapping ("id" , "type=integer" , "content" , "type=text" );
357+ .setMapping ("id" , "type=integer" , "content" , "type=text" , "length" , "type=integer" );
327358 assertAcked (CreateRequest );
328359 client ().prepareBulk ()
329- .add (new IndexRequest (indexName ).id ("1" ).source ("id" , 1 , "content" , "This is a brown fox" ))
330- .add (new IndexRequest (indexName ).id ("2" ).source ("id" , 2 , "content" , "This is a brown dog" ))
331- .add (new IndexRequest (indexName ).id ("3" ).source ("id" , 3 , "content" , "This dog is really brown" ))
332- .add (new IndexRequest (indexName ).id ("4" ).source ("id" , 4 , "content" , "The dog is brown but this document is very very long" ))
333- .add (new IndexRequest (indexName ).id ("5" ).source ("id" , 5 , "content" , "There is also a white cat" ))
334- .add (new IndexRequest (indexName ).id ("6" ).source ("id" , 6 , "content" , "The quick brown fox jumps over the lazy dog" ))
360+ .add (new IndexRequest (indexName ).id ("1" ).source ("id" , 1 , "content" , "This is a brown fox" , "length" , 19 ))
361+ .add (new IndexRequest (indexName ).id ("2" ).source ("id" , 2 , "content" , "This is a brown dog" , "length" , 19 ))
362+ .add (new IndexRequest (indexName ).id ("3" ).source ("id" , 3 , "content" , "This dog is really brown" , "length" , 25 ))
363+ .add (
364+ new IndexRequest (indexName ).id ("4" )
365+ .source ("id" , 4 , "content" , "The dog is brown but this document is very very long" , "length" , 52 )
366+ )
367+ .add (new IndexRequest (indexName ).id ("5" ).source ("id" , 5 , "content" , "There is also a white cat" , "length" , 25 ))
368+ .add (
369+ new IndexRequest (indexName ).id ("6" ).source ("id" , 6 , "content" , "The quick brown fox jumps over the lazy dog" , "length" , 43 )
370+ )
335371 .setRefreshPolicy (WriteRequest .RefreshPolicy .IMMEDIATE )
336372 .get ();
337373 ensureYellow (indexName );
0 commit comments