From 4436e89a0f48d54e47c2c33689e0b828cbb31149 Mon Sep 17 00:00:00 2001 From: luyuncheng Date: Fri, 1 Mar 2024 14:49:55 +0800 Subject: [PATCH 1/2] Fix KNNQuery Precision Signed-off-by: luyuncheng --- .../opensearch/_types/query_dsl/KnnQuery.java | 7 ++++++- .../_types/query_dsl/KnnQueryTest.java | 7 +++++++ .../opensearch/model/RequestEncodingTest.java | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQuery.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQuery.java index 175fe6e11a..e9144c0550 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQuery.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQuery.java @@ -9,6 +9,7 @@ package org.opensearch.client.opensearch._types.query_dsl; import jakarta.json.stream.JsonGenerator; +import java.math.BigDecimal; import java.util.function.Function; import javax.annotation.Nullable; import org.opensearch.client.json.JsonpDeserializable; @@ -19,6 +20,8 @@ import org.opensearch.client.util.ApiTypeHelper; import org.opensearch.client.util.ObjectBuilder; +import static java.math.RoundingMode.HALF_UP; + @JsonpDeserializable public class KnnQuery extends QueryBase implements QueryVariant { private final String field; @@ -93,7 +96,9 @@ protected void serializeInternal(JsonGenerator generator, JsonpMapper mapper) { generator.writeKey("vector"); generator.writeStartArray(); for (float value : this.vector) { - generator.write(value); + BigDecimal b = new BigDecimal(value); + double T = b.setScale(6, HALF_UP).doubleValue(); + generator.write(T); } generator.writeEnd(); diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQueryTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQueryTest.java index df036f42c1..dbbf1cd176 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQueryTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQueryTest.java @@ -11,4 +11,11 @@ public void toBuilder() { assertEquals(toJson(copied), toJson(origin)); } + + @Test + public void toBuilderPrecision() { + KnnQuery origin = new KnnQuery.Builder().field("field").vector(new float[] { 0.1f, 0.4f }).k(1).build(); + + assertEquals(toJson(origin), "{\"field\":{\"vector\":[0.1,0.4],\"k\":1}}"); + } } diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java index 61a57e1091..cfbc107d82 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java @@ -33,6 +33,7 @@ package org.opensearch.client.opensearch.model; import org.junit.Test; +import org.opensearch.client.json.jackson.JacksonJsonpMapper; import org.opensearch.client.json.jsonb.JsonbJsonpMapper; import org.opensearch.client.opensearch.core.SearchRequest; @@ -61,4 +62,22 @@ public void testParametersNotInJson() { assertNull(request.q()); } + + @Test + public void testKnnVectorPrecision() { + + float[] vector = {0.4f, 0.3f}; + SearchRequest request = new SearchRequest.Builder().q("knn") + .query(q -> q.knn(k -> k.field("values").vector(vector).k(1))) + .build(); + + JacksonJsonpMapper mapper = new JacksonJsonpMapper(); + String str = toJson(request, mapper); + assertEquals("{\"query\":{\"knn\":{\"values\":{\"vector\":[0.4,0.3],\"k\":1}}}}", str); + + request = fromJson(str, SearchRequest.class, mapper); + + assertTrue(request.query().isKnn()); + assertNull(request.q()); + } } From 1e2e50d972b85cbeb3461ac69d2985ea01a98f93 Mon Sep 17 00:00:00 2001 From: luyuncheng Date: Fri, 1 Mar 2024 15:08:05 +0800 Subject: [PATCH 2/2] Fix KNNQuery Precision & spotless Signed-off-by: luyuncheng --- .../client/opensearch/_types/query_dsl/KnnQuery.java | 4 ++-- .../client/opensearch/model/RequestEncodingTest.java | 6 ++---- 2 files changed, 4 insertions(+), 6 deletions(-) diff --git a/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQuery.java b/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQuery.java index e9144c0550..0c61cbd02e 100644 --- a/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQuery.java +++ b/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQuery.java @@ -8,6 +8,8 @@ package org.opensearch.client.opensearch._types.query_dsl; +import static java.math.RoundingMode.HALF_UP; + import jakarta.json.stream.JsonGenerator; import java.math.BigDecimal; import java.util.function.Function; @@ -20,8 +22,6 @@ import org.opensearch.client.util.ApiTypeHelper; import org.opensearch.client.util.ObjectBuilder; -import static java.math.RoundingMode.HALF_UP; - @JsonpDeserializable public class KnnQuery extends QueryBase implements QueryVariant { private final String field; diff --git a/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java b/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java index cfbc107d82..5941de2b01 100644 --- a/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java +++ b/java-client/src/test/java/org/opensearch/client/opensearch/model/RequestEncodingTest.java @@ -66,10 +66,8 @@ public void testParametersNotInJson() { @Test public void testKnnVectorPrecision() { - float[] vector = {0.4f, 0.3f}; - SearchRequest request = new SearchRequest.Builder().q("knn") - .query(q -> q.knn(k -> k.field("values").vector(vector).k(1))) - .build(); + float[] vector = { 0.4f, 0.3f }; + SearchRequest request = new SearchRequest.Builder().q("knn").query(q -> q.knn(k -> k.field("values").vector(vector).k(1))).build(); JacksonJsonpMapper mapper = new JacksonJsonpMapper(); String str = toJson(request, mapper);