Skip to main content
How to
Document relations
Automatically enrich search results with related data from other indexes using foreign keys.
Foreign keys let you link documents across indexes so that search results are automatically enriched with related data. Instead of duplicating information, you store it once in a dedicated index and reference it by ID.
For example, a
This tells Meilisearch that the
Without foreign keys, a result would look like this:
With foreign keys configured, the same result is automatically hydrated:
movies index can reference actors by ID. When you search for movies, Meilisearch automatically replaces the actor IDs with full actor documents from the actors index. This approach also works with multi-search when querying across related indexes.
Step 1: Enable the experimental feature
Foreign keys must be activated through the experimental features endpoint before you can use them:Step 2: Create your related index
Add documents to the index you want to reference. In this example, create anactors index with actor data:
Step 3: Configure foreign keys in the main index
Use the settings endpoint to define which fields contain foreign references and which index they point to:actors field in the movies index contains IDs that reference documents in the actors index.
Step 4: Add documents with foreign IDs
Add documents to your main index. Use arrays of IDs for the foreign key field:Step 5: Search and see hydrated results
When you search themovies index, Meilisearch automatically replaces foreign IDs with full documents from the referenced index:
Limitations
- Experimental: This feature may change or be removed in future versions.
- No remote sharding: Foreign keys are not supported in environments using remote sharding.
- One direction: Hydration works from the main index to the referenced index. The referenced index does not automatically link back.
Next steps
Foreign keys settings API
Full API reference for foreign key settings
Experimental features API
Enable and manage experimental features
Indexing overview
Learn more about how indexing works in Meilisearch
