Skip to content

Commit e7444f7

Browse files
authored
Fix scaled_float numeric type in aggregations (#22351)
`scaled_float` should be used as DOUBLE in aggregations but currently they are used as LONG. This change fixes this issue and adds a simple it test for it. Fixes #22350
1 parent 3cb164b commit e7444f7

File tree

3 files changed

+54
-1
lines changed

3 files changed

+54
-1
lines changed

core/src/main/java/org/elasticsearch/index/mapper/ScaledFloatFieldMapper.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,10 @@ public Index index() {
504504

505505
@Override
506506
public NumericType getNumericType() {
507-
return scaledFieldData.getNumericType();
507+
/**
508+
* {@link ScaledFloatLeafFieldData#getDoubleValues()} transforms the raw long values in `scaled` floats.
509+
*/
510+
return NumericType.DOUBLE;
508511
}
509512

510513
}

core/src/test/java/org/elasticsearch/index/mapper/ScaledFloatFieldTypeTests.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@ public void testFieldData() throws IOException {
182182
// single-valued
183183
ft.setName("scaled_float1");
184184
IndexNumericFieldData fielddata = (IndexNumericFieldData) ft.fielddataBuilder().build(indexSettings, ft, null, null, null);
185+
assertEquals(fielddata.getNumericType(), IndexNumericFieldData.NumericType.DOUBLE);
185186
AtomicNumericFieldData leafFieldData = fielddata.load(reader.leaves().get(0));
186187
SortedNumericDoubleValues values = leafFieldData.getDoubleValues();
187188
values.setDocument(0);

rest-api-spec/src/main/resources/rest-api-spec/test/search.aggregation/20_terms.yaml

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ setup:
1818
type: long
1919
double:
2020
type: double
21+
scaled_float:
22+
type: scaled_float
23+
scaling_factor: 100
2124
date:
2225
type: date
2326

@@ -282,6 +285,52 @@ setup:
282285

283286
- match: { aggregations.double_terms.buckets.1.doc_count: 1 }
284287

288+
---
289+
"Scaled float test":
290+
- do:
291+
index:
292+
index: test_1
293+
type: test
294+
id: 1
295+
body: { "scaled_float": 9.99 }
296+
297+
- do:
298+
index:
299+
index: test_1
300+
type: test
301+
id: 2
302+
body: { "scaled_float": 9.994 }
303+
304+
- do:
305+
index:
306+
index: test_1
307+
type: test
308+
id: 3
309+
body: { "scaled_float": 8.99 }
310+
311+
- do:
312+
indices.refresh: {}
313+
314+
- do:
315+
search:
316+
body: { "size" : 0, "aggs" : { "scaled_float_terms" : { "terms" : { "field" : "scaled_float" } } } }
317+
318+
- match: { hits.total: 3 }
319+
320+
- length: { aggregations.scaled_float_terms.buckets: 2 }
321+
322+
- match: { aggregations.scaled_float_terms.buckets.0.key: 9.99 }
323+
324+
- is_false: aggregations.scaled_float_terms.buckets.0.key_as_string
325+
326+
- match: { aggregations.scaled_float_terms.buckets.0.doc_count: 2 }
327+
328+
- match: { aggregations.scaled_float_terms.buckets.1.key: 8.99 }
329+
330+
- is_false: aggregations.scaled_float_terms.buckets.1.key_as_string
331+
332+
- match: { aggregations.scaled_float_terms.buckets.1.doc_count: 1 }
333+
285334
---
286335
"Date test":
287336
- do:

0 commit comments

Comments
 (0)