Skip to content

Commit b36c641

Browse files
committed
Fix match query on a scaled_float property no longer matches for some values
Signed-off-by: kkewwei <[email protected]> Signed-off-by: kkewwei <[email protected]>
1 parent d18982c commit b36c641

File tree

3 files changed

+65
-15
lines changed

3 files changed

+65
-15
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
1818
### Removed
1919

2020
### Fixed
21+
- Fix match query on a `scaled_float` property no longer matches for some values ([#17879](https://github.com/opensearch-project/OpenSearch/pull/17879))
2122

2223
### Security
2324

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

+7-3
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public String typeName() {
229229
@Override
230230
public Query termQuery(Object value, QueryShardContext context) {
231231
failIfNotIndexedAndNoDocValues();
232-
long scaledValue = Math.round(scale(value));
232+
long scaledValue = parsedLong(parse(value), scalingFactor);
233233
Query query = NumberFieldMapper.NumberType.LONG.termQuery(name(), scaledValue, hasDocValues(), isSearchable());
234234
if (boost() != 1f) {
235235
query = new BoostQuery(query, boost());
@@ -242,7 +242,7 @@ public Query termsQuery(List<?> values, QueryShardContext context) {
242242
failIfNotIndexedAndNoDocValues();
243243
List<Long> scaledValues = new ArrayList<>(values.size());
244244
for (Object value : values) {
245-
long scaledValue = Math.round(scale(value));
245+
long scaledValue = parsedLong(parse(value), scalingFactor);
246246
scaledValues.add(scaledValue);
247247
}
248248
Query query = NumberFieldMapper.NumberType.LONG.termsQuery(
@@ -465,7 +465,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
465465
throw new IllegalArgumentException("[scaled_float] only supports finite values, but got [" + doubleValue + "]");
466466
}
467467
}
468-
long scaledValue = Math.round(doubleValue * scalingFactor);
468+
long scaledValue = parsedLong(doubleValue, scalingFactor);
469469

470470
List<Field> fields = NumberFieldMapper.NumberType.LONG.createFields(fieldType().name(), scaledValue, indexed, hasDocValues, stored);
471471
context.doc().addAll(fields);
@@ -483,6 +483,10 @@ private static Double parse(XContentParser parser, boolean coerce) throws IOExce
483483
return parser.doubleValue(coerce);
484484
}
485485

486+
private static long parsedLong(double value, double scalingFactor) {
487+
return Math.round(value * scalingFactor);
488+
}
489+
486490
/**
487491
* Converts an Object to a double by checking it against known types first
488492
*/

modules/mapper-extras/src/yamlRestTest/resources/rest-api-spec/test/scaled_float/10_basic.yml

+57-12
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ setup:
3535
id: 4
3636
body: { "number" : 1.53 }
3737

38+
- do:
39+
index:
40+
index: test
41+
id: 5
42+
body: { "number": "92233720368547750" }
43+
- do:
44+
index:
45+
index: test
46+
id: 6
47+
body: { "number": 92233720368547750 }
48+
3849
- do:
3950
indices.refresh: {}
4051

@@ -46,27 +57,27 @@ setup:
4657
rest_total_hits_as_int: true
4758
body: { "size" : 0, "aggs" : { "my_terms" : { "terms" : { "field" : "number" } } } }
4859

49-
- match: { hits.total: 4 }
60+
- match: { hits.total: 6 }
5061

51-
- length: { aggregations.my_terms.buckets: 3 }
62+
- length: { aggregations.my_terms.buckets: 4 }
5263

5364
- match: { aggregations.my_terms.buckets.0.key: 1.53 }
5465

5566
- is_false: aggregations.my_terms.buckets.0.key_as_string
5667

5768
- match: { aggregations.my_terms.buckets.0.doc_count: 2 }
5869

59-
- match: { aggregations.my_terms.buckets.1.key: -2.1 }
70+
- match: { aggregations.my_terms.buckets.2.key: -2.1 }
6071

61-
- is_false: aggregations.my_terms.buckets.1.key_as_string
72+
- is_false: aggregations.my_terms.buckets.2.key_as_string
6273

63-
- match: { aggregations.my_terms.buckets.1.doc_count: 1 }
74+
- match: { aggregations.my_terms.buckets.2.doc_count: 1 }
6475

65-
- match: { aggregations.my_terms.buckets.2.key: 1 }
76+
- match: { aggregations.my_terms.buckets.3.key: 1 }
6677

67-
- is_false: aggregations.my_terms.buckets.2.key_as_string
78+
- is_false: aggregations.my_terms.buckets.3.key_as_string
6879

69-
- match: { aggregations.my_terms.buckets.2.doc_count: 1 }
80+
- match: { aggregations.my_terms.buckets.3.doc_count: 1 }
7081

7182
---
7283
"Search":
@@ -76,14 +87,14 @@ setup:
7687
rest_total_hits_as_int: true
7788
body: { "size" : 0, "query" : { "range" : { "number" : { "gte" : -2 } } } }
7889

79-
- match: { hits.total: 3 }
90+
- match: { hits.total: 5 }
8091

8192
- do:
8293
search:
8394
rest_total_hits_as_int: true
8495
body: { "size" : 0, "query" : { "range" : { "number" : { "gte" : 0 } } } }
8596

86-
- match: { hits.total: 3 }
97+
- match: { hits.total: 5 }
8798

8899
- do:
89100
search:
@@ -92,6 +103,40 @@ setup:
92103

93104
- match: { hits.total: 2 }
94105

106+
- do:
107+
search:
108+
index: test
109+
body: {
110+
"query": {
111+
"term": {
112+
"number": "92233720368547750"
113+
}
114+
}
115+
}
116+
117+
- length: { hits.hits: 2 }
118+
- match: { hits.hits.0._id: "5" }
119+
- match: { hits.hits.0._source.number: "92233720368547750" }
120+
- match: { hits.hits.1._id: "6" }
121+
- match: { hits.hits.1._source.number: 92233720368547750 }
122+
123+
- do:
124+
search:
125+
index: test
126+
body: {
127+
"query": {
128+
"terms": {
129+
"number": [ "92233720368547750" ]
130+
}
131+
}
132+
}
133+
134+
- length: { hits.hits: 2 }
135+
- match: { hits.hits.0._id: "5" }
136+
- match: { hits.hits.0._source.number: "92233720368547750" }
137+
- match: { hits.hits.1._id: "6" }
138+
- match: { hits.hits.1._source.number: 92233720368547750 }
139+
95140
---
96141
"Sort":
97142

@@ -103,7 +148,7 @@ setup:
103148
number:
104149
order: asc
105150

106-
- match: { hits.total.value: 4 }
151+
- match: { hits.total.value: 6 }
107152
- match: { hits.hits.0._id: "3" }
108153
- match: { hits.hits.0.sort.0: -2.1 }
109154

@@ -119,7 +164,7 @@ setup:
119164
order: asc
120165
numeric_type: long
121166

122-
- match: { hits.total.value: 4 }
167+
- match: { hits.total.value: 6 }
123168
- match: { hits.hits.0._id: "3" }
124169
- match: { hits.hits.0.sort.0: -2 }
125170

0 commit comments

Comments
 (0)