Skip to main content
Meilisearch allows you to edit documents in place using Rhai scripting functions. Instead of fetching documents, modifying them externally, and re-indexing, you write a short function that Meilisearch applies to each matching document.
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 firstName and lastName into fullName)
  • Conditional edits: update only documents matching a filter expression

Enable the feature

Send a PATCH request to /experimental-features:

Basic usage

Send a POST 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:
This converts the title field to uppercase for every document in the movies index. The operation is asynchronous and returns a task object.

Filter target documents

Use the filter parameter to apply the function only to documents matching a filter expression:
This sets 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

The context parameter lets you pass external data into your function. Access it through the context variable:
This applies a 20% discount to specific products in the electronics category.

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: For the full language reference, see the Rhai Book.

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 error field 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.