Skip to content

Commit a43baec

Browse files
Merge #1063
1063: Support searching for similar documents r=sanders41 a=ellnix # Pull Request ## Related issue Fixes #988 Instructions were not to add the `GET` endpoint, it seems other integrations also do not have it. I tried as closely as possible to match the documentation and the signature of the main `search` method. If I should wrap the results in a class, please let me know once again and I'll do it. Co-authored-by: ellnix <[email protected]>
2 parents 6941a59 + 319baff commit a43baec

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,3 +751,5 @@ get_all_batches_1: |-
751751
client.get_batches()
752752
get_batch_1: |-
753753
client.get_batch(BATCH_UID)
754+
get_similar_post_1: |-
755+
client.index("INDEX_NAME").get_similar_documents({"id": "TARGET_DOCUMENT_ID", "embedder": "default"})

meilisearch/config.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class Paths:
2020
facet_search = "facet-search"
2121
multi_search = "multi-search"
2222
document = "documents"
23+
similar = "similar"
2324
setting = "settings"
2425
ranking_rules = "ranking-rules"
2526
distinct_attribute = "distinct-attribute"

meilisearch/index.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,30 @@ def get_documents(
406406
)
407407
return DocumentsResults(response)
408408

409+
def get_similar_documents(self, parameters: Mapping[str, Any]) -> Dict[str, Any]:
410+
"""Get the documents similar to a document.
411+
412+
Parameters
413+
----------
414+
parameters:
415+
parameters accepted by the get similar documents route: https://www.meilisearch.com/docs/reference/api/similar#body
416+
"id" and "embedder" are required.
417+
418+
Returns
419+
-------
420+
results:
421+
Dictionary with hits, offset, limit, processingTimeMs, and id
422+
423+
Raises
424+
------
425+
MeilisearchApiError
426+
An error containing details about why Meilisearch can't process your request. Meilisearch error codes are described here: https://www.meilisearch.com/docs/reference/errors/error_codes#meilisearch-errors
427+
"""
428+
return self.http.post(
429+
f"{self.config.paths.index}/{self.uid}/{self.config.paths.similar}",
430+
body=parameters,
431+
)
432+
409433
def add_documents(
410434
self,
411435
documents: Sequence[Mapping[str, Any]],

tests/index/test_index_document_meilisearch.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,32 @@ def test_get_documents_filter_with_fields(index_with_documents):
266266
assert next(iter(genres)) == "action"
267267

268268

269+
@pytest.mark.usefixtures("enable_vector_search")
270+
def test_get_similar_documents(empty_index):
271+
index = empty_index()
272+
index.update_embedders({"manual": {"source": "userProvided", "dimensions": 3}})
273+
274+
hp3 = {
275+
"id": 1,
276+
"title": "Harry Potter and the Prisoner of Azkaban",
277+
"_vectors": {"manual": [0.8, 0.8, 0.8]},
278+
}
279+
hp4 = {
280+
"id": 2,
281+
"title": "Harry Potter and the Goblet of Fire",
282+
"_vectors": {"manual": [0.7, 0.7, 0.9]},
283+
}
284+
lotr = {"id": 3, "title": "The Lord of the Rings", "_vectors": {"manual": [0.6, 0.5, 0.2]}}
285+
286+
addition = index.add_documents([hp3, hp4, lotr])
287+
index.wait_for_task(addition.task_uid)
288+
289+
similars = index.get_similar_documents({"id": hp4["id"], "embedder": "manual"})
290+
291+
assert similars["hits"][0]["id"] == hp3["id"]
292+
assert similars["hits"][1]["id"] == lotr["id"]
293+
294+
269295
def test_update_documents(index_with_documents, small_movies):
270296
"""Tests updating a single document and a set of documents."""
271297
index = index_with_documents()

0 commit comments

Comments
 (0)