diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SourceGenerator.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SourceGenerator.java index f4f98a4e4ff..bac4fdcfb2a 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SourceGenerator.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/execution/search/SourceGenerator.java @@ -5,8 +5,6 @@ */ package org.elasticsearch.xpack.sql.execution.search; -import org.elasticsearch.index.query.BoolQueryBuilder; -import org.elasticsearch.index.query.ConstantScoreQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.AggregationBuilder; import org.elasticsearch.search.aggregations.bucket.composite.CompositeAggregationBuilder; @@ -28,6 +26,7 @@ import org.elasticsearch.xpack.sql.querydsl.container.Sort; import java.util.List; import static java.util.Collections.singletonList; +import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.search.sort.SortBuilders.fieldSort; import static org.elasticsearch.search.sort.SortBuilders.scoreSort; import static org.elasticsearch.search.sort.SortBuilders.scriptSort; @@ -37,20 +36,23 @@ public abstract class SourceGenerator { private static final List NO_STORED_FIELD = singletonList(StoredFieldsContext._NONE_); public static SearchSourceBuilder sourceBuilder(QueryContainer container, QueryBuilder filter, Integer size) { - final SearchSourceBuilder source = new SearchSourceBuilder(); + QueryBuilder finalQuery = null; // add the source - if (container.query() == null) { + if (container.query() != null) { if (filter != null) { - source.query(new ConstantScoreQueryBuilder(filter)); + finalQuery = boolQuery().must(container.query().asBuilder()).filter(filter); + } else { + finalQuery = container.query().asBuilder(); } } else { if (filter != null) { - source.query(new BoolQueryBuilder().must(container.query().asBuilder()).filter(filter)); - } else { - source.query(container.query().asBuilder()); + finalQuery = boolQuery().filter(filter); } } + final SearchSourceBuilder source = new SearchSourceBuilder(); + source.query(finalQuery); + SqlSourceBuilder sortBuilder = new SqlSourceBuilder(); // Iterate through all the columns requested, collecting the fields that // need to be retrieved from the result documents diff --git a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/query/BoolQuery.java b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/query/BoolQuery.java index 64949fe318c..47075773b01 100644 --- a/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/query/BoolQuery.java +++ b/x-pack/plugin/sql/src/main/java/org/elasticsearch/xpack/sql/querydsl/query/BoolQuery.java @@ -5,13 +5,13 @@ */ package org.elasticsearch.xpack.sql.querydsl.query; -import java.util.Objects; - import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.sort.NestedSortBuilder; import org.elasticsearch.xpack.sql.tree.Location; +import java.util.Objects; + import static org.elasticsearch.index.query.QueryBuilders.boolQuery; /** @@ -63,9 +63,8 @@ public class BoolQuery extends Query { public QueryBuilder asBuilder() { BoolQueryBuilder boolQuery = boolQuery(); if (isAnd) { - // TODO are we throwing out score by using filter? - boolQuery.filter(left.asBuilder()); - boolQuery.filter(right.asBuilder()); + boolQuery.must(left.asBuilder()); + boolQuery.must(right.asBuilder()); } else { boolQuery.should(left.asBuilder()); boolQuery.should(right.asBuilder()); diff --git a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SourceGeneratorTests.java b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SourceGeneratorTests.java index f038a20823d..816b6651335 100644 --- a/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SourceGeneratorTests.java +++ b/x-pack/plugin/sql/src/test/java/org/elasticsearch/xpack/sql/execution/search/SourceGeneratorTests.java @@ -5,9 +5,6 @@ */ package org.elasticsearch.xpack.sql.execution.search; -import org.elasticsearch.index.query.BoolQueryBuilder; -import org.elasticsearch.index.query.ConstantScoreQueryBuilder; -import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.AggregatorFactories.Builder; @@ -28,6 +25,8 @@ import org.elasticsearch.xpack.sql.tree.Location; import org.elasticsearch.xpack.sql.type.KeywordEsField; import static java.util.Collections.singletonList; +import static org.elasticsearch.index.query.QueryBuilders.boolQuery; +import static org.elasticsearch.index.query.QueryBuilders.matchQuery; import static org.elasticsearch.search.sort.SortBuilders.fieldSort; import static org.elasticsearch.search.sort.SortBuilders.scoreSort; @@ -42,22 +41,22 @@ public class SourceGeneratorTests extends ESTestCase { public void testQueryNoFilter() { QueryContainer container = new QueryContainer().with(new MatchQuery(Location.EMPTY, "foo", "bar")); SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder(container, null, randomIntBetween(1, 10)); - assertEquals(new MatchQueryBuilder("foo", "bar").operator(Operator.OR), sourceBuilder.query()); + assertEquals(matchQuery("foo", "bar").operator(Operator.OR), sourceBuilder.query()); } public void testNoQueryFilter() { QueryContainer container = new QueryContainer(); - QueryBuilder filter = new MatchQueryBuilder("bar", "baz"); + QueryBuilder filter = matchQuery("bar", "baz"); SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder(container, filter, randomIntBetween(1, 10)); - assertEquals(new ConstantScoreQueryBuilder(new MatchQueryBuilder("bar", "baz")), sourceBuilder.query()); + assertEquals(boolQuery().filter(matchQuery("bar", "baz")), sourceBuilder.query()); } public void testQueryFilter() { QueryContainer container = new QueryContainer().with(new MatchQuery(Location.EMPTY, "foo", "bar")); - QueryBuilder filter = new MatchQueryBuilder("bar", "baz"); + QueryBuilder filter = matchQuery("bar", "baz"); SearchSourceBuilder sourceBuilder = SourceGenerator.sourceBuilder(container, filter, randomIntBetween(1, 10)); - assertEquals(new BoolQueryBuilder().must(new MatchQueryBuilder("foo", "bar").operator(Operator.OR)) - .filter(new MatchQueryBuilder("bar", "baz")), sourceBuilder.query()); + assertEquals(boolQuery().must(matchQuery("foo", "bar").operator(Operator.OR)).filter(matchQuery("bar", "baz")), + sourceBuilder.query()); } public void testLimit() { diff --git a/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java b/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java index 3019a00351c..e0cf0efac47 100644 --- a/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java +++ b/x-pack/qa/sql/src/main/java/org/elasticsearch/xpack/qa/sql/rest/RestSqlTestCase.java @@ -16,7 +16,6 @@ import org.elasticsearch.client.Request; import org.elasticsearch.client.Response; import org.elasticsearch.client.ResponseException; import org.elasticsearch.common.CheckedSupplier; -import org.elasticsearch.common.Strings; import org.elasticsearch.common.collect.Tuple; import org.elasticsearch.common.io.Streams; import org.elasticsearch.common.xcontent.XContentHelper; @@ -33,12 +32,11 @@ import java.nio.charset.StandardCharsets; import java.sql.JDBCType; import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Locale; import java.util.Map; -import java.util.TreeMap; import static java.util.Collections.emptyList; -import static java.util.Collections.emptyMap; import static java.util.Collections.singletonList; import static java.util.Collections.singletonMap; import static java.util.Collections.unmodifiableMap; @@ -396,19 +394,23 @@ public abstract class RestSqlTestCase extends ESRestTestCase implements ErrorsTe assertNotNull(query); @SuppressWarnings("unchecked") - Map constantScore = (Map) query.get("constant_score"); - assertNotNull(constantScore); + Map bool = (Map) query.get("bool"); + assertNotNull(bool); @SuppressWarnings("unchecked") - Map filter = (Map) constantScore.get("filter"); + List filter = (List) bool.get("filter"); assertNotNull(filter); @SuppressWarnings("unchecked") - Map match = (Map) filter.get("match"); - assertNotNull(match); + Map map = (Map) filter.get(0); + assertNotNull(map); @SuppressWarnings("unchecked") - Map matchQuery = (Map) match.get("test"); + Map matchQ = (Map) map.get("match"); + + @SuppressWarnings("unchecked") + Map matchQuery = (Map) matchQ.get("test"); + assertNotNull(matchQuery); assertEquals("foo", matchQuery.get("query")); }