Snowflake

Snowflake Cortex Search Deep Dive: Hybrid Search and RAG Patterns

2026-03-21
更新: 2026-03-27
NicheeLab Editorial Team

Snowflake Cortex Search delivers hybrid retrieval — fusing keyword and semantic search — as a fully managed service. Embedding generation, index building, and score fusion are all automated, so you only need a single CREATE CORTEX SEARCH SERVICE statement to add high-quality search to your application. Used as a Retrieval-Augmented Generation (RAG) retriever, it pairs cleanly with Cortex LLM functions to power internal document Q&A systems.

The hybrid search behind Cortex Search internally blends two complementary retrieval techniques.

Retrieval styleTechniqueStrengthsWeaknesses
Keyword (sparse)BM25-based inverted indexStrong on exact matches and proper nounsWeak on synonyms and paraphrasing
Semantic (dense)Vector embeddings with approximate nearest-neighbor searchStrong on semantic similarityWeak on exact proper nouns and model numbers
Hybrid (Cortex Search)Fuses both scoresCombines precision with semantic understandingHigher indexing cost
検索フロー:

[ユーザークエリ: "Snowflakeのデータ保護機能"]
       │
       ├──→ キーワード検索 ──→ BM25スコア
       │     └─ "Snowflake", "データ", "保護" で転置インデックスを走査
       │
       ├──→ セマンティック検索 ──→ ベクトル類似度スコア
       │     └─ クエリをエンベディング化 → 近似近傍探索
       │
       └──→ スコア融合(Reciprocal Rank Fusion)
              └─ 最終ランキング結果を返却

CREATE CORTEX SEARCH SERVICE

You create a Cortex Search service with DDL, specifying the source table, the column to search, optional filter columns, and the refresh cadence.

CREATE OR REPLACE CORTEX SEARCH SERVICE knowledge_base_search
  ON doc_content                    -- 検索対象のテキストカラム
  ATTRIBUTES doc_title, category    -- フィルタ用カラム(任意)
  WAREHOUSE = search_wh             -- インデックス構築用WH
  TARGET_LAG = '1 hour'             -- ソースデータの反映遅延
AS (
  SELECT
    doc_id,
    doc_title,
    doc_content,
    category,
    updated_at
  FROM knowledge_base
  WHERE is_published = TRUE
);

Key Parameters

ParameterDescriptionRequired
ONText column targeted by both full-text and semantic searchYes
ATTRIBUTESColumns available for metadata filtering (used in WHERE-style conditions)No
WAREHOUSEWarehouse used to build the indexYes
TARGET_LAGMaximum delay before source-table changes appear in the indexYes

Running Search Queries

You query a Cortex Search service through the Python SDK (recommended) or the REST API.

# Python SDKでの検索
from snowflake.core import Root

root = Root(session)

search_service = (
    root.databases["MY_DB"]
    .schemas["PUBLIC"]
    .cortex_search_services["KNOWLEDGE_BASE_SEARCH"]
)

results = search_service.search(
    query="Snowflakeのタイムトラベル機能について",
    columns=["doc_id", "doc_title", "doc_content", "category"],
    filter={
        "@eq": {"category": "data_protection"}
    },
    limit=5
)

for result in results.results:
    print(f"Title: {result['doc_title']}")
    print(f"Score: {result['search_score']}")
    print(f"Content: {result['doc_content'][:200]}...")
    print("---")

Filter Syntax

The following filter operators are available on columns listed in ATTRIBUTES.

# 完全一致
{"@eq": {"category": "security"}}

# OR条件(複数値)
{"@or": [
  {"@eq": {"category": "security"}},
  {"@eq": {"category": "governance"}}
]}

# AND条件
{"@and": [
  {"@eq": {"category": "security"}},
  {"@eq": {"doc_type": "guide"}}
]}

Using Cortex Search as a RAG Retriever

By wiring Cortex Search as the retriever in front of Cortex LLM functions, you can build a RAG pipeline grounded in your internal knowledge base.

RAGパイプライン構成:

[ユーザー質問]
       │
       ▼
[Cortex Search]  ← リトリーバ
       │ 関連ドキュメント Top-K を取得
       ▼
[プロンプト構築]
       │ "以下のコンテキストに基づいて回答してください:
       │  {検索結果のdoc_content}"
       ▼
[SNOWFLAKE.CORTEX.COMPLETE()]  ← ジェネレータ
       │ コンテキスト付きで回答を生成
       ▼
[回答をユーザーに返却]
# RAGパイプラインの実装例(Streamlit in Snowflake)
import streamlit as st
from snowflake.core import Root
from snowflake.snowpark.context import get_active_session

session = get_active_session()
root = Root(session)

search_service = (
    root.databases["KB_DB"]
    .schemas["PUBLIC"]
    .cortex_search_services["KB_SEARCH"]
)

user_question = st.text_input("質問を入力してください")

if user_question:
    # Step 1: Cortex Searchで関連ドキュメント取得
    results = search_service.search(
        query=user_question,
        columns=["doc_content"],
        limit=3
    )

    context = "\n\n".join([r["doc_content"] for r in results.results])

    # Step 2: COMPLETE関数で回答生成
    prompt = f"""以下のコンテキストのみに基づいて質問に回答してください。
コンテキストに情報がない場合は「情報が見つかりませんでした」と回答してください。

コンテキスト:
{context}

質問: {user_question}"""

    answer = session.sql(
        "SELECT SNOWFLAKE.CORTEX.COMPLETE('llama3.1-70b', ?)",
        params=[prompt]
    ).collect()[0][0]

    st.write(answer)

Cost Model

  • Index build: consumes credits on the warehouse you specify
  • Incremental index refresh: consumes serverless credits, scaled to TARGET_LAG
  • Search query execution: consumes serverless credits (no warehouse needed)
  • Storage: standard storage charges apply to the index data

Cortex Search vs. Other Approaches

ApproachRetrieval styleEmbedding managementBest-fit use case
Cortex SearchHybrid (automatic)AutomaticRAG retrievers and document search
VECTOR type + distance functionsSemantic onlyManualCustom embeddings and image search
SEARCH OPTIMIZATIONKeyword only (point-lookup optimization)Not neededSpeeding up equality and LIKE lookups

Exam Prep Highlights

  • Cortex Search is a hybrid keyword + semantic search service
  • CREATE CORTEX SEARCH SERVICE specifies ON (search target) and TARGET_LAG (refresh cadence)
  • Search queries run on serverless compute — no warehouse required
  • Columns listed in ATTRIBUTES are usable for metadata filtering
  • In RAG pipelines, pair it as the retriever with the COMPLETE function
  • Understand how it differs from manual vector search built on the VECTOR type

Sample Question

Cortex Search

問題 1

In the SQL statement that creates a Cortex Search service, which parameter controls the maximum delay before source-table changes appear in the search index?

  1. A. REFRESH_INTERVAL
  2. B. TARGET_LAG
  3. C. INDEX_SYNC_PERIOD
  4. D. UPDATE_FREQUENCY

正解: B

The TARGET_LAG parameter on CREATE CORTEX SEARCH SERVICE sets the maximum delay before source-table changes are reflected in the index. For example, '1 hour' guarantees that changes show up within at most one hour. A, C, and D are not real Cortex Search parameters.

Frequently Asked Questions

What is hybrid search in Cortex Search?

Hybrid search combines traditional keyword matching (sparse retrieval such as BM25) with vector-based semantic search (dense retrieval). Cortex Search fuses the two scores internally and returns results that balance keyword precision with semantic relevance. You don't have to tune the fusion weights yourself.

What is the difference between Cortex Search and vector search?

Cortex Search is a managed service purpose-built for hybrid search over text data — indexing, embedding generation, and score fusion are all automated. Snowflake's VECTOR data type and functions like VECTOR_L2_DISTANCE, on the other hand, are low-level APIs where you generate and store embeddings yourself and write the similarity logic by hand. For a RAG retriever, Cortex Search is much faster to build.

When are data updates reflected in a Cortex Search index?

You set the TARGET_LAG parameter when running CREATE CORTEX SEARCH SERVICE. For example, '1 hour' guarantees that source-table changes show up in the index within at most one hour. Shorter TARGET_LAG values give you fresher results but burn more serverless credits.

Check what you learned with practice questions

Practice with certification-focused question sets

無料で問題を解いてみる
Author

NicheeLab Editorial Team

NicheeLab editorial team focused on data engineering and cloud certification learning. Content is structured around practical study needs and official exam domains.


Related articles
Snowflake

Snowflake Certifications: All 11 Exams Explained (2026)

Every SnowPro certification — Associate, Core, Specialty, Ad...

Snowflake

Snowflake Exam Difficulty Ranking: All 11 Certs Compared (2026)

All 11 SnowPro exams ranked by difficulty with study-time es...

Snowflake

Snowflake Study Guide: Fastest Pass Route by Exam (2026)

How to pass SnowPro certifications efficiently — official ma...

Snowflake

SnowPro Core (COF-C03): Complete Exam Guide (2026)

Pass the SnowPro Core exam — six domains, scope, sample ques...

Snowflake

SnowPro Associate Platform (SOL-C01): Complete Guide (2026)

The entry-level SnowPro Associate exam — scope, weighting, s...

Browse all Snowflake articles (103)
© 2026 NicheeLab All rights reserved.