Skip to content

Commit ade3cae

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 4c3230a commit ade3cae

File tree

3 files changed

+126
-2
lines changed

3 files changed

+126
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6868
### Fixed
6969
- Fix bytes parameter on `_cat/recovery` ([#17598](https://github.com/opensearch-project/OpenSearch/pull/17598))
7070
- Fix slow performance of FeatureFlag checks ([#17611](https://github.com/opensearch-project/OpenSearch/pull/17611))
71+
- Fix match query on a `scaled_float` property no longer matches for some values ([#17879](https://github.com/opensearch-project/OpenSearch/pull/17879))
7172

7273
### Security
7374

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

+2-2
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(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
# The test setup includes two parts:
2+
# part1: test mapping and indexing
3+
# part2: test query
4+
---
5+
"Mappings and Indexing":
6+
7+
# Create indices with constant_keyword field type
8+
- do:
9+
indices.create:
10+
index: test
11+
body:
12+
mappings:
13+
properties:
14+
genre:
15+
type: "scaled_float"
16+
scaling_factor: "100.0"
17+
18+
# Index documents to test integer and string are both ok.
19+
- do:
20+
index:
21+
index: test
22+
id: 1
23+
body: {
24+
"genre": "92233720368547750"
25+
}
26+
27+
- do:
28+
index:
29+
index: test
30+
id: 2
31+
body: {
32+
"genre": 92233720368547750
33+
}
34+
35+
- do:
36+
index:
37+
index: test
38+
id: 3
39+
body: {
40+
"genre": 1.11
41+
}
42+
# Refresh
43+
- do:
44+
indices.refresh:
45+
index: test
46+
47+
# Check mapping
48+
- do:
49+
indices.get_mapping:
50+
index: test
51+
- is_true: test.mappings
52+
- match: { test.mappings.properties.genre.type: scaled_float }
53+
54+
# Verify Document Count
55+
- do:
56+
search:
57+
index: test
58+
body: {
59+
query: {
60+
match_all: {}
61+
}
62+
}
63+
64+
- length: { hits.hits: 3 }
65+
66+
---
67+
"Queries":
68+
- skip:
69+
version: " - 3.99.99"
70+
reason: "fixed in 3.0.0"
71+
72+
# Test rangeQuery
73+
- do:
74+
search:
75+
index: test
76+
body: {
77+
query: {
78+
range: {
79+
genre: {
80+
gte: "92233720368547750"
81+
}
82+
}
83+
}
84+
}
85+
86+
- length: { hits.hits: 2 }
87+
- match: { hits.hits.0._id: "1" }
88+
- match: { hits.hits.0._id: "2" }
89+
90+
- do:
91+
search:
92+
index: test
93+
body: {
94+
query: {
95+
term: {
96+
"field0_BigDecimal": "92233720368547750"
97+
}
98+
}
99+
}
100+
101+
- length: { hits.hits: 2 }
102+
- match: { hits.hits.0._id: "1" }
103+
- match: { hits.hits.0._id: "2" }
104+
105+
- do:
106+
search:
107+
index: test
108+
body: {
109+
query: {
110+
terms: {
111+
"field0_BigDecimal": [ "92233720368547750" ]
112+
}
113+
}
114+
}
115+
116+
- length: { hits.hits: 2 }
117+
- match: { hits.hits.0._id: "1" }
118+
- match: { hits.hits.0._id: "2" }
119+
120+
# Delete Index when connection is teardown
121+
- do:
122+
indices.delete:
123+
index: test

0 commit comments

Comments
 (0)