Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/136195.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 136195
summary: Further simplify `SingleValueMatchQuery`
area: ES|QL
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand All @@ -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)) {
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -70,7 +70,7 @@ public static List<Object[]> params() {

private final Setup setup;

public SingleValueMathQueryTests(Setup setup) {
public SingleValueMatchQueryTests(Setup setup) {
this.setup = setup;
}

Expand Down