opensearch-project / opensearch-net

OpenSearch .NET Client
Apache License 2.0
107 stars 49 forks source link

[FEATURE] Add support for knn queries #178

Closed rharpertm closed 1 year ago

rharpertm commented 1 year ago

Is your feature request related to a problem?

Currently, it appears that it's not possible to create an approximate knn query via the library, It'd be great to add support for this query type.

What solution would you like?

It would be nice to have a query structure that fits with the current library conventions. Perhaps something like:

q.knn(knn => knn.Field("search_vector")
                    .Vector(embString)
                    .K(25));

What alternatives have you considered?

For our current use case, we can get around this using a function_score and a Raw query DSL JSON string.

q.FunctionScore(fs =>
                    fs.Functions(f => f.Weight(10))
                        .Query(query =>
                            query.Raw(@"{
                                ""knn"": {
                                    ""search_vector"": {
                                        ""vector"": [" + embeddingString + @"],
                                        ""k"":25
                                    }
                                }
                            }")
                        ));

Do you have any additional context?

knn queries have been around since v1.0 of OpenSearch, and itd be awesome to have this query type supported.

Xtansia commented 1 year ago

@rharpertm Thank you for bringing this to our attention, unfortunately there is a handful of features like this that primarily come from plugins that are not currently implemented in the client. We do have some big overarching actions in progress around code generation to patch these holes (https://github.com/opensearch-project/opensearch-clients/issues/19 & https://github.com/opensearch-project/opensearch-clients/issues/55). In the meantime, however, we would gladly accept a PR to add this specific binding if you or anyone else wished to put one together.

Xtansia commented 1 year ago

Hi @rharpertm,

This support has now been released as part of v1.4.0 of the client.

You can find a brief example of using it here in the integration test: https://github.com/opensearch-project/opensearch-net/blob/v1.4.0/tests/Tests/QueryDsl/Specialized/Knn/KnnQueryUsageTests.cs#L82