Skip to content
Merged
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 @@ -351,13 +351,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 @@ -541,13 +547,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 @@ -714,13 +722,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 @@ -874,12 +884,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 @@ -998,11 +1008,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 @@ -1124,7 +1134,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 @@ -1135,7 +1145,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 @@ -1308,7 +1320,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 @@ -1317,7 +1329,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 @@ -1500,7 +1514,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 @@ -1891,11 +1905,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 @@ -1934,7 +1948,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
Loading