opensearch-java icon indicating copy to clipboard operation
opensearch-java copied to clipboard

Fix KNNQuery Precision

Open luyuncheng opened this issue 11 months ago • 1 comments

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 avatar Mar 01 '24 07:03 luyuncheng

@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?

reta avatar Mar 13 '24 14:03 reta