Create and query an embeddings index (deprecated)
To get started with the Embeddings Index API, you first need to create an index.
Deprecation notice
The Embeddings Index API is deprecated and will be sunset in a future release. We recommend migrating to the new Embeddings feature, now natively available within Sanity datasets.
The new Embeddings feature offers a more integrated experience with improved performance and full support going forward. No new features or fixes will be made to this package.
Migrate today: Dataset Embeddings documentation
If you have questions or need migration support, please open a discussion or reach out in the Sanity Community.
You can create an embeddings index in one of the following ways:
- With the Embeddings Index CLI.
- With the Embeddings Index UI for Sanity Studio.
- With the Embeddings Index HTTP API.
This guide walks you through configuring an embeddings index for a Sanity project using the Embeddings Index CLI.
Prerequisites
- The Sanity CLI. The CLI ships with the main Sanity package. The Embeddings Index CLI runs its commands through the Sanity CLI and authenticates with your Sanity login. Run
sanity loginbefore you start, or set aSANITY_EMBEDDINGS_TOKENenvironment variable. - Run the commands from the root directory of a Sanity project that contains a
sanity.cli.tsorsanity.cli.jsfile. - A user or token with permission to create webhooks, create tokens, and read datasets. The
createcommand uses these permissions to set up a robot token and a webhook that keeps the index in sync with the dataset.
Gotcha
The Embeddings Index API doesn't support dataset aliases. This means that you have to use the real dataset name in all requests.
Create an embeddings index
To create an embeddings index, open a terminal session, and then run the command that matches how you plan to create an index:
# Create an embeddings index by passing arguments
npx @sanity/embeddings-index-cli create --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"
# Alternatively, create an embeddings index by passing a JSON manifest
npx @sanity/embeddings-index-cli create --manifest MANIFEST_FILE_NAME.json# Create an embeddings index by passing arguments
pnpm dlx @sanity/embeddings-index-cli create --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"
# Alternatively, create an embeddings index by passing a JSON manifest
pnpm dlx @sanity/embeddings-index-cli create --manifest MANIFEST_FILE_NAME.json# Create an embeddings index by passing arguments
yarn dlx @sanity/embeddings-index-cli create --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"
# Alternatively, create an embeddings index by passing a JSON manifest
yarn dlx @sanity/embeddings-index-cli create --manifest MANIFEST_FILE_NAME.json# Create an embeddings index by passing arguments
bunx @sanity/embeddings-index-cli create --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"
# Alternatively, create an embeddings index by passing a JSON manifest
bunx @sanity/embeddings-index-cli create --manifest MANIFEST_FILE_NAME.jsonCreating an index can take time, depending on the number of existing documents and the indexer load.
The commands in this guide use npx to run the CLI, but you can also install it in your project as a development dependency and use the embeddings-index command as shown in the CLI readme.
You can define the configuration of an embeddings index in one of the following ways:
- By passing configuration arguments when you create the index in the CLI.
- By storing the configuration details in a JSON manifest file.
Define the index in the CLI
To define a new embeddings index in the root directory of a Sanity project, pass the following arguments with the embeddings-index create command:
--indexName: Assign a descriptive name to the index.--dataset: Specify the name of an existing dataset. This is the target dataset to index. If you omit it, the CLI uses the dataset in yoursanity.cli.tsorsanity.cli.jsfile.--filter: Specify the filtering criteria to include in the index only the selected subset of documents from the dataset.
The filter must be a valid GROQ filter without the square brackets that wrap the filter in a full query such as*[_type=='tutorial'].
Example:_type=='tutorial'--projection: Specify the projection criteria to include in the index only the selected subset of properties from the filtered documents.
The projection must be a valid GROQ projection, including curly brackets.
Example:{title, author}
Alternatively, you can create an embeddings index by passing a JSON manifest file with the --manifest argument:
--manifest MANIFEST_FILE_NAME.json
Example
# Create embeddings index with arguments
# 'filter' has no '[]' square brackets
# 'projection' keeps '{}' curly brackets
npx @sanity/embeddings-index-cli create --indexName "my-embeddings-index" --dataset "production" --filter "_type=='myDocumentType'" --projection "{...}"
# Create embeddings index with JSON manifest
# The JSON manifest is in the project root directory
npx @sanity/embeddings-index-cli create --manifest embeddings-index-manifest.json# Create embeddings index with arguments
# 'filter' has no '[]' square brackets
# 'projection' keeps '{}' curly brackets
pnpm dlx @sanity/embeddings-index-cli create --indexName "my-embeddings-index" --dataset "production" --filter "_type=='myDocumentType'" --projection "{...}"
# Create embeddings index with JSON manifest
# The JSON manifest is in the project root directory
pnpm dlx @sanity/embeddings-index-cli create --manifest embeddings-index-manifest.json# Create embeddings index with arguments
# 'filter' has no '[]' square brackets
# 'projection' keeps '{}' curly brackets
yarn dlx @sanity/embeddings-index-cli create --indexName "my-embeddings-index" --dataset "production" --filter "_type=='myDocumentType'" --projection "{...}"
# Create embeddings index with JSON manifest
# The JSON manifest is in the project root directory
yarn dlx @sanity/embeddings-index-cli create --manifest embeddings-index-manifest.json# Create embeddings index with arguments
# 'filter' has no '[]' square brackets
# 'projection' keeps '{}' curly brackets
bunx @sanity/embeddings-index-cli create --indexName "my-embeddings-index" --dataset "production" --filter "_type=='myDocumentType'" --projection "{...}"
# Create embeddings index with JSON manifest
# The JSON manifest is in the project root directory
bunx @sanity/embeddings-index-cli create --manifest embeddings-index-manifest.jsonDefine the index in a JSON manifest
To store, reuse, and manage embeddings indexes with source code control and versioning, define their configuration in a JSON manifest file. Save the manifest file, for example manifest.json, in the root directory of your Sanity project.
A JSON manifest file defining an embeddings index must contain the following required fields:
{
indexName: string,
dataset: string,
filter: string,
projection: string
}Example
{
"indexName": "my-embeddings-index",
"dataset": "production",
"filter": "_type=='myType'",
"projection": "{...}"
}The manifest must be valid JSON, so it can't contain comments. The filter value has no surrounding square brackets, and the projection value keeps its curly brackets.
To create a JSON manifest file, invoke the manifest command:
npx @sanity/embeddings-index-cli manifest --out manifest.json --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"pnpm dlx @sanity/embeddings-index-cli manifest --out manifest.json --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"yarn dlx @sanity/embeddings-index-cli manifest --out manifest.json --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"bunx @sanity/embeddings-index-cli manifest --out manifest.json --indexName "INDEX_NAME" --dataset "DATASET_NAME" --filter "GROQ_FILTER" --projection "GROQ_PROJECTION"To change the configuration of an existing index, run the delete command, and then run the create command again.
Check the status of an embeddings index
You can check the status of your embeddings indexes to monitor the creation progress or the completeness of the indexes.
To check the status of all embeddings indexes in a dataset, run the following command. Add --dataset to target a dataset other than the one in your Sanity CLI configuration:
npx @sanity/embeddings-index-cli listpnpm dlx @sanity/embeddings-index-cli listyarn dlx @sanity/embeddings-index-cli listbunx @sanity/embeddings-index-cli listTo check the status of a specific embeddings index in a Sanity project, run:
npx @sanity/embeddings-index-cli get --indexName "INDEX_NAME"pnpm dlx @sanity/embeddings-index-cli get --indexName "INDEX_NAME"yarn dlx @sanity/embeddings-index-cli get --indexName "INDEX_NAME"bunx @sanity/embeddings-index-cli get --indexName "INDEX_NAME"Query an index
To query an index, make a request with the Embeddings Index HTTP API.
import { createClient } from '@sanity/client'
const client = createClient({
projectId: '<your-project-id>',
dataset: 'DATASET_NAME',
apiVersion: 'vX', // vX is required for embeddings API calls
token: process.env.SANITY_API_TOKEN,
useCdn: false,
});
const dataset = 'DATASET_NAME'
const indexName = 'INDEX_NAME'
const response = await client.request({
url: `/embeddings-index/query/${dataset}/${indexName}`,
method: 'POST',
body: {
query: 'sci-fi adventure with cowboys and aliens',
maxResults: 10,
filter: {
type: ['summary', 'synopsis', 'userReview'],
},
},
})curl --request POST 'https://<your-project-id>.api.sanity.io/vX/embeddings-index/query/DATASET_NAME/INDEX_NAME' \
--header 'Authorization: Bearer API_TOKEN' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--data '{
"query": "sci-fi adventure with cowboys and aliens",
"maxResults": 10,
"filter": {
"type": ["summary", "synopsis", "userReview"]
}
}'This example uses the query endpoint to search against an index and filter by document type. Learn more about querying the API.
Next steps
- To move off the deprecated Embeddings Index API, see Dataset Embeddings.
- For every endpoint and command option, see the Embeddings Index API reference and the Embeddings Index CLI reference.
Was this page helpful?
Feedback submitted, thanks for helping us improve the docs. Have you tried these alternatives?