diff --git a/.code-samples.meilisearch.yaml b/.code-samples.meilisearch.yaml index fff94476..e7eb46ce 100644 --- a/.code-samples.meilisearch.yaml +++ b/.code-samples.meilisearch.yaml @@ -751,3 +751,5 @@ get_all_batches_1: |- client.get_batches() get_batch_1: |- client.get_batch(BATCH_UID) +get_similar_post_1: |- + client.index("INDEX_NAME").get_similar_documents({"id": "TARGET_DOCUMENT_ID", "embedder": "default"}) diff --git a/meilisearch/config.py b/meilisearch/config.py index 624be11e..1d40a694 100644 --- a/meilisearch/config.py +++ b/meilisearch/config.py @@ -20,6 +20,7 @@ class Paths: facet_search = "facet-search" multi_search = "multi-search" document = "documents" + similar = "similar" setting = "settings" ranking_rules = "ranking-rules" distinct_attribute = "distinct-attribute" diff --git a/meilisearch/index.py b/meilisearch/index.py index 42f3bf10..7504e722 100644 --- a/meilisearch/index.py +++ b/meilisearch/index.py @@ -406,6 +406,30 @@ def get_documents( ) return DocumentsResults(response) + def get_similar_documents(self, parameters: Mapping[str, Any]) -> Dict[str, Any]: + """Get the documents similar to a document. + + Parameters + ---------- + parameters: + parameters accepted by the get similar documents route: https://www.meilisearch.com/docs/reference/api/similar#body + "id" and "embedder" are required. + + Returns + ------- + results: + Dictionary with hits, offset, limit, processingTimeMs, and id + + Raises + ------ + MeilisearchApiError + 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 + """ + return self.http.post( + f"{self.config.paths.index}/{self.uid}/{self.config.paths.similar}", + body=parameters, + ) + def add_documents( self, documents: Sequence[Mapping[str, Any]], diff --git a/tests/index/test_index_document_meilisearch.py b/tests/index/test_index_document_meilisearch.py index 2669e200..88a43142 100644 --- a/tests/index/test_index_document_meilisearch.py +++ b/tests/index/test_index_document_meilisearch.py @@ -266,6 +266,32 @@ def test_get_documents_filter_with_fields(index_with_documents): assert next(iter(genres)) == "action" +@pytest.mark.usefixtures("enable_vector_search") +def test_get_similar_documents(empty_index): + index = empty_index() + index.update_embedders({"manual": {"source": "userProvided", "dimensions": 3}}) + + hp3 = { + "id": 1, + "title": "Harry Potter and the Prisoner of Azkaban", + "_vectors": {"manual": [0.8, 0.8, 0.8]}, + } + hp4 = { + "id": 2, + "title": "Harry Potter and the Goblet of Fire", + "_vectors": {"manual": [0.7, 0.7, 0.9]}, + } + lotr = {"id": 3, "title": "The Lord of the Rings", "_vectors": {"manual": [0.6, 0.5, 0.2]}} + + addition = index.add_documents([hp3, hp4, lotr]) + index.wait_for_task(addition.task_uid) + + similars = index.get_similar_documents({"id": hp4["id"], "embedder": "manual"}) + + assert similars["hits"][0]["id"] == hp3["id"] + assert similars["hits"][1]["id"] == lotr["id"] + + def test_update_documents(index_with_documents, small_movies): """Tests updating a single document and a set of documents.""" index = index_with_documents()