Skip to content

Commit b94cd56

Browse files
authored
[8.19] Use IndexOrDocValuesQuery in NumberFieldType#termQuery implementations (#128293) (#128394)
* Use IndexOrDocValuesQuery in NumberFieldType#termQuery implementations (#128293) * Update x-pack/plugin/esql/qa/server/single-node/src/javaRestTest/java/org/elasticsearch/xpack/esql/qa/single_node/PushQueriesIT.java
1 parent 911fa96 commit b94cd56

File tree

10 files changed

+219
-81
lines changed

10 files changed

+219
-81
lines changed

docs/changelog/128293.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
pr: 128293
2+
summary: Use `IndexOrDocValuesQuery` in `NumberFieldType#termQuery` implementations
3+
area: Search
4+
type: enhancement
5+
issues: []

modules/mapper-extras/src/main/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldMapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -323,7 +323,7 @@ public boolean isSearchable() {
323323
public Query termQuery(Object value, SearchExecutionContext context) {
324324
failIfNotIndexedNorDocValuesFallback(context);
325325
long scaledValue = Math.round(scale(value));
326-
return NumberFieldMapper.NumberType.LONG.termQuery(name(), scaledValue, isIndexed());
326+
return NumberFieldMapper.NumberType.LONG.termQuery(name(), scaledValue, isIndexed(), hasDocValues());
327327
}
328328

329329
@Override

modules/mapper-extras/src/test/java/org/elasticsearch/index/mapper/extras/ScaledFloatFieldTypeTests.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
import org.apache.lucene.document.Document;
1313
import org.apache.lucene.document.DoublePoint;
14+
import org.apache.lucene.document.LongField;
1415
import org.apache.lucene.document.LongPoint;
1516
import org.apache.lucene.document.SortedNumericDocValuesField;
1617
import org.apache.lucene.index.DirectoryReader;
@@ -47,7 +48,7 @@ public void testTermQuery() {
4748
);
4849
double value = (randomDouble() * 2 - 1) * 10000;
4950
long scaledValue = Math.round(value * ft.getScalingFactor());
50-
assertEquals(LongPoint.newExactQuery("scaled_float", scaledValue), ft.termQuery(value, MOCK_CONTEXT));
51+
assertEquals(LongField.newExactQuery("scaled_float", scaledValue), ft.termQuery(value, MOCK_CONTEXT));
5152

5253
MappedFieldType ft2 = new ScaledFloatFieldMapper.ScaledFloatFieldType("scaled_float", 0.1 + randomDouble() * 100, false);
5354
ElasticsearchException e2 = expectThrows(ElasticsearchException.class, () -> ft2.termQuery("42", MOCK_CONTEXT_DISALLOW_EXPENSIVE));

server/src/main/java/org/elasticsearch/index/mapper/NumberFieldMapper.java

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,19 @@ public Float parse(XContentParser parser, boolean coerce) throws IOException {
352352
}
353353

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

361361
if (isIndexed) {
362+
if (hasDocValues) {
363+
return new IndexOrDocValuesQuery(
364+
HalfFloatPoint.newExactQuery(field, v),
365+
SortedNumericDocValuesField.newSlowExactQuery(field, HalfFloatPoint.halfFloatToSortableShort(v))
366+
);
367+
}
362368
return HalfFloatPoint.newExactQuery(field, v);
363369
} else {
364370
return SortedNumericDocValuesField.newSlowExactQuery(field, HalfFloatPoint.halfFloatToSortableShort(v));
@@ -542,13 +548,15 @@ public Float parse(XContentParser parser, boolean coerce) throws IOException {
542548
}
543549

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

551-
if (isIndexed) {
557+
if (isIndexed && hasDocValues) {
558+
return FloatField.newExactQuery(field, v);
559+
} else if (isIndexed) {
552560
return FloatPoint.newExactQuery(field, v);
553561
} else {
554562
return SortedNumericDocValuesField.newSlowExactQuery(field, NumericUtils.floatToSortableInt(v));
@@ -715,13 +723,15 @@ public FieldValues<Number> compile(String fieldName, Script script, ScriptCompil
715723
}
716724

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

724-
if (isIndexed) {
732+
if (isIndexed && hasDocValues) {
733+
return DoubleField.newExactQuery(field, v);
734+
} else if (isIndexed) {
725735
return DoublePoint.newExactQuery(field, v);
726736
} else {
727737
return SortedNumericDocValuesField.newSlowExactQuery(field, NumericUtils.doubleToSortableLong(v));
@@ -875,12 +885,12 @@ public Byte parse(XContentParser parser, boolean coerce) throws IOException {
875885
}
876886

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

883-
return INTEGER.termQuery(field, value, isIndexed);
893+
return INTEGER.termQuery(field, value, isIndexed, hasDocValues);
884894
}
885895

886896
@Override
@@ -999,11 +1009,11 @@ public Short parse(XContentParser parser, boolean coerce) throws IOException {
9991009
}
10001010

10011011
@Override
1002-
public Query termQuery(String field, Object value, boolean isIndexed) {
1012+
public Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues) {
10031013
if (isOutOfRange(value)) {
10041014
return Queries.newMatchNoDocsQuery("Value [" + value + "] is out of range");
10051015
}
1006-
return INTEGER.termQuery(field, value, isIndexed);
1016+
return INTEGER.termQuery(field, value, isIndexed, hasDocValues);
10071017
}
10081018

10091019
@Override
@@ -1125,7 +1135,7 @@ public Integer parse(XContentParser parser, boolean coerce) throws IOException {
11251135
}
11261136

11271137
@Override
1128-
public Query termQuery(String field, Object value, boolean isIndexed) {
1138+
public Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues) {
11291139
if (hasDecimalPart(value)) {
11301140
return Queries.newMatchNoDocsQuery("Value [" + value + "] has a decimal part");
11311141
}
@@ -1136,7 +1146,9 @@ public Query termQuery(String field, Object value, boolean isIndexed) {
11361146
}
11371147
int v = parse(value, true);
11381148

1139-
if (isIndexed) {
1149+
if (isIndexed && hasDocValues) {
1150+
return IntField.newExactQuery(field, v);
1151+
} else if (isIndexed) {
11401152
return IntPoint.newExactQuery(field, v);
11411153
} else {
11421154
return SortedNumericDocValuesField.newSlowExactQuery(field, v);
@@ -1309,7 +1321,7 @@ public FieldValues<Number> compile(String fieldName, Script script, ScriptCompil
13091321
}
13101322

13111323
@Override
1312-
public Query termQuery(String field, Object value, boolean isIndexed) {
1324+
public Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues) {
13131325
if (hasDecimalPart(value)) {
13141326
return Queries.newMatchNoDocsQuery("Value [" + value + "] has a decimal part");
13151327
}
@@ -1318,7 +1330,9 @@ public Query termQuery(String field, Object value, boolean isIndexed) {
13181330
}
13191331

13201332
long v = parse(value, true);
1321-
if (isIndexed) {
1333+
if (isIndexed && hasDocValues) {
1334+
return LongField.newExactQuery(field, v);
1335+
} else if (isIndexed) {
13221336
return LongPoint.newExactQuery(field, v);
13231337
} else {
13241338
return SortedNumericDocValuesField.newSlowExactQuery(field, v);
@@ -1502,7 +1516,7 @@ public final TypeParser parser() {
15021516
return parser;
15031517
}
15041518

1505-
public abstract Query termQuery(String field, Object value, boolean isIndexed);
1519+
public abstract Query termQuery(String field, Object value, boolean isIndexed, boolean hasDocValues);
15061520

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

@@ -1893,11 +1907,11 @@ public NumberFieldType(
18931907
}
18941908

18951909
public NumberFieldType(String name, NumberType type) {
1896-
this(name, type, true);
1910+
this(name, type, true, true);
18971911
}
18981912

1899-
public NumberFieldType(String name, NumberType type, boolean isIndexed) {
1900-
this(name, type, isIndexed, false, true, true, null, Collections.emptyMap(), null, false, null, null, false);
1913+
public NumberFieldType(String name, NumberType type, boolean isIndexed, boolean hasDocValues) {
1914+
this(name, type, isIndexed, false, hasDocValues, true, null, Collections.emptyMap(), null, false, null, null, false);
19011915
}
19021916

19031917
@Override
@@ -1936,7 +1950,7 @@ public boolean isSearchable() {
19361950
@Override
19371951
public Query termQuery(Object value, SearchExecutionContext context) {
19381952
failIfNotIndexedNorDocValuesFallback(context);
1939-
return type.termQuery(name(), value, isIndexed());
1953+
return type.termQuery(name(), value, isIndexed(), hasDocValues());
19401954
}
19411955

19421956
@Override

0 commit comments

Comments
 (0)