Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a knn method to elasticsearch_dsl.search.Search #1691

Merged
merged 2 commits into from
Jan 3, 2024

Conversation

miguelgrinberg
Copy link
Collaborator

@miguelgrinberg miguelgrinberg commented Dec 22, 2023

Fixes #1679

This PR adds the knn option for k-nearest neighbor vector search.

@miguelgrinberg miguelgrinberg changed the title feat: Add a knn method to elasticsearch_dsl.search.Search Add a knn method to elasticsearch_dsl.search.Search Dec 22, 2023
@miguelgrinberg miguelgrinberg force-pushed the knn-option branch 2 times, most recently from 5833e00 to cc87f50 Compare December 22, 2023 16:49
@pquentin
Copy link
Member

This looks good but I did not test locally yet. However, can you please add docs? Unfortunately, the docstring is not enough, you need to edit docs/search_dsl.rst too. Here's the current output: https://elasticsearch-dsl--1691.org.readthedocs.build/en/1691/search_dsl.html

Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! I only have a small nit.

Comment on lines 503 to 511
def knn(
self,
field,
k,
num_candidates,
query_vector=None,
query_vector_builder=None,
filter=None,
similarity=None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@pquentin I wonder why is boost omitted from our documentation. But in any case, I have added it now.

Copy link
Member

@pquentin pquentin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! LGTM.

@pquentin pquentin added the backport 8.x Backport to 8.x label Jan 3, 2024
@pquentin pquentin merged commit baed085 into elastic:main Jan 3, 2024
18 checks passed
github-actions bot pushed a commit that referenced this pull request Jan 3, 2024
* Add a `knn` method to `elasticsearch_dsl.search.Search`

* add knn's boost option

(cherry picked from commit baed085)
pquentin pushed a commit that referenced this pull request Jan 3, 2024
* Add a `knn` method to `elasticsearch_dsl.search.Search`

* add knn's boost option

(cherry picked from commit baed085)

Co-authored-by: Miguel Grinberg <[email protected]>
@israellias
Copy link

We need an update for this change since this PR has arrived at ElasticSearch v8.12.0

I'm trying to use "knn" inside the "query" object. But I'm getting the following error:

elasticsearch_dsl.exceptions.UnknownDslObject: DSL class `knn` does not exist in query.

How could I add a new Knn class in the accepted ones? Something like

# elasticsearch-dsl/query.py

class Knn(Query):
    name = "knn"

@miguelgrinberg miguelgrinberg deleted the knn-option branch April 8, 2024 08:37
@miguelgrinberg
Copy link
Collaborator Author

@israellias I have added #1770 to track this request. Thanks!

@israellias
Copy link

Thanks @miguelgrinberg
Do you have any workaround?
I'm trying

queryset = queryset.update_from_dict(
    {
        "query": {
            "knn": {
                "field": "embedding",
                "query_vector": res.data[0].embedding,
                "filter": filter_for_knn,
                "num_candidates": 30,
            }
        },
    }
)

But I'm noticing that update_from_dict still calls Q which results in doing the same as

queryset.query("knn", field="embedding", query_vector=res.data[0].embedding, filter=filter_for_knn, num_candidates=30)

and that is throwing the error since the class is simply not listed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport 8.x Backport to 8.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Does elasticsearch-dsl-py support knn_search?
3 participants