This feature is experimental. Enable it before use and expect its API to change between releases.
When to use functions
- Bulk field updates: add, rename, or remove fields across thousands of documents
- Data normalization: convert strings to uppercase, trim whitespace, reformat dates
- Computed fields: derive new fields from existing ones (e.g. concatenate
firstNameandlastNameintofullName) - Conditional edits: update only documents matching a filter expression
Enable the feature
Send aPATCH request to /experimental-features:
Basic usage
Send aPOST request to /indexes/{index_uid}/documents/edit with a function parameter containing Rhai code. The function receives each document as doc and can modify its fields directly:
title field to uppercase for every document in the movies index. The operation is asynchronous and returns a task object.
Filter target documents
Use thefilter parameter to apply the function only to documents matching a filter expression:
status to "archived" only for movies released before the year 2000. The filter parameter uses the same filter expression syntax as search filters. Filtered attributes must be declared in filterableAttributes.
Pass data with context
Thecontext parameter lets you pass external data into your function. Access it through the context variable:
Examples
Add a new field
Remove a field
Concatenate fields
Conditional logic
Use context for batch tagging
Rhai language basics
Rhai is a lightweight scripting language. Here are the most common operations for document editing:| Operation | Syntax |
|---|---|
| Set a field | doc.field = value |
| String interpolation | doc.field = `Hello ${doc.name}` |
| Uppercase / lowercase | doc.field.to_upper(), doc.field.to_lower() |
| Remove a field | doc.remove("field") |
| Conditionals | if condition { ... } else { ... } |
| Access context | context.key |
| Check if array contains | array.contains(value) |
| String concatenation | "hello" + " " + "world" |
| Math operations | doc.price * 0.9, doc.count + 1 |
Important considerations
- Edit-by-function is an asynchronous operation. It returns a task that you can monitor like any other indexing task.
- The function runs on every document matching the filter (or all documents if no filter is provided). Test on a small subset first using a restrictive filter.
- Edit-by-function tasks cannot be autobatched with other task types. Each edit operation runs as its own batch.
- If the function contains a syntax error or runtime error, the task will fail. Check the task’s
errorfield for details. - Editing documents triggers a reindex of the modified documents.
Next steps
Filter expression syntax
Learn the full filter syntax for targeting documents.
Rhai language reference
Explore the complete Rhai scripting language documentation.
Monitor tasks
Track the progress of your edit-by-function operations.
Add and update documents
Other ways to modify documents in Meilisearch.