Query Cortex Search Directly in SQL with the CORTEX_SEARCH_RETRIEVE Function
The CORTEX_SEARCH_RETRIEVE table function lets you query Cortex Search services directly in SQL with low latency, supporting single queries or row-by-row lookups with LATERAL joins for RAG and chatbots.
What changed
Snowflake has released the CORTEX_SEARCH_RETRIEVE table function in Public Preview. This function enables low-latency Cortex Search queries directly within SQL statements.
Use cases
Imagine you have a table containing customer support tickets and need to fetch the most relevant knowledge base articles in real time to ground an LLM response or enrich data, without leaving SQL.
This function enables you to:
- Retrieve contextual text for RAG pipelines and chatbots using SQL.
- Run row-by-row semantic lookups over a table using
LATERALjoins. - Combine search results with downstream analytical tables using
JOINandGROUP BY.
How to use
The general syntax uses the table function wrapper:
SELECT *
FROM TABLE(CORTEX_SEARCH_RETRIEVE(
service_name => '<database>.<schema>.<cortex_search_service>',
query => '<query_string>',
limit => 10
));
The following example runs a single semantic search against a configured search service:
SELECT *
FROM TABLE(CORTEX_SEARCH_RETRIEVE(
service_name => 'my_db.my_schema.my_search_service',
query => 'how do I file an expense report',
limit => 5
));
What happens in this code:
- The function targets
my_search_servicein the specified database and schema. - The query string is processed against the existing serving layer of the search service.
- The statement outputs the top 5 matching records with their associated metadata.
To query the service once per record in a table, use a LATERAL join:
SELECT
q.question_id,
q.question_text,
cs.article_title,
cs.METADATA$RESULT_DETAIL
FROM user_questions AS q,
LATERAL CORTEX_SEARCH_RETRIEVE(
service_name => 'my_db.my_schema.my_search_service',
query => q.question_text,
limit => 2
) AS cs;
What happens in this code:
- The
LATERALjoin executes the search function once for each row inuser_questions. - The value in
q.question_textacts as the dynamic query string. - Up to 2 matching records are retrieved for each row without aborting the batch if an isolated row fails.
What it returns
The function returns the indexed source columns (excluding VECTOR data types) alongside the following metadata fields:
| Field | Description |
|---|---|
METADATA$REQUEST_ID | Unique request identifier useful for logging and debugging queries. |
METADATA$ERROR | Error string if the search failed for that specific row; NULL on success. |
METADATA$RESULT_DETAIL | JSON string containing scoring metrics and score components. |
Limitations and requirements
- Maturity: Public Preview across all accounts in regions where Cortex Search is available.
- Permissions: The executing role requires the
USAGEprivilege on the Cortex Search Service. - Serving state: The serving layer must be running. If it was suspended due to inactivity, the first query resumes it, introducing initial latency.
- Workload design: Designed for low-latency queries on single items or small sets of rows. For high-volume entity resolution or batch jobs, use
CORTEX_SEARCH_BATCHinstead. - Data sharing: Not supported for shared services accessed via listings or secure data shares.
- snowflake cortex
- cortex search
- rag
- sql
Retrieve Cortex Search (Public Preview)
Summary and explanation generated by AI from the official release note. When in doubt, the platform's official documentation prevails.
Related
- SnowflakeGA
Custom runtime images now generally available for ML Jobs
Build and register your own container images as Custom Runtime Environments (CREs) to use in ML Jobs on Snowflake.
snowflake ml · ml jobs - SnowflakeGA
Data Clean Rooms updates featuring Python 3.13 and new edits
Snowflake Data Clean Rooms updates bring Python 3.13, general availability for collaboration edits, and fixes for security scan timeouts.
data clean rooms · python - SnowflakePublic Preview
Cortex AI Gateway expands to Azure, GCP, and new AWS regions in Snowflake
Cortex AI Gateway is now available in Azure, GCP, and AWS New Zealand commercial regions, centralizing AI model governance, spend tracking, and observability.
snowflake cortex · cortex ai gateway