Skip to content

[8.19] Use IndexOrDocValuesQuery in NumberFieldType#termQuery implementations (#128293) #128394

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 23, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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/128293.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 128293
summary: Use `IndexOrDocValuesQuery` in `NumberFieldType#termQuery` implementations
area: Search
type: enhancement
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ public boolean isSearchable() {
public Query termQuery(Object value, SearchExecutionContext context) {
failIfNotIndexedNorDocValuesFallback(context);
long scaledValue = Math.round(scale(value));
return NumberFieldMapper.NumberType.LONG.termQuery(name(), scaledValue, isIndexed());
return NumberFieldMapper.NumberType.LONG.termQuery(name(), scaledValue, isIndexed(), hasDocValues());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.apache.lucene.document.Document;
import org.apache.lucene.document.DoublePoint;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.LongPoint;
import org.apache.lucene.document.SortedNumericDocValuesField;
import org.apache.lucene.index.DirectoryReader;
Expand Down Expand Up @@ -47,7 +48,7 @@ public void testTermQuery() {
);
double value = (randomDouble() * 2 - 1) * 10000;
long scaledValue = Math.round(value * ft.getScalingFactor());
assertEquals(LongPoint.newExactQuery("scaled_float", scaledValue), ft.termQuery(value, MOCK_CONTEXT));
assertEquals(LongField.newExactQuery("scaled_float", scaledValue), ft.termQuery(value, MOCK_CONTEXT));

MappedFieldType ft2 = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 0.1 + randomDouble() * 100, false);
ElasticsearchException e2 = expectThrows(ElasticsearchException.class, () -> ft2.termQuery("42", MOCK_CONTEXT_DISALLOW_EXPENSIVE));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,19 @@ public Float parse(XContentParser parser, boolean coerce) throws IOException {
}

@Override
public Query termQuery(String field, Object value, boolean isIndexed) {
public Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues) {
float v = parseToFloat(value);
if (Float.isFinite(HalfFloatPoint.sortableShortToHalfFloat(HalfFloatPoint.halfFloatToSortableShort(v))) == false) {
return Queries.newMatchNoDocsQuery("Value [" + value + "] is out of range");
}

if (isIndexed) {
if (hasDocValues) {
return new IndexOrDocValuesQuery(
HalfFloatPoint.newExactQuery(field, v),
SortedNumericDocValuesField.newSlowExactQuery(field, HalfFloatPoint.halfFloatToSortableShort(v))
);
}
return HalfFloatPoint.newExactQuery(field, v);
} else {
return SortedNumericDocValuesField.newSlowExactQuery(field, HalfFloatPoint.halfFloatToSortableShort(v));
Expand Down Expand Up @@ -542,13 +548,15 @@ public Float parse(XContentParser parser, boolean coerce) throws IOException {
}

@Override
public Query termQuery(String field, Object value, boolean isIndexed) {
public Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues) {
float v = parseToFloat(value);
if (Float.isFinite(v) == false) {
return new MatchNoDocsQuery("Value [" + value + "] is out of range");
}

if (isIndexed) {
if (isIndexed && hasDocValues) {
return FloatField.newExactQuery(field, v);
} else if (isIndexed) {
return FloatPoint.newExactQuery(field, v);
} else {
return SortedNumericDocValuesField.newSlowExactQuery(field, NumericUtils.floatToSortableInt(v));
Expand Down Expand Up @@ -715,13 +723,15 @@ public FieldValues<Number> compile(String fieldName, Script script, ScriptCompil
}

@Override
public Query termQuery(String field, Object value, boolean isIndexed) {
public Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues) {
double v = objectToDouble(value);
if (Double.isFinite(v) == false) {
return Queries.newMatchNoDocsQuery("Value [" + value + "] has a decimal part");
}

if (isIndexed) {
if (isIndexed && hasDocValues) {
return DoubleField.newExactQuery(field, v);
} else if (isIndexed) {
return DoublePoint.newExactQuery(field, v);
} else {
return SortedNumericDocValuesField.newSlowExactQuery(field, NumericUtils.doubleToSortableLong(v));
Expand Down Expand Up @@ -875,12 +885,12 @@ public Byte parse(XContentParser parser, boolean coerce) throws IOException {
}

@Override
public Query termQuery(String field, Object value, boolean isIndexed) {
public Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues) {
if (isOutOfRange(value)) {
return new MatchNoDocsQuery("Value [" + value + "] is out of range");
}

return INTEGER.termQuery(field, value, isIndexed);
return INTEGER.termQuery(field, value, isIndexed, hasDocValues);
}

@Override
Expand Down Expand Up @@ -999,11 +1009,11 @@ public Short parse(XContentParser parser, boolean coerce) throws IOException {
}

@Override
public Query termQuery(String field, Object value, boolean isIndexed) {
public Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues) {
if (isOutOfRange(value)) {
return Queries.newMatchNoDocsQuery("Value [" + value + "] is out of range");
}
return INTEGER.termQuery(field, value, isIndexed);
return INTEGER.termQuery(field, value, isIndexed, hasDocValues);
}

@Override
Expand Down Expand Up @@ -1125,7 +1135,7 @@ public Integer parse(XContentParser parser, boolean coerce) throws IOException {
}

@Override
public Query termQuery(String field, Object value, boolean isIndexed) {
public Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues) {
if (hasDecimalPart(value)) {
return Queries.newMatchNoDocsQuery("Value [" + value + "] has a decimal part");
}
Expand All @@ -1136,7 +1146,9 @@ public Query termQuery(String field, Object value, boolean isIndexed) {
}
int v = parse(value, true);

if (isIndexed) {
if (isIndexed && hasDocValues) {
return IntField.newExactQuery(field, v);
} else if (isIndexed) {
return IntPoint.newExactQuery(field, v);
} else {
return SortedNumericDocValuesField.newSlowExactQuery(field, v);
Expand Down Expand Up @@ -1309,7 +1321,7 @@ public FieldValues<Number> compile(String fieldName, Script script, ScriptCompil
}

@Override
public Query termQuery(String field, Object value, boolean isIndexed) {
public Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues) {
if (hasDecimalPart(value)) {
return Queries.newMatchNoDocsQuery("Value [" + value + "] has a decimal part");
}
Expand All @@ -1318,7 +1330,9 @@ public Query termQuery(String field, Object value, boolean isIndexed) {
}

long v = parse(value, true);
if (isIndexed) {
if (isIndexed && hasDocValues) {
return LongField.newExactQuery(field, v);
} else if (isIndexed) {
return LongPoint.newExactQuery(field, v);
} else {
return SortedNumericDocValuesField.newSlowExactQuery(field, v);
Expand Down Expand Up @@ -1502,7 +1516,7 @@ public final TypeParser parser() {
return parser;
}

public abstract Query termQuery(String field, Object value, boolean isIndexed);
public abstract Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues);

public abstract Query termsQuery(String field, Collection<?> values);

Expand Down Expand Up @@ -1893,11 +1907,11 @@ public NumberFieldType(
}

public NumberFieldType(String name, NumberType type) {
this(name, type, true);
this(name, type, true, true);
}

public NumberFieldType(String name, NumberType type, boolean isIndexed) {
this(name, type, isIndexed, false, true, true, null, Collections.emptyMap(), null, false, null, null, false);
public NumberFieldType(String name, NumberType type, boolean isIndexed, boolean hasDocValues) {
this(name, type, isIndexed, false, hasDocValues, true, null, Collections.emptyMap(), null, false, null, null, false);
}

@Override
Expand Down Expand Up @@ -1936,7 +1950,7 @@ public boolean isSearchable() {
@Override
public Query termQuery(Object value, SearchExecutionContext context) {
failIfNotIndexedNorDocValuesFallback(context);
return type.termQuery(name(), value, isIndexed());
return type.termQuery(name(), value, isIndexed(), hasDocValues());
}

@Override
Expand Down
Loading