Skip to content

Update Documents with Functions #1074

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 6 commits into from
Apr 4, 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
27 changes: 27 additions & 0 deletions meilisearch/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,33 @@ def multi_search(
body={"queries": queries, "federation": federation},
)

def update_documents_by_function(
self, index_uid: str, queries: Dict[str, List[Dict[str, Any]]]
) -> Dict[str, Any]:
"""Update Documents by function
Parameters
----------
index_uid:
The index_uid where you want to update documents of.
queries:
List of dictionaries containing functions with or without filters that you want to use to update documents.

Returns
-------
task_info:
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task

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(
path=f"{self.config.paths.index}/{index_uid}/{self.config.paths.document}/{self.config.paths.edit}",
body=dict(queries),
)

def get_all_stats(self) -> Dict[str, Any]:
"""Get all stats of Meilisearch

Expand Down
1 change: 1 addition & 0 deletions meilisearch/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Paths:
search_cutoff_ms = "search-cutoff-ms"
proximity_precision = "proximity-precision"
localized_attributes = "localized-attributes"
edit = "edit"

def __init__(
self,
Expand Down
17 changes: 17 additions & 0 deletions tests/client/test_client_update_document_by_functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import pytest

from tests.common import INDEX_UID


@pytest.mark.usefixtures("enable_edit_documents_by_function")
def test_update_document_by_function(client, index_with_documents):
"""Delete the document with id and update document title"""
index_with_documents()
response = client.update_documents_by_function(
INDEX_UID,
{"function": 'if doc.id == "522681" {doc = () } else {doc.title = `* ${doc.title} *`}'},
)

assert isinstance(response, dict)
assert isinstance(response["taskUid"], int)
assert response["indexUid"] == INDEX_UID
35 changes: 35 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
from typing import Optional

import requests
from pytest import fixture

import meilisearch
Expand Down Expand Up @@ -233,6 +234,40 @@ def get_private_key(client):
return key


@fixture
def enable_vector_search():
requests.patch(
f"{common.BASE_URL}/experimental-features",
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
json={"vectorStore": True},
timeout=10,
)
yield
requests.patch(
f"{common.BASE_URL}/experimental-features",
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
json={"vectorStore": False},
timeout=10,
)


@fixture
def enable_edit_documents_by_function():
requests.patch(
f"{common.BASE_URL}/experimental-features",
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
json={"editDocumentsByFunction": True},
timeout=10,
)
yield
requests.patch(
f"{common.BASE_URL}/experimental-features",
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
json={"editDocumentsByFunction": False},
timeout=10,
)


@fixture
def new_embedders():
return {
Expand Down
1 change: 0 additions & 1 deletion tests/index/test_index_document_meilisearch.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ def test_update_documents_json_custom_serializer(empty_index):


def test_update_documents_raw_custom_serializer(empty_index):

documents = [
{"id": uuid4(), "title": "test 1", "when": datetime.now()},
{"id": uuid4(), "title": "Test 2", "when": datetime.now()},
Expand Down