Skip to content

Commit

Permalink
knn as a query option (#1781) (#1807)
Browse files Browse the repository at this point in the history
Fixes #1770

(cherry picked from commit 9a53a5b)

Co-authored-by: Miguel Grinberg <miguel.grinberg@gmail.com>
  • Loading branch information
github-actions[bot] and miguelgrinberg authored Apr 29, 2024
1 parent c65e7bf commit 6e595c0
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
4 changes: 4 additions & 0 deletions elasticsearch_dsl/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ class Intervals(Query):
name = "intervals"


class Knn(Query):
name = "knn"


class Limit(Query):
name = "limit"

Expand Down
11 changes: 11 additions & 0 deletions tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -612,3 +612,14 @@ def test_expand_double_underscore_to_dot_setting():
q = query.Term(comment__count=2)
assert q.to_dict() == {"term": {"comment__count": 2}}
utils.EXPAND__TO_DOT = True


def test_knn_query():
q = query.Knn(field="image-vector", query_vector=[-5, 9, -12], num_candidates=10)
assert q.to_dict() == {
"knn": {
"field": "image-vector",
"query_vector": [-5, 9, -12],
"num_candidates": 10,
}
}

0 comments on commit 6e595c0

Please sign in to comment.