AI Search - Custom metadata filtering for AI Search
Key Points
- Define up to 5 custom metadata fields
- Supports text, number, and boolean types
- Filter searches using custom fields (e.g., $gte)
Summary
AI Search now lets you define and use custom metadata fields to filter retrieval results. You can create up to 5 custom fields per AI Search instance (types: text, number, boolean), attach metadata at ingest, and include those fields in search filters alongside built-in attributes like folder and timestamp.
Key Points
- Define custom metadata when creating an instance via the API using the
custom_metadataarray (max 5 fields). Example field entry:{"field_name": "category", "data_type": "text"}. - Supported data types:
text,number,boolean. - Attach metadata depending on source:
- R2: set S3-compatible custom headers
x-amz-meta-*when uploading objects. - Website: add
<meta>tags to HTML pages.
- R2: set S3-compatible custom headers
- Filter search results by including custom fields in
ai_search_options.retrieval.filters. Example filter snippet:"filters": { "category": "documentation", "version": { "$gte": 2.0 } }. - API calls require
Authorization: Bearer {API_TOKEN}andContent-Type: application/jsonheaders.
Practical notes for engineers
- Each AI Search instance has a hard limit of 5 custom metadata fields; plan schema accordingly.
- Use numeric operators (e.g.,
$gte) onnumberfields and boolean values forbooleanfields. - Combine custom filters with existing filters (e.g.,
folder,timestamp) to narrow retrievals. - Example endpoints: create instance
POST /accounts/{ACCOUNT_ID}/ai-search/instanceswithcustom_metadataarray; searchPOST /accounts/{ACCOUNT_ID}/ai-search/instances/{NAME}/searchwithai_search_options.retrieval.filters.
Where to go next
Review your data ingestion path (R2 uploads or website pages) to ensure metadata is being written correctly, then update search queries to use the new filter fields.