Skip to content

Commit 1a19e47

Browse files
Merge #1074
1074: Update Documents with Functions r=curquiza a=MuddyHope # Pull Request ## Related issue Fixes #1010 ## What does this PR do? - Added function for updating the document with rhai functions. ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: MuddyHope <[email protected]> Co-authored-by: MuddyHope <[email protected]>
2 parents 7daf595 + 3697219 commit 1a19e47

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

meilisearch/client.py

+27
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,33 @@ def multi_search(
252252
body={"queries": queries, "federation": federation},
253253
)
254254

255+
def update_documents_by_function(
256+
self, index_uid: str, queries: Dict[str, List[Dict[str, Any]]]
257+
) -> Dict[str, Any]:
258+
"""Update Documents by function
259+
Parameters
260+
----------
261+
index_uid:
262+
The index_uid where you want to update documents of.
263+
queries:
264+
List of dictionaries containing functions with or without filters that you want to use to update documents.
265+
266+
Returns
267+
-------
268+
task_info:
269+
TaskInfo instance containing information about a task to track the progress of an asynchronous process.
270+
https://www.meilisearch.com/docs/reference/api/tasks#get-one-task
271+
272+
Raises
273+
------
274+
MeilisearchApiError
275+
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
276+
"""
277+
return self.http.post(
278+
path=f"{self.config.paths.index}/{index_uid}/{self.config.paths.document}/{self.config.paths.edit}",
279+
body=dict(queries),
280+
)
281+
255282
def get_all_stats(self) -> Dict[str, Any]:
256283
"""Get all stats of Meilisearch
257284

meilisearch/config.py

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ class Paths:
4444
search_cutoff_ms = "search-cutoff-ms"
4545
proximity_precision = "proximity-precision"
4646
localized_attributes = "localized-attributes"
47+
edit = "edit"
4748

4849
def __init__(
4950
self,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import pytest
2+
3+
from tests.common import INDEX_UID
4+
5+
6+
@pytest.mark.usefixtures("enable_edit_documents_by_function")
7+
def test_update_document_by_function(client, index_with_documents):
8+
"""Delete the document with id and update document title"""
9+
index_with_documents()
10+
response = client.update_documents_by_function(
11+
INDEX_UID,
12+
{"function": 'if doc.id == "522681" {doc = () } else {doc.title = `* ${doc.title} *`}'},
13+
)
14+
15+
assert isinstance(response, dict)
16+
assert isinstance(response["taskUid"], int)
17+
assert response["indexUid"] == INDEX_UID

tests/conftest.py

+35
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
from typing import Optional
44

5+
import requests
56
from pytest import fixture
67

78
import meilisearch
@@ -233,6 +234,40 @@ def get_private_key(client):
233234
return key
234235

235236

237+
@fixture
238+
def enable_vector_search():
239+
requests.patch(
240+
f"{common.BASE_URL}/experimental-features",
241+
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
242+
json={"vectorStore": True},
243+
timeout=10,
244+
)
245+
yield
246+
requests.patch(
247+
f"{common.BASE_URL}/experimental-features",
248+
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
249+
json={"vectorStore": False},
250+
timeout=10,
251+
)
252+
253+
254+
@fixture
255+
def enable_edit_documents_by_function():
256+
requests.patch(
257+
f"{common.BASE_URL}/experimental-features",
258+
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
259+
json={"editDocumentsByFunction": True},
260+
timeout=10,
261+
)
262+
yield
263+
requests.patch(
264+
f"{common.BASE_URL}/experimental-features",
265+
headers={"Authorization": f"Bearer {common.MASTER_KEY}"},
266+
json={"editDocumentsByFunction": False},
267+
timeout=10,
268+
)
269+
270+
236271
@fixture
237272
def new_embedders():
238273
return {

tests/index/test_index_document_meilisearch.py

-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,6 @@ def test_update_documents_json_custom_serializer(empty_index):
171171

172172

173173
def test_update_documents_raw_custom_serializer(empty_index):
174-
175174
documents = [
176175
{"id": uuid4(), "title": "test 1", "when": datetime.now()},
177176
{"id": uuid4(), "title": "Test 2", "when": datetime.now()},

0 commit comments

Comments
 (0)