|
@@ -11,12 +11,16 @@ import org.elasticsearch.ElasticsearchException;
|
|
|
import org.elasticsearch.action.index.IndexRequest;
|
|
|
import org.elasticsearch.action.support.WriteRequest;
|
|
|
import org.elasticsearch.common.settings.Settings;
|
|
|
+import org.elasticsearch.index.query.QueryBuilder;
|
|
|
+import org.elasticsearch.index.query.QueryBuilders;
|
|
|
import org.elasticsearch.xpack.esql.VerificationException;
|
|
|
import org.elasticsearch.xpack.esql.action.AbstractEsqlIntegTestCase;
|
|
|
import org.junit.Before;
|
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
+import static org.elasticsearch.index.query.QueryBuilders.boolQuery;
|
|
|
+import static org.elasticsearch.index.query.QueryBuilders.matchQuery;
|
|
|
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertAcked;
|
|
|
import static org.hamcrest.CoreMatchers.containsString;
|
|
|
|
|
@@ -120,6 +124,119 @@ public class MatchOperatorIT extends AbstractEsqlIntegTestCase {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Test for https://github.com/elastic/elasticsearch/issues/123967
|
|
|
+ */
|
|
|
+ public void testWhereMatchWithScoring_AndRequestFilter() {
|
|
|
+ var query = """
|
|
|
+ FROM test METADATA _score
|
|
|
+ | WHERE content:"fox"
|
|
|
+ | SORT _score DESC
|
|
|
+ | KEEP content, _score
|
|
|
+ """;
|
|
|
+
|
|
|
+ QueryBuilder filter = boolQuery().must(matchQuery("content", "brown"));
|
|
|
+
|
|
|
+ try (var resp = run(query, randomPragmas(), filter)) {
|
|
|
+ assertColumnNames(resp.columns(), List.of("content", "_score"));
|
|
|
+ assertColumnTypes(resp.columns(), List.of("text", "double"));
|
|
|
+ assertValues(
|
|
|
+ resp.values(),
|
|
|
+ List.of(
|
|
|
+ List.of("This is a brown fox", 1.4274532794952393),
|
|
|
+ List.of("The quick brown fox jumps over the lazy dog", 1.1248724460601807)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testWhereMatchWithScoring_AndNoScoreRequestFilter() {
|
|
|
+ var query = """
|
|
|
+ FROM test METADATA _score
|
|
|
+ | WHERE content:"fox"
|
|
|
+ | SORT _score DESC
|
|
|
+ | KEEP content, _score
|
|
|
+ """;
|
|
|
+
|
|
|
+ QueryBuilder filter = boolQuery().filter(matchQuery("content", "brown"));
|
|
|
+
|
|
|
+ try (var resp = run(query, randomPragmas(), filter)) {
|
|
|
+ assertColumnNames(resp.columns(), List.of("content", "_score"));
|
|
|
+ assertColumnTypes(resp.columns(), List.of("text", "double"));
|
|
|
+ assertValues(
|
|
|
+ resp.values(),
|
|
|
+ List.of(
|
|
|
+ List.of("This is a brown fox", 1.156558871269226),
|
|
|
+ List.of("The quick brown fox jumps over the lazy dog", 0.9114001989364624)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testWhereMatchWithScoring_And_MatchAllRequestFilter() {
|
|
|
+ var query = """
|
|
|
+ FROM test METADATA _score
|
|
|
+ | WHERE content:"fox"
|
|
|
+ | SORT _score DESC
|
|
|
+ | KEEP content, _score
|
|
|
+ """;
|
|
|
+
|
|
|
+ QueryBuilder filter = QueryBuilders.matchAllQuery();
|
|
|
+
|
|
|
+ try (var resp = run(query, randomPragmas(), filter)) {
|
|
|
+ assertColumnNames(resp.columns(), List.of("content", "_score"));
|
|
|
+ assertColumnTypes(resp.columns(), List.of("text", "double"));
|
|
|
+ assertValues(
|
|
|
+ resp.values(),
|
|
|
+ List.of(
|
|
|
+ List.of("This is a brown fox", 2.1565589904785156),
|
|
|
+ List.of("The quick brown fox jumps over the lazy dog", 1.9114001989364624)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testScoringOutsideQuery() {
|
|
|
+ var query = """
|
|
|
+ FROM test METADATA _score
|
|
|
+ | SORT _score DESC
|
|
|
+ | KEEP content, _score
|
|
|
+ """;
|
|
|
+
|
|
|
+ QueryBuilder filter = boolQuery().must(matchQuery("content", "fox"));
|
|
|
+
|
|
|
+ try (var resp = run(query, randomPragmas(), filter)) {
|
|
|
+ assertColumnNames(resp.columns(), List.of("content", "_score"));
|
|
|
+ assertColumnTypes(resp.columns(), List.of("text", "double"));
|
|
|
+ assertValues(
|
|
|
+ resp.values(),
|
|
|
+ List.of(
|
|
|
+ List.of("This is a brown fox", 1.156558871269226),
|
|
|
+ List.of("The quick brown fox jumps over the lazy dog", 0.9114001989364624)
|
|
|
+ )
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void testScoring_Zero_OutsideQuery() {
|
|
|
+ var query = """
|
|
|
+ FROM test METADATA _score
|
|
|
+ | SORT _score DESC
|
|
|
+ | KEEP content, _score
|
|
|
+ """;
|
|
|
+
|
|
|
+ QueryBuilder filter = boolQuery().filter(matchQuery("content", "fox"));
|
|
|
+
|
|
|
+ try (var resp = run(query, randomPragmas(), filter)) {
|
|
|
+ assertColumnNames(resp.columns(), List.of("content", "_score"));
|
|
|
+ assertColumnTypes(resp.columns(), List.of("text", "double"));
|
|
|
+ assertValues(
|
|
|
+ resp.values(),
|
|
|
+ List.of(List.of("This is a brown fox", 0.0), List.of("The quick brown fox jumps over the lazy dog", 0.0))
|
|
|
+ );
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
public void testWhereMatchWithScoringDifferentSort() {
|
|
|
var query = """
|
|
|
FROM test
|