Basic operations

This page covers the main Search Index operations: define fields, create an index, ingest and query documents, update the schema, and delete data. The Python examples use the cookbook_docs schema from Custom fields. The toolkit is async, so run them inside an async function. The REST examples use a separate support_articles index. For a complete runnable script, see the quickstart.

Custom fields

Custom fields

Every field a Document or DocumentChunk subclass adds to the base models becomes a typed column on the index. Document fields describe a whole source document; chunk fields describe one retrievable passage. The backing table is flat, so document fields are copied onto every chunk row, and a single filter can combine document fields (such as category) and chunk fields (such as page).

The toolkit infers the wire type from the annotation, and MistralSearchIndexField overrides any part of it:

AnnotationWire typeIndexed by default
strtextyes
StrEnumkeywordyes
intlongno
floatfloatno
boolboolno
datetimetimestampno
list[T] of the preceding typesT[]no
dict, a pydantic modeljsonno
from typing import Annotated, Sequence

from mistralai.search.toolkit.document import Document, DocumentChunk
from mistralai.search.toolkit.embedding import MistralEmbeddingPreset
from mistralai.search.toolkit.mistral_search_index.schema import (
    MistralSearchIndexCollectionSchema,
    MistralSearchIndexField,
    MistralSearchIndexFieldType,
)

class ReportChunk(DocumentChunk):
    # Chunk metadata is stored as an opaque JSON blob and cannot be filtered on;
    # a field of its own makes the page number a filterable column.
    page: Annotated[int | None, MistralSearchIndexField(indexed=True)] = None

class ReportDocument(Document):
    title: str  # text: full-text searchable
    category: Annotated[str, MistralSearchIndexField(type=MistralSearchIndexFieldType.KEYWORD, indexed=True)]
    page_count: int  # long: Range filters
    published_year: Annotated[int | None, MistralSearchIndexField(type=MistralSearchIndexFieldType.INT, indexed=True)] = None
    keywords: Annotated[list[str], MistralSearchIndexField(type=MistralSearchIndexFieldType.KEYWORD_ARRAY)] = []
    chunks: Sequence[ReportChunk] = []

schema = MistralSearchIndexCollectionSchema.from_document_type(
    collection_name="cookbook_docs",
    document_type=ReportDocument,
    embedding_model=MistralEmbeddingPreset.MISTRAL_EMBED_DIM_1024,
)

Things to know about custom fields:

  • Optional means nullable. A field whose annotation includes None is optional; every other field is required, and the index rejects a document without it. A field with a default but no None, such as keywords in the example, is still required. Set MistralSearchIndexField(required=...) to override this; over REST, required defaults to false.
  • indexed builds a database index (btree for scalars, full-text for text). Filters work without one; set indexed=True on fields you filter on often. Keyword retrieval works only on indexed text fields.
  • Filterable fields are scalars: text, keyword, int, long, float, and timestamp. Array and json fields are stored and returned with every hit, but you can't filter on them. You can't filter on bool fields either.
  • Names are constrained. Field names must match [a-z][a-z0-9]*(_[a-z0-9]+)* and be at most 32 characters. Index names follow the same rule. A custom field must not repeat a key of the same chunk's metadata (paged OCR chunks carry page_number in their metadata, so the chunk field in the example is named page), and the system fields and chunks are reserved: declaring one as a custom field returns a 422.
  • Document.content is not stored. Only its chunks are. Chunk ids derive from (source_id, locator), so two chunks of one document must have distinct locators.
  • Schemas evolve additively. MistralSearchIndexService.update_index can add optional fields or turn on indexed for an existing field. Removing or retyping a field, moving it between document and chunk scope, un-indexing it, or adding a required one is refused with a 422. To make one of these changes, recreate the index. An index created before custom fields existed has none of them: recreate it and re-ingest.
  • PUT replaces the schema. Over REST, send the whole schema on every update, not only the changed fields.

System fields

System fields

The canonical Document / DocumentChunk fields map onto the index's own fields:

ScopeWire fieldFrom the toolkitNotes
documentsource_idDocument.source_idIdentifies the source document.
documentdocument_idnot sentServer-computed from source_id, same as Document.id.
documentdocument_metadataDocument.metadataDefaults to {}.
chunkidnot sentServer-computed from (source_id, locator), same as DocumentChunk.id.
chunklocatorDocumentChunk.locatorPosition in the source, for example char:0-512.
chunkstart_offset, end_offsetsame
chunkcontentDocumentChunk.contentThe passage text.
chunkcontent_embeddingDocumentChunk.embeddingRequired: ingestion never embeds, and an unembedded chunk raises before the request.
chunkchunk_typeDocumentChunk.chunk_typeDefaults to content.
chunkparent_refDocumentChunk.parent_refOptional.
chunkmetadataDocumentChunk.metadataDefaults to {}.

Create an index

Create an index

MistralSearchIndexService manages the index lifecycle, and its get_search_index method returns a MistralSearchIndex for document and query operations. The same API is available in the mistralai SDK under client.beta.context.search_indexes.

Note

To create an index, you need the workspace create_search_indexes permission. Without it, or when the organization reaches its index limit, the request returns 403. The creator becomes the index owner, with the see, query, ingest, modify, and delete permissions on it. If you don't have a permission on an existing index, the API returns 404, not 403.

With the toolkit, derive the schema from your document type and wait for provisioning:

import asyncio

from mistralai.search.toolkit.mistral_search_index import MistralSearchIndexService
from mistralai.search.toolkit.mistral_search_index.models import MistralSearchIndexStatus

service = MistralSearchIndexService()
info = await service.create_index(schema)
while info.status == MistralSearchIndexStatus.PROVISIONING:
    await asyncio.sleep(1)
    info = await service.get_index_info("cookbook_docs")
if info.status != MistralSearchIndexStatus.READY:
    raise RuntimeError(f"provisioning failed: {info.status_message}")

You can't change the embedding model after creation: it determines the vector column type and the vector operator class.

Over REST, the same call is POST /v1/context/search-indexes with the schema declared inline:

curl -X POST https://api.mistral.ai/v1/context/search-indexes \
  -H "Authorization: Bearer $MISTRAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "name": "support_articles",
    "schema": {
      "document_fields": {
        "title": { "type": "text", "indexed": true },
        "product": { "type": "keyword", "indexed": true }
      },
      "chunk_fields": {
        "page_number": { "type": "int", "indexed": true }
      }
    },
    "config": {
      "embedding": { "type": "mistral", "name": "mistral-embed", "dimensions": 1024 }
    }
  }'

The following table lists the lifecycle, document, and search endpoints:

OperationEndpoint
Create an indexPOST /v1/context/search-indexes
List indexesGET /v1/context/search-indexes
Get an indexGET /v1/context/search-indexes/{index_name}
Update the schema (additive only)PUT /v1/context/search-indexes/{index_name}
Delete an indexDELETE /v1/context/search-indexes/{index_name}
Ingest documentsPOST /v1/context/search-indexes/{index_name}/documents
Delete documentsDELETE /v1/context/search-indexes/{index_name}/documents
SearchPOST /v1/context/search-indexes/{index_name}/search

Create, update, and delete are asynchronous and return 202. Deletion moves the index to deleting; the name stays reserved until it completes, and only then can you recreate it. Every document and query call on an index that is not ready returns 409.

Ingest documents

Ingest documents

Ingestion doesn't compute embeddings: every chunk carries its own, and the toolkit raises an error before it sends a chunk without one. A document's source_id is its identity. If you ingest the same source_id again, the new version replaces the document: its chunks are deleted and the new ones inserted in one transaction. In a toolkit ingestion pipeline, source_id defaults to the file path or name, so ingesting the same file again updates it instead of adding a copy.

Over REST, send up to 100 documents per request to POST /v1/context/search-indexes/{index_name}/documents. Each document carries its custom fields and up to 1,000 chunks. Each chunk needs a complete embedding with the dimensions configured on the index. The response is 200, with an accepted or rejected entry per document, so check each entry.

For the support_articles index, the following script embeds one article with the embeddings API, then ingests it. It uses jq to build the request bodies:

TEXT="Rotate an API key from the console under Workspace > API keys."
EMBEDDING=$(curl -s https://api.mistral.ai/v1/embeddings \
  -H "Authorization: Bearer $MISTRAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d "$(jq -n --arg text "$TEXT" '{model: "mistral-embed", input: [$text]}')" \
  | jq -c '.data[0].embedding')

curl -X POST https://api.mistral.ai/v1/context/search-indexes/support_articles/documents \
  -H "Authorization: Bearer $MISTRAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d "$(jq -n --arg text "$TEXT" --argjson embedding "$EMBEDDING" '{
    documents: [{
      source_id: "s3://docs/api-keys.md",
      title: "Managing API keys",
      product: "console",
      chunks: [{
        locator: "char:0-\($text | length)",
        start_offset: 0,
        end_offset: ($text | length),
        content: $text,
        content_embedding: $embedding,
        page_number: 1
      }]
    }]
  }')"

The response reports each document by its position in the request:

{
  "accepted": 1,
  "rejected": 0,
  "results": [
    {
      "status": "accepted",
      "position": 0,
      "document_id": "4bf49690-f3c9-5045-a170-9eaea02f5775",
      "chunk_count": 1
    }
  ]
}

A rejected entry carries errors instead, for example {"location": ["chunks", 0, "content_embedding"], "type": "missing", "message": "field is required"}.

With the toolkit, embed the content and ingest the document:

from mistralai.search.toolkit.document import SourceId
from mistralai.search.toolkit.embedding import MistralEmbedder

text = "Rotate an API key from the console under Workspace > API keys."
embedder = MistralEmbedder(model_name="mistral-embed")
embedding = (await embedder.embed([text])).embeddings[0]
article = ReportDocument(
    source_id=SourceId("s3://docs/api-keys.md"),
    content=text,
    title="Managing API keys",
    category="console",
    page_count=1,
    keywords=["api", "keys"],
    chunks=[ReportChunk(
        source_id=SourceId("s3://docs/api-keys.md"),
        locator=f"char:0-{len(text)}",
        start_offset=0,
        end_offset=len(text),
        content=text,
        embedding=embedding,
        page=1,
    )],
)
index = await service.get_search_index("cookbook_docs")
await index.index_document(article)

Query an index

Query an index

Query with POST /v1/context/search-indexes/{index_name}/search, or from Python with MistralSearchIndex.retrieve, which takes the retriever query DSL:

RetrieverPurpose
NearestNeighbourRetrieverApproximate-nearest-neighbour over content_embedding. For a mistral embedding model, pass query and the server embeds it; a custom model has no server-side embedder, so pass query_embedding yourself.
KeywordRetrieverFull-text match on content, or another indexed text field via field.
RRFRetrieverFuses its children's rankings with reciprocal rank fusion. weights and rank_constant tune it, and its filter is ANDed onto every child.

Filters are Equal, In, Range (gt / gte / lt / lte), And, Or, and Not, over any filterable field. Every retriever takes a filter, not only RRFRetriever. top_k defaults to 20 and is capped at 100 on the top-level retriever, and exceeding it returns 400.

Children of an RRFRetriever can set top_k up to 1,000. Each child's top_k must be at least the parent's.

from mistralai.search.toolkit.mistral_search_index.retriever import (
    Equal,
    KeywordRetriever,
    NearestNeighbourRetriever,
    RRFRetriever,
)

results = await index.retrieve(
    RRFRetriever(
        top_k=5,
        filter=Equal(field="category", value="console"),
        retrievers=[
            KeywordRetriever(query="how do I rotate an api key", top_k=5),
            NearestNeighbourRetriever(query="how do I rotate an api key", top_k=5),
        ],
    )
)

For the support_articles index, query over REST:

curl -X POST https://api.mistral.ai/v1/context/search-indexes/support_articles/search \
  -H "Authorization: Bearer $MISTRAL_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "retriever": {
      "type": "rrf",
      "top_k": 5,
      "filter": { "type": "equal", "field": "product", "value": "console" },
      "retrievers": [
        { "type": "keyword", "query": "how do I rotate an api key", "top_k": 5 },
        { "type": "nearest_neighbour", "query": "how do I rotate an api key", "top_k": 5 }
      ]
    }
  }'

MistralSearchIndex.search(VectorSearchQuery) builds the retriever for you: nearest-neighbour when the query has no text, and RRF over keyword and nearest-neighbour when it does. It matches content only, and returns custom fields on every hit. To match keywords on a custom field or to filter on your fields, use the retriever DSL.

Each result is a SearchResult whose chunk carries the document-scope fields alongside the chunk fields. Embeddings are never returned. A higher score is better: 1 - distance for cosine, -distance for l2 and inner_product, a keyword relevance score for KeywordRetriever, and the fused RRF score for RRFRetriever. distance is the raw vector distance, or None for keyword and RRF results.

A search index is also a navigable index: navigate, read, grep, and get_chunk walk a source document in reading order instead of by relevance. These are the same primitives behind Agentic Search, and they are exposed on the index endpoints (POST .../navigate, POST .../read, POST .../grep, GET .../chunks/{chunk_id}).

Update the schema

Update the schema

Declare the expanded schema and call update_index. Schema updates are additive: keep existing fields. Wait for the index to return to READY before relying on the added fields.

class UpdatedReportDocument(ReportDocument):
    owner: str | None = None

updated_schema = MistralSearchIndexCollectionSchema.from_document_type(
    collection_name="cookbook_docs",
    document_type=UpdatedReportDocument,
    embedding_model=MistralEmbeddingPreset.MISTRAL_EMBED_DIM_1024,
)
await service.update_index(updated_schema)
Delete documents

Delete documents

To delete a document and its chunks, pass its document ID, not its source ID. You can't undo a deletion, but you can ingest the document again.

await index.delete_document(article.id)

If no document matches the ID, delete_document raises DocumentNotFoundError. Over REST, DELETE /v1/context/search-indexes/{index_name}/documents takes up to 100 document_ids and returns the IDs it deleted, plus the IDs it didn't find in missing.

Delete an index

Delete an index

Warning

Deleting an index removes all its documents and chunks, and you can't undo it. Check the index name before you call delete_index.

Deletion is asynchronous: the index moves to DELETING, then requests for it return 404.

await service.delete_index("cookbook_docs")