diff --git a/docs/changelog/136195.yaml b/docs/changelog/136195.yaml new file mode 100644 index 0000000000000..3a54bd77ed12e --- /dev/null +++ b/docs/changelog/136195.yaml @@ -0,0 +1,5 @@ +pr: 136195 +summary: Further simplify `SingleValueMatchQuery` +area: ES|QL +type: enhancement +issues: [] diff --git a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/querydsl/query/SingleValueMatchQuery.java b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/querydsl/query/SingleValueMatchQuery.java index de587f7fadc69..97eda529ca9e1 100644 --- a/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/querydsl/query/SingleValueMatchQuery.java +++ b/x-pack/plugin/esql/compute/src/main/java/org/elasticsearch/compute/querydsl/query/SingleValueMatchQuery.java @@ -35,6 +35,7 @@ import org.elasticsearch.index.fielddata.LeafOrdinalsFieldData; import org.elasticsearch.index.fielddata.SortedBinaryDocValues; import org.elasticsearch.index.fielddata.SortedNumericLongValues; +import org.elasticsearch.index.fielddata.plain.ConstantIndexFieldData; import java.io.IOException; import java.util.Objects; @@ -132,15 +133,10 @@ private ScorerSupplier scorerSupplier( ScoreMode scoreMode ) throws IOException { final int maxDoc = context.reader().maxDoc(); - if (SortedNumericLongValues.unwrapSingleton(sortedNumerics) != null) { - // check for dense field - // TODO: check doc values skippers - final PointValues points = context.reader().getPointValues(fieldData.getFieldName()); - if (points != null && points.getDocCount() == maxDoc) { - return new DocIdSetIteratorScorerSupplier(boost, scoreMode, DocIdSetIterator.all(maxDoc)); - } else { - return new PredicateScorerSupplier(boost, scoreMode, maxDoc, MULTI_VALUE_MATCH_COST, sortedNumerics::advanceExact); - } + NumericDocValues ndv = DocValues.unwrapSingleton(DocValues.getSortedNumeric(context.reader(), fieldData.getFieldName())); + if (ndv != null && ndv.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) { + ndv = DocValues.unwrapSingleton(DocValues.getSortedNumeric(context.reader(), fieldData.getFieldName())); + return new DocIdSetIteratorScorerSupplier(boost, scoreMode, ndv); } final CheckedIntPredicate predicate = doc -> { if (false == sortedNumerics.advanceExact(doc)) { @@ -162,21 +158,10 @@ private ScorerSupplier scorerSupplier( ScoreMode scoreMode ) throws IOException { final int maxDoc = context.reader().maxDoc(); - if (DocValues.unwrapSingleton(sortedSetDocValues) != null) { - // check for dense field - // TODO: check doc values skippers - final Terms terms = context.reader().terms(fieldData.getFieldName()); - if (terms != null && terms.getDocCount() == maxDoc) { - return new DocIdSetIteratorScorerSupplier(boost, scoreMode, DocIdSetIterator.all(maxDoc)); - } else { - return new PredicateScorerSupplier( - boost, - scoreMode, - maxDoc, - MULTI_VALUE_MATCH_COST, - sortedSetDocValues::advanceExact - ); - } + SortedDocValues sdv = DocValues.unwrapSingleton(DocValues.getSortedSet(context.reader(), fieldData.getFieldName())); + if (sdv != null && sdv.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) { + sdv = DocValues.unwrapSingleton(DocValues.getSortedSet(context.reader(), fieldData.getFieldName())); + return new DocIdSetIteratorScorerSupplier(boost, scoreMode, sdv); } final CheckedIntPredicate predicate = doc -> { if (false == sortedSetDocValues.advanceExact(doc)) { @@ -224,6 +209,9 @@ private ScorerSupplier scorerSupplier( @Override public Query rewrite(IndexSearcher indexSearcher) throws IOException { + if (fieldData instanceof ConstantIndexFieldData cfd && cfd.getValue() != null) { + return new MatchAllDocsQuery(); + } for (LeafReaderContext context : indexSearcher.getIndexReader().leaves()) { final LeafReader reader = context.reader(); final int maxDoc = reader.maxDoc(); diff --git a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/querydsl/query/SingleValueMathQueryTests.java b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/querydsl/query/SingleValueMatchQueryTests.java similarity index 98% rename from x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/querydsl/query/SingleValueMathQueryTests.java rename to x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/querydsl/query/SingleValueMatchQueryTests.java index 12933d557659a..69a9d123d7f3e 100644 --- a/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/querydsl/query/SingleValueMathQueryTests.java +++ b/x-pack/plugin/esql/src/test/java/org/elasticsearch/xpack/esql/querydsl/query/SingleValueMatchQueryTests.java @@ -43,7 +43,7 @@ import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.sameInstance; -public class SingleValueMathQueryTests extends MapperServiceTestCase { +public class SingleValueMatchQueryTests extends MapperServiceTestCase { interface Setup { XContentBuilder mapping(XContentBuilder builder) throws IOException; @@ -70,7 +70,7 @@ public static List params() { private final Setup setup; - public SingleValueMathQueryTests(Setup setup) { + public SingleValueMatchQueryTests(Setup setup) { this.setup = setup; }