Skip to content

Commit 75cea4e

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 471acef commit 75cea4e

File tree

3 files changed

+86
-16
lines changed

3 files changed

+86
-16
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
8585
- Fix bytes parameter on `_cat/recovery` ([#17598](https://github.com/opensearch-project/OpenSearch/pull/17598))
8686
- Fix slow performance of FeatureFlag checks ([#17611](https://github.com/opensearch-project/OpenSearch/pull/17611))
8787
- Fix shard recovery in pull-based ingestion to avoid skipping messages ([#17868](https://github.com/opensearch-project/OpenSearch/pull/17868)))
88+
- Fix match query on a `scaled_float` property no longer matches for some values ([#17879](https://github.com/opensearch-project/OpenSearch/pull/17879))
8889

8990
### Security
9091

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

+4-4
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 = Math.round(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 = Math.round(parse(value) * scalingFactor);
246246
scaledValues.add(scaledValue);
247247
}
248248
Query query = NumberFieldMapper.NumberType.LONG.termsQuery(
@@ -262,15 +262,15 @@ public Query rangeQuery(Object lowerTerm, Object upperTerm, boolean includeLower
262262
failIfNotIndexedAndNoDocValues();
263263
Long lo = null;
264264
if (lowerTerm != null) {
265-
double dValue = scale(lowerTerm);
265+
double dValue = Math.round(parse(lowerTerm) * scalingFactor);
266266
if (includeLower == false) {
267267
dValue = Math.nextUp(dValue);
268268
}
269269
lo = Math.round(Math.ceil(dValue));
270270
}
271271
Long hi = null;
272272
if (upperTerm != null) {
273-
double dValue = scale(upperTerm);
273+
double dValue = Math.round(parse(upperTerm) * scalingFactor);
274274
if (includeUpper == false) {
275275
dValue = Math.nextDown(dValue);
276276
}

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

+81-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,64 @@ setup:
92103

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

106+
---
107+
"Search_With_Precision_Lose":
108+
- skip:
109+
version: " - 3.99.99"
110+
reason: "fixed in 3.0.0"
111+
112+
- do:
113+
search:
114+
index: test
115+
body: {
116+
"query": {
117+
"range": {
118+
"number": {
119+
"gte": "92233720368547750"
120+
}
121+
}
122+
}
123+
}
124+
- length: { hits.hits: 2 }
125+
- match: { hits.hits.0._id: "5" }
126+
- match: { hits.hits.0._source.number: "92233720368547750" }
127+
- match: { hits.hits.1._id: "6" }
128+
- match: { hits.hits.1._source.number: 92233720368547750 }
129+
130+
- do:
131+
search:
132+
index: test
133+
body: {
134+
"query": {
135+
"term": {
136+
"field0_BigDecimal": "92233720368547750"
137+
}
138+
}
139+
}
140+
141+
- length: { hits.hits: 2 }
142+
- match: { hits.hits.0._id: "5" }
143+
- match: { hits.hits.0._source.number: "92233720368547750" }
144+
- match: { hits.hits.1._id: "6" }
145+
- match: { hits.hits.1._source.number: 92233720368547750 }
146+
147+
- do:
148+
search:
149+
index: test
150+
body: {
151+
"query": {
152+
"terms": {
153+
"field0_BigDecimal": [ "92233720368547750" ]
154+
}
155+
}
156+
}
157+
158+
- length: { hits.hits: 2 }
159+
- match: { hits.hits.0._id: "5" }
160+
- match: { hits.hits.0._source.number: "92233720368547750" }
161+
- match: { hits.hits.1._id: "6" }
162+
- match: { hits.hits.1._source.number: 92233720368547750 }
163+
95164
---
96165
"Sort":
97166

@@ -103,7 +172,7 @@ setup:
103172
number:
104173
order: asc
105174

106-
- match: { hits.total.value: 4 }
175+
- match: { hits.total.value: 6 }
107176
- match: { hits.hits.0._id: "3" }
108177
- match: { hits.hits.0.sort.0: -2.1 }
109178

@@ -119,7 +188,7 @@ setup:
119188
order: asc
120189
numeric_type: long
121190

122-
- match: { hits.total.value: 4 }
191+
- match: { hits.total.value: 6 }
123192
- match: { hits.hits.0._id: "3" }
124193
- match: { hits.hits.0.sort.0: -2 }
125194

0 commit comments

Comments
 (0)