Snowflake Cortex Search
Keyword search fails when users phrase a question differently from the text. A person types "money back" while the manual says "refund policy". Cortex Search combines keyword matching with meaning-based search, so the best passages surface even when the words differ. It forms the retrieval half of question answering over private documents.
The Librarian Comparison
A good librarian understands what a visitor means, not only the words spoken. Asked for "books about getting your cash back", the librarian points to the shelf on returns and refunds. Cortex Search plays the librarian. It keeps an index of your text, matches the question by meaning and by keywords, and returns the best passages first.
HYBRID SEARCH
=============
Question: "How do I get my money back?"
|
+-------------------+--------------------+
v v v
KEYWORD MATCH MEANING MATCH RERANKING
finds "money", finds "refund sorts combined
"back" policy" by meaning results by relevance
\ | /
+------------------+-------------------+
v
Top passages: "Refund policy",
"Returns within 30 days"Preparing the Source Table
Search works best on small passages, so split long documents into chunks of a few hundred words. Each row holds one chunk and some descriptive columns.
CREATE OR REPLACE TABLE help_docs (
doc_title STRING,
category STRING,
chunk STRING
);Creating a Search Service
CREATE OR REPLACE CORTEX SEARCH SERVICE help_search
ON chunk
ATTRIBUTES doc_title, category
WAREHOUSE = search_wh
TARGET_LAG = '1 hour'
AS (
SELECT chunk, doc_title, category
FROM help_docs
);The ON column holds the searchable text. ATTRIBUTES lists extra columns for filtering and display. TARGET_LAG tells Snowflake how fresh the index should stay as the source table changes.
Querying the Service
SELECT SNOWFLAKE.CORTEX.SEARCH_PREVIEW(
'help_search',
'{
"query": "how do I get my money back",
"columns": ["doc_title", "chunk"],
"filter": {"@eq": {"category": "billing"}},
"limit": 3
}'
);The result is a JSON document with the top three matching chunks. Applications call the same service through a REST API or a Python client for production use.
Retrieval Augmented Generation
RAG means retrieve first and generate second. Search finds the right passages, and a language model writes an answer based on them. The model answers from your documents rather than guessing from general knowledge.
RAG FLOW
========
User question
|
v
CORTEX SEARCH ---- top 3 passages ---+
v
AI_COMPLETE (prompt = question + passages)
|
v
Answer grounded in your documents
WITH hits AS (
SELECT PARSE_JSON(SNOWFLAKE.CORTEX.SEARCH_PREVIEW(
'help_search',
'{"query":"how do I get my money back","columns":["chunk"],"limit":3}'
)):results AS results
)
SELECT AI_COMPLETE(
'llama3.1-8b',
'Answer using only the passages below. If the answer is missing, say so. Passages: '
|| results::STRING
|| ' Question: how do I get my money back'
) AS answer
FROM hits;The instruction "answer using only the passages" reduces made-up answers.
Chunking Tips
- Split documents at headings or paragraphs, with a few hundred words per chunk.
- Overlap neighboring chunks slightly so sentences at the edges keep context.
- Store the document title and section name with each chunk for citations.
- Remove repeated headers, footers, and page numbers.
Operating the Service
SHOW CORTEX SEARCH SERVICES;
DESCRIBE CORTEX SEARCH SERVICE help_search;
ALTER CORTEX SEARCH SERVICE help_search SUSPEND;
ALTER CORTEX SEARCH SERVICE help_search RESUME;Costs include warehouse time for refreshes, serving charges while the service runs, and storage for the index. Suspend services that nobody uses, and right-size the warehouse.
Access Control
Grant USAGE on the search service to the roles that may query it. Remember that results come from the indexed table, so index only text that every allowed user may read. Use separate services for content with different audiences.
A Practical Scenario
A software company stores two thousand help articles in Snowflake. Support agents waste time scanning keyword results. A data engineer chunks the articles, creates a search service with a category attribute, and connects it to a small chat page. Agents type questions in everyday words and receive the three best passages, often resolving tickets in half the time.
Frequently Asked Questions
How large can the source table be?
Services handle large tables, with limits stated in the documentation. Chunk long documents so each row stays focused.
Key Points
- Cortex Search blends keyword and meaning-based search with reranking.
- A search service indexes one text column plus filter attributes.
- TARGET_LAG keeps the index fresh as source data changes.
- RAG combines search results with AI_COMPLETE for grounded answers.
- Good chunking drives answer quality.
- Grant service access carefully and suspend unused services.
