Course contents

SnowPro Core COF-C03 Study GuidePart 7: AI featuresCortex Search & Cortex Analyst

Cortex Search & Cortex Analyst

Cortex Search & Cortex Analyst

Definition

Two managed services answer questions instead of transforming text. Cortex Search retrieves the most relevant passages from unstructured text, the retrieval half of a RAG application. Cortex Analyst answers business questions over structured tables through a semantic view, returning the SQL it generated, which the app then runs on a warehouse under the user's role. Unstructured versus structured is the entire decision.

Two question-answering services, chosen by where the answer lives. Top: a question whose answer is written in company documents goes to Cortex Search, which returns the most relevant passages of text. Bottom: a question whose answer is a number in tables goes to Cortex Analyst, which writes SQL from a semantic view and returns that SQL for the app to run.THE ANSWER IS WRITTEN IN DOCUMENTS (TEXT)THE ANSWER IS A NUMBER IN TABLESQUESTION"How do we handle VATon partial refunds?"SERVICECortex Searchsearches indexed textRESULTRelevant passagesfrom 40,000 policy docsQUESTION"What were Q2 refundsby region?"SERVICECortex Analystwrites SQL from asemantic viewRESULTGenerated SQLthe app runs it

THE ANSWER IS WRITTEN IN DOCUMENTS (TEXT)

QUESTION"How do we handle VATon partial refunds?"

SERVICECortex Searchsearches indexed text

RESULTRelevant passagesfrom 40,000 policy docs

THE ANSWER IS A NUMBER IN TABLES

QUESTION"What were Q2 refundsby region?"

SERVICECortex Analystwrites SQL from a semantic view

RESULTGenerated SQLthe app runs it

Figure 7.2 Pick the service by where the answer lives: an answer written in documents comes from Cortex Search, a number held in tables comes from Cortex Analyst. Questions and document count are illustrative.

Cortex Search is created against a text column and maintains its own hybrid index (vector similarity plus keyword matching, then semantic reranking), refreshed on a TARGET_LAG you declare, the same freshness contract dynamic tables use. The warehouse named in the service runs each refresh, and change tracking must be on for the source tables. Youness watches it for a governance reason that's easy to miss: searches run with the service owner's rights. Anyone with USAGE on the service (and on its database and schema) can retrieve every passage it indexed, whatever their own privileges or row access policies on POLICY_DOCS. Control access with the source query and with who gets USAGE on the service.

The bill has four meters: the warehouse that builds and refreshes the index (nothing when a refresh finds no changes), tokens to embed new or changed rows, serving compute per GB per month of indexed data even when nobody searches, and storage.

CREATE CORTEX SEARCH SERVICE policy_search
  ON body
  ATTRIBUTES doc_type, country
  WAREHOUSE = build_wh
  TARGET_LAG = '1 hour'
  AS SELECT body, doc_type, country FROM policy_docs;
Cortex Search as the retrieval half of a chat-over-documents app. Left, build time: warehouse BUILD_WH runs the service's source query on POLICY_DOCS with the service owner's privileges and keeps the index fresh to the target lag. The index is billed per GB per month while the service exists. Right, question time: Alice's app sends her question to the service, gets the top passages back, and AI_COMPLETE writes an answer from them. Anyone with USAGE on the service can retrieve everything it indexed, whatever their own privileges on POLICY_DOCS.BUILD TIME (OWNER)QUESTION TIME (ALICE)TABLEPOLICY_DOCStext columnWAREHOUSEBUILD_WHruns the source queryas the service ownerSEARCH SERVICEPOLICY_SEARCHindex of passages,fresh to TARGET_LAGindex billed per GB per month,even when nobody searchesAPPAlice’s chat appneeds USAGE onthe serviceRESULTTop passagesmost relevant textAI MODELAI_COMPLETEquestion + passagesSECURITYOwner’s rights: USAGE on the service exposes everything it indexedFilter the source query, or build one service per audiencereadsquestionanswer

BUILD TIME (OWNER)

TABLEPOLICY_DOCStext column

reads

WAREHOUSEBUILD_WHruns the source query as the service owner

SEARCH SERVICEPOLICY_SEARCHindex of passages, fresh to TARGET_LAG

index billed per GB per month, even when nobody searches

question

QUESTION TIME (ALICE)

APPAlice’s chat appneeds USAGE on the service

RESULTTop passagesmost relevant text

answer

AI MODELAI_COMPLETEquestion + passages

SECURITYOwner’s rights: USAGE on the service exposes everything it indexedFilter the source query, or build one service per audience

Figure 7.3 Cortex Search is the retrieval half of RAG: a warehouse keeps the index fresh, the app retrieves passages, and AI_COMPLETE writes the answer. The service answers with its owner's rights, so USAGE on it exposes everything it indexed.

Cortex Analyst works the other way around: no index over text, but a semantic view, a schema object where Thomas declares what "revenue" means, which table holds it, how regions join, and which synonyms business users say. Older projects keep the same definitions in a YAML semantic model file on a stage; Snowflake recommends semantic views for new work. The service turns "Q2 refunds by region" into SQL against exactly those definitions, using only the model's metadata (names, types, descriptions), never table rows. It returns that SQL, and the app runs it on a warehouse under the caller's role, which is what makes it safe to expose to Safia's team: it can't invent a join that isn't in the model, and her privileges and policies still apply. A role needs SNOWFLAKE.CORTEX_USER or the narrower SNOWFLAKE.CORTEX_ANALYST_USER, plus SELECT on the tables. Analyst bills per successful message, plus the warehouse that runs the SQL. Cortex Code (next page) helps people who write SQL; Analyst takes SQL out of the conversation for business users. The audience is the tell. Snowflake now recommends Cortex Agents for new conversational apps; an agent can call Analyst as one of its tools.

Cortex Analyst. Safia asks a business question in an app. Analyst reads the semantic view, which declares the tables, joins, metrics and synonyms, and generates SQL. The SQL runs on a warehouse under Safia's own role, so her privileges and any row access or masking policies apply. She gets the answer and the SQL that produced it. Analyst is billed per message, and the warehouse is billed for running the SQL.ONE QUESTION, START TO FINISHQUESTIONSafia’s question"Q2 refundsby region?"SERVICECortex Analystwrites SQLbilled per messageSEMANTIC VIEWREFUNDS_SVtables, joins, metrics,synonymsWAREHOUSEWarehouseruns the SQL asSafia’s roleRESULTAnswer + SQLher privileges andpolicies appliedasksdefinitionsSQLrows

ONE QUESTION, START TO FINISH

QUESTIONSafia’s question"Q2 refunds by region?"

asks

SERVICECortex Analystwrites SQLbilled per message

definitions

SEMANTIC VIEWREFUNDS_SVtables, joins, metrics, synonyms

SQL

WAREHOUSEWarehouseruns the SQL as Safia’s role

rows

RESULTAnswer + SQLher privileges and policies applied

Figure 7.4 Cortex Analyst writes SQL using only what the semantic view defines; the app runs it under the caller's role, so her privileges and policies still apply. Billed per successful message, plus the warehouse that runs the SQL. REFUNDS_SV is an illustrative name.
Key takeaways
  • Search retrieves from text, Analyst answers from tables. Search pays a warehouse for refreshes, tokens to embed new or changed rows, and serving per GB per month even when nobody searches. Analyst pays per successful message, plus the warehouse that runs its SQL.
  • Search is the retrieval layer of RAG; pair it with AI_COMPLETE to build the "chat with our documents" app everyone asks for.
  • Analyst's SQL runs under the caller's role, so policies apply. Search runs with its owner's rights, so USAGE on a search service exposes everything it indexed.