Skip to content

Fix match query on a scaled_float property no longer matches for some values #17879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
### Removed

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

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@ public String typeName() {
@Override
public Query termQuery(Object value, QueryShardContext context) {
failIfNotIndexedAndNoDocValues();
long scaledValue = Math.round(scale(value));
long scaledValue = parsedLong(parse(value), scalingFactor);
Query query = NumberFieldMapper.NumberType.LONG.termQuery(name(), scaledValue, hasDocValues(), isSearchable());
if (boost() != 1f) {
query = new BoostQuery(query, boost());
Expand All @@ -242,7 +242,7 @@ public Query termsQuery(List<?> values, QueryShardContext context) {
failIfNotIndexedAndNoDocValues();
List<Long> scaledValues = new ArrayList<>(values.size());
for (Object value : values) {
long scaledValue = Math.round(scale(value));
long scaledValue = parsedLong(parse(value), scalingFactor);
scaledValues.add(scaledValue);
}
Query query = NumberFieldMapper.NumberType.LONG.termsQuery(
Expand Down Expand Up @@ -465,7 +465,7 @@ protected void parseCreateField(ParseContext context) throws IOException {
throw new IllegalArgumentException("[scaled_float] only supports finite values, but got [" + doubleValue + "]");
}
}
long scaledValue = Math.round(doubleValue * scalingFactor);
long scaledValue = parsedLong(doubleValue, scalingFactor);

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

private static long parsedLong(double value, double scalingFactor) {
return Math.round(value * scalingFactor);
}

/**
* Converts an Object to a double by checking it against known types first
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ setup:
id: 4
body: { "number" : 1.53 }

- do:
index:
index: test
id: 5
body: { "number": "92233720368547750" }
- do:
index:
index: test
id: 6
body: { "number": 92233720368547750 }

- do:
indices.refresh: {}

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

- match: { hits.total: 4 }
- match: { hits.total: 6 }

- length: { aggregations.my_terms.buckets: 3 }
- length: { aggregations.my_terms.buckets: 4 }

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

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

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

- match: { aggregations.my_terms.buckets.1.key: -2.1 }
- match: { aggregations.my_terms.buckets.2.key: -2.1 }

- is_false: aggregations.my_terms.buckets.1.key_as_string
- is_false: aggregations.my_terms.buckets.2.key_as_string

- match: { aggregations.my_terms.buckets.1.doc_count: 1 }
- match: { aggregations.my_terms.buckets.2.doc_count: 1 }

- match: { aggregations.my_terms.buckets.2.key: 1 }
- match: { aggregations.my_terms.buckets.3.key: 1 }

- is_false: aggregations.my_terms.buckets.2.key_as_string
- is_false: aggregations.my_terms.buckets.3.key_as_string

- match: { aggregations.my_terms.buckets.2.doc_count: 1 }
- match: { aggregations.my_terms.buckets.3.doc_count: 1 }

---
"Search":
Expand All @@ -76,14 +87,14 @@ setup:
rest_total_hits_as_int: true
body: { "size" : 0, "query" : { "range" : { "number" : { "gte" : -2 } } } }

- match: { hits.total: 3 }
- match: { hits.total: 5 }

- do:
search:
rest_total_hits_as_int: true
body: { "size" : 0, "query" : { "range" : { "number" : { "gte" : 0 } } } }

- match: { hits.total: 3 }
- match: { hits.total: 5 }

- do:
search:
Expand All @@ -92,6 +103,40 @@ setup:

- match: { hits.total: 2 }

- do:
search:
index: test
body: {
"query": {
"term": {
"number": "92233720368547750"
}
}
}

- length: { hits.hits: 2 }
- match: { hits.hits.0._id: "5" }
- match: { hits.hits.0._source.number: "92233720368547750" }
- match: { hits.hits.1._id: "6" }
- match: { hits.hits.1._source.number: 92233720368547750 }

- do:
search:
index: test
body: {
"query": {
"terms": {
"number": [ "92233720368547750" ]
}
}
}

- length: { hits.hits: 2 }
- match: { hits.hits.0._id: "5" }
- match: { hits.hits.0._source.number: "92233720368547750" }
- match: { hits.hits.1._id: "6" }
- match: { hits.hits.1._source.number: 92233720368547750 }

---
"Sort":

Expand All @@ -103,7 +148,7 @@ setup:
number:
order: asc

- match: { hits.total.value: 4 }
- match: { hits.total.value: 6 }
- match: { hits.hits.0._id: "3" }
- match: { hits.hits.0.sort.0: -2.1 }

Expand All @@ -119,7 +164,7 @@ setup:
order: asc
numeric_type: long

- match: { hits.total.value: 4 }
- match: { hits.total.value: 6 }
- match: { hits.hits.0._id: "3" }
- match: { hits.hits.0.sort.0: -2 }

Expand Down
Loading