Skip to content

Commit 02d7608

Browse files
Bug fix for scaled_float in encodePoint method (#18952) (#18968)
* Fix scaled float bug * Update changelog * Update changelog * Add clamps --------- (cherry picked from commit 35a91c1) Signed-off-by: Prudhvi Godithi <[email protected]> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 79d3e38 commit 02d7608

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
119119
- Fix socks5 user password settings for Azure repo ([#18904](https://github.com/opensearch-project/OpenSearch/pull/18904))
120120
- Fix gRPC transport SETTING_GRPC_MAX_MSG_SIZE setting not exposed to users ([#18910](https://github.com/opensearch-project/OpenSearch/pull/18910))
121121
- Reset isPipelineResolved to false to resolve the system ingest pipeline again. ([#18911](https://github.com/opensearch-project/OpenSearch/pull/18911))
122+
- Bug fix for `scaled_float` in `encodePoint` method ([#18952](https://github.com/opensearch-project/OpenSearch/pull/18952))
122123

123124
### Security
124125

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ public byte[] encodePoint(Number value) {
219219

220220
@Override
221221
public byte[] encodePoint(Object value, boolean roundUp) {
222-
double doubleValue = parse(value);
223-
long scaledValue = Math.round(scale(doubleValue));
222+
long scaledValue = Math.round(scale(value));
224223
if (roundUp) {
225224
if (scaledValue < Long.MAX_VALUE) {
226225
scaledValue = scaledValue + 1;
@@ -230,7 +229,9 @@ public byte[] encodePoint(Object value, boolean roundUp) {
230229
scaledValue = scaledValue - 1;
231230
}
232231
}
233-
return encodePoint(scaledValue);
232+
byte[] point = new byte[Long.BYTES];
233+
LongPoint.encodeDimension(scaledValue, point, 0);
234+
return point;
234235
}
235236

236237
public double getScalingFactor() {

modules/mapper-extras/src/test/java/org/opensearch/index/mapper/ScaledFloatFieldMapperTests.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
package org.opensearch.index.mapper;
3434

3535
import org.apache.lucene.document.Document;
36+
import org.apache.lucene.document.LongPoint;
3637
import org.apache.lucene.document.SortedNumericDocValuesField;
3738
import org.apache.lucene.document.StoredField;
3839
import org.apache.lucene.index.DirectoryReader;
@@ -473,4 +474,19 @@ public void testRejectIndexOptions() {
473474
assertThat(e.getMessage(), containsString("Failed to parse mapping [_doc]: Field [scaling_factor] is required"));
474475
assertWarnings("Parameter [index_options] has no effect on type [scaled_float] and will be removed in future");
475476
}
477+
478+
public void testScaledFloatEncodePoint() {
479+
double scalingFactor = 100.0;
480+
ScaledFloatFieldMapper.ScaledFloatFieldType fieldType = new ScaledFloatFieldMapper.ScaledFloatFieldType(
481+
"test_field",
482+
scalingFactor
483+
);
484+
double originalValue = 10.5;
485+
byte[] encodedRoundUp = fieldType.encodePoint(originalValue, true);
486+
byte[] encodedRoundDown = fieldType.encodePoint(originalValue, false);
487+
long decodedUp = LongPoint.decodeDimension(encodedRoundUp, 0);
488+
long decodedDown = LongPoint.decodeDimension(encodedRoundDown, 0);
489+
assertEquals(1051, decodedUp); // 10.5 scaled = 1050, then +1 = 1051 (represents 10.51)
490+
assertEquals(1049, decodedDown); // 10.5 scaled = 1050, then -1 = 1049 (represents 10.49)
491+
}
476492
}

release-notes/opensearch.release-notes-3.2.0.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,5 @@ Compatible with OpenSearch and OpenSearch Dashboards version 3.2.0
111111
* Close IndexFieldDataService asynchronously ([#18888](https://github.com/opensearch-project/OpenSearch/pull/18888))
112112
* Fix query string regex queries incorrectly swallowing TooComplexToDeterminizeException ([#18883](https://github.com/opensearch-project/OpenSearch/pull/18883))
113113
* Fix socks5 user password settings for Azure repo ([#18904](https://github.com/opensearch-project/OpenSearch/pull/18904))
114-
* Reset isPipelineResolved to false to resolve the system ingest pipeline again. ([#18911](https://github.com/opensearch-project/OpenSearch/pull/18911))
114+
* Reset isPipelineResolved to false to resolve the system ingest pipeline again. ([#18911](https://github.com/opensearch-project/OpenSearch/pull/18911))
115+
* Bug fix for `scaled_float` in `encodePoint` method ([#18952](https://github.com/opensearch-project/OpenSearch/pull/18952))

0 commit comments

Comments
 (0)