Skip to content

Added inner_hits option to kNN search #1777

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

Merged
merged 1 commit into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions elasticsearch_dsl/search_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,7 @@ def knn(
boost=None,
filter=None,
similarity=None,
inner_hits=None,
):
"""
Add a k-nearest neighbor (kNN) search.
Expand All @@ -526,6 +527,7 @@ def knn(
:arg boost: A floating-point boost factor for kNN scores
:arg filter: query to filter the documents that can match
:arg similarity: the minimum similarity required for a document to be considered a match, as a float value
:arg inner_hits: retrieve hits from nested field

Example::

Expand Down Expand Up @@ -560,6 +562,8 @@ def knn(
s._knn[-1]["filter"] = filter
if similarity is not None:
s._knn[-1]["similarity"] = similarity
if inner_hits is not None:
s._knn[-1]["inner_hits"] = inner_hits
return s

def rank(self, rrf=None):
Expand Down
2 changes: 2 additions & 0 deletions tests/_async/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def test_knn():
query_vector_builder={
"text_embedding": {"model_id": "foo", "model_text": "search text"}
},
inner_hits={"size": 1},
)
assert {
"knn": [
Expand All @@ -283,6 +284,7 @@ def test_knn():
"text_embedding": {"model_id": "foo", "model_text": "search text"}
},
"boost": 0.8,
"inner_hits": {"size": 1},
},
]
} == s.to_dict()
Expand Down
2 changes: 2 additions & 0 deletions tests/_sync/test_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ def test_knn():
query_vector_builder={
"text_embedding": {"model_id": "foo", "model_text": "search text"}
},
inner_hits={"size": 1},
)
assert {
"knn": [
Expand All @@ -283,6 +284,7 @@ def test_knn():
"text_embedding": {"model_id": "foo", "model_text": "search text"}
},
"boost": 0.8,
"inner_hits": {"size": 1},
},
]
} == s.to_dict()
Expand Down
Loading