Skip to content

Commit 2d31399

Browse files
committed
Make non full text functions score evaluate as 0.0
1 parent 65e16b7 commit 2d31399

File tree

2 files changed

+13
-65
lines changed

2 files changed

+13
-65
lines changed

x-pack/plugin/esql/src/internalClusterTest/java/org/elasticsearch/xpack/esql/plugin/MatchFunctionIT.java

Lines changed: 10 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
2222
import static org.elasticsearch.xpack.esql.EsqlTestUtils.getValuesList;
2323
import static org.hamcrest.CoreMatchers.containsString;
24-
import static org.hamcrest.Matchers.closeTo;
2524
import static org.hamcrest.Matchers.equalTo;
2625
import static org.hamcrest.Matchers.greaterThan;
2726
import static org.hamcrest.Matchers.lessThan;
@@ -270,7 +269,7 @@ public void testDisjunctionScoring() {
270269
FROM test METADATA _score
271270
| WHERE match(content, "fox") OR length(content) < 20
272271
| KEEP id, _score
273-
| SORT _score DESC
272+
| SORT _score DESC, id ASC
274273
""";
275274

276275
try (var resp = run(query)) {
@@ -280,16 +279,15 @@ public void testDisjunctionScoring() {
280279
assertThat(values.size(), equalTo(3));
281280

282281
assertThat(values.get(0).get(0), equalTo(1));
283-
assertThat(values.get(1).get(0), equalTo(2));
284-
assertThat(values.get(2).get(0), equalTo(6));
282+
assertThat(values.get(1).get(0), equalTo(6));
283+
assertThat(values.get(2).get(0), equalTo(2));
285284

286285
// Matches full text query and non pushable query
287-
assertThat((Double) values.get(0).get(1), greaterThan(2.0));
286+
assertThat((Double) values.get(0).get(1), greaterThan(0.0));
288287
// Matches just non pushable query
289-
assertThat((Double) values.get(1).get(1), equalTo(1.0));
288+
assertThat((Double) values.get(1).get(1), greaterThan(0.0));
290289
// Matches just full text query
291-
assertThat((Double) values.get(2).get(1), lessThan(1.0));
292-
assertThat((Double) values.get(2).get(1), greaterThan(0.0));
290+
assertThat((Double) values.get(2).get(1), equalTo(0.0));
293291
}
294292
}
295293

@@ -319,24 +317,6 @@ public void testDisjunctionScoringMultipleNonPushableFunctions() {
319317
}
320318
}
321319

322-
public void testScoresAreSimilarToPushable() {
323-
var nonPushableQuery = """
324-
FROM test METADATA _score
325-
| WHERE match(content, "fox") OR (length(content) < 40 AND id > 3)
326-
| KEEP id, _score
327-
| SORT _score DESC
328-
""";
329-
330-
var pushableQuery = """
331-
FROM test METADATA _score
332-
| WHERE match(content, "fox") OR (length < 40 AND id > 3)
333-
| KEEP id, _score
334-
| SORT _score DESC
335-
""";
336-
337-
compareQueryResultsAndScores(nonPushableQuery, pushableQuery);
338-
}
339-
340320
public void testDisjunctionScoringWithNot() {
341321
var query = """
342322
FROM test METADATA _score
@@ -351,51 +331,17 @@ public void testDisjunctionScoringWithNot() {
351331
List<List<Object>> values = getValuesList(resp);
352332
assertThat(values.size(), equalTo(3));
353333

354-
assertThat(values.get(0).get(0), equalTo(4));
355-
assertThat(values.get(1).get(0), equalTo(1));
334+
assertThat(values.get(0).get(0), equalTo(1));
335+
assertThat(values.get(1).get(0), equalTo(4));
356336
assertThat(values.get(2).get(0), equalTo(5));
357337

358-
// Matches NOT and non pushable query
359-
assertThat((Double) values.get(0).get(1), equalTo(1.0));
360-
// Matches just NOT
338+
// Matches NOT and non pushable query gets score of 0.0
339+
assertThat((Double) values.get(0).get(1), equalTo(0.0));
361340
assertThat((Double) values.get(1).get(1), equalTo(0.0));
362341
assertThat((Double) values.get(2).get(1), equalTo(0.0));
363342
}
364343
}
365344

366-
public void testScoresAreSimilarToPushableUsingNot() {
367-
var nonPushableQuery = """
368-
FROM test METADATA _score
369-
| WHERE NOT(match(content, "fox")) OR (length(content) < 25 AND id > 3)
370-
| KEEP id, _score
371-
| SORT _score DESC, id ASC
372-
""";
373-
374-
var pushableQuery = """
375-
FROM test METADATA _score
376-
| WHERE NOT(match(content, "fox")) OR (length < 25 AND id > 3)
377-
| KEEP id, _score
378-
| SORT _score DESC, id ASC
379-
""";
380-
381-
compareQueryResultsAndScores(nonPushableQuery, pushableQuery);
382-
}
383-
384-
private void compareQueryResultsAndScores(String queryMatch, String queryQstr) {
385-
try (var respMatch = run(queryMatch); var respQstr = run(queryQstr)) {
386-
assertEquals(respMatch.columns(), respQstr.columns());
387-
var matchValues = getValuesList(respMatch);
388-
var qstrValues = getValuesList(respQstr);
389-
assertEquals(matchValues.size(), qstrValues.size());
390-
for (int i = 0; i < matchValues.size(); i++) {
391-
// Compare ids
392-
assertEquals(matchValues.get(i).get(0), qstrValues.get(i).get(0));
393-
// Compare scores
394-
assertThat((Double) matchValues.get(i).get(1), closeTo((Double) qstrValues.get(i).get(1), 0.0001));
395-
}
396-
}
397-
}
398-
399345
private void createAndPopulateIndex() {
400346
var indexName = "test";
401347
var client = client().admin().indices();

x-pack/plugin/esql/src/main/java/org/elasticsearch/xpack/esql/evaluator/mapper/BooleanToScoringExpressionEvaluator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
*/
2525
public class BooleanToScoringExpressionEvaluator implements EvalOperator.ExpressionEvaluator {
2626

27+
public static final double SCORE_FOR_TRUE = 0.0;
28+
2729
private final EvalOperator.ExpressionEvaluator inner;
2830
private final DriverContext driverContext;
2931

@@ -59,7 +61,7 @@ private DoubleBlock eval(BooleanBlock booleanBlock) {
5961
for (int i = 0; i < positionCount; i++) {
6062
boolean value = booleanBlock.getBoolean(i);
6163
if (value) {
62-
builder.appendDouble(1.0);
64+
builder.appendDouble(SCORE_FOR_TRUE);
6365
} else {
6466
builder.appendDouble(SCORE_FOR_FALSE);
6567
}

0 commit comments

Comments
 (0)