opensearch-java
opensearch-java copied to clipboard
Fix KNNQuery Precision
Description
There is a knn query like
KnnQuery origin = new KnnQuery.Builder().field("field").vector(new float[] { 0.1f, 0.4f }).k(1).build();
When it serialize to json string, the result would be:
{"field":{"vector":[0.10000000149011612,0.4000000059604645],"k":1}}
like the test code shows:
https://github.com/opensearch-project/opensearch-java/blob/4436e89a0f48d54e47c2c33689e0b828cbb31149/java-client/src/test/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQueryTest.java#L16-L20
Proposal
I think it because in KnnQuery#serializeInternal
call generator.write(value);
but jakarta.json.stream.JsonGenerator
write numeric only support double. when KnnQuery#vector
is float type, it would make precision not correctly.
https://github.com/opensearch-project/opensearch-java/blob/045c805eb58a166f55888d27a0358d184f117761/java-client/src/main/java/org/opensearch/client/opensearch/_types/query_dsl/KnnQuery.java#L86-L97
Issues Resolved
#883
By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. For more information on following Developer Certificate of Origin and signing off your commits, please check here.
@luyuncheng I believe the precision with float numbers is not specific to knn plugin (the float is used in many other places like boost etc, ...), why it is causing issues in your case?