Skip to content

Support searching for similar documents #1063

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 2 commits into from
Jan 24, 2025
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
2 changes: 2 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
24 changes: 24 additions & 0 deletions meilisearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]],
Expand Down
26 changes: 26 additions & 0 deletions tests/index/test_index_document_meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Loading