# SnowflakeCortexSearchRetriever

> **Class** in `langchain_snowflake`

📖 [View in docs](https://reference.langchain.com/python/langchain-snowflake/retrievers/SnowflakeCortexSearchRetriever)

Snowflake Cortex Search retriever using REST API exclusively.

This retriever integrates with Snowflake's Cortex Search service exclusively
through the REST API. Cortex Search is a managed service that provides
enterprise-grade semantic search capabilities.

Note: This retriever uses Cortex Search, which is different from Search Preview.
Cortex Search only supports REST API access, not SQL functions.

## Signature

```python
SnowflakeCortexSearchRetriever(
    self,
    **kwargs: Any = {},
)
```

## Description

**Setup:**

Install ``langchain-snowflake`` and configure Snowflake connection.

.. code-block:: bash

    pip install -U langchain-snowflake

**Key init args:**

service_name: str
    Fully qualified name of the Cortex Search service
session: Optional[Session]
    Active Snowflake session
k: int
    Number of documents to retrieve (default: 4)
search_columns: Optional[List[str]]
    Columns to return in search results
filter_dict: Optional[Dict[str, Any]]
    Filter criteria for search results
content_field: str
    Metadata field containing the actual content (default: "TRANSCRIPT_TEXT")
join_separator: str
    String to join multiple documents (default: "\n\n")
fallback_to_page_content: bool
    Fall back to page_content when metadata field is empty (default: True)

**Instantiate:**

.. code-block:: python

from . import SnowflakeCortexSearchRetriever

# Using existing session (recommended)
retriever = SnowflakeCortexSearchRetriever(
    service_name="mydb.myschema.my_search_service",
    session=session,
    k=5
)

# Using connection parameters
retriever = SnowflakeCortexSearchRetriever(
    service_name="mydb.myschema.my_search_service",
    account="your-account",
    user="your-user",
    password="your-password",
    warehouse="your-warehouse",
    k=3
)

# Using custom content field (e.g., for datasets that store content in "CHUNK")
retriever_custom = SnowflakeCortexSearchRetriever(
    service_name="mydb.myschema.my_search_service",
    session=session,
    k=5,
    content_field="CHUNK",  # Extract from metadata["CHUNK"] instead of "TRANSCRIPT_TEXT"
    join_separator="\n---\n",  # Custom separator
    fallback_to_page_content=True
)

**Usage:**

.. code-block:: python

query = "What is machine learning?"
docs = retriever.invoke(query)
for doc in docs:
    print(doc.page_content)

**Use within a chain:**

.. code-block:: python

from langchain_core.output_parsers import StrOutputParser
from langchain_core.prompts import ChatPromptTemplate
from langchain_core.runnables import RunnablePassthrough
from . import ChatSnowflake

prompt = ChatPromptTemplate.from_template(
    """Answer the question based only on the context provided.

Context: {context}

Question: {question}"""
)

llm = ChatSnowflake(model="llama3.1-70b", session=session)

# With auto_format_for_rag=True (default), no format_docs needed!
chain = (
    {"context": retriever, "question": RunnablePassthrough()}
    | prompt
    | llm
    | StrOutputParser()
)

# Or with manual control:
# from .formatters import format_cortex_search_documents
# retriever_manual = SnowflakeCortexSearchRetriever(..., auto_format_for_rag=False)
# chain = (
#     {"context": retriever_manual | format_cortex_search_documents, "question": RunnablePassthrough()}
#     | prompt | llm | StrOutputParser()
# )

response = chain.invoke("What is the capital of France?")

## Extends

- `BaseRetriever`
- `SnowflakeConnectionMixin`

## Constructors

```python
__init__(
    self,
    **kwargs: Any = {},
) -> None
```


## Properties

- `service_name`
- `k`
- `search_columns`
- `filter_dict`
- `auto_format_for_rag`
- `content_field`
- `join_separator`
- `fallback_to_page_content`
- `model_config`

## Methods

- [`format_documents()`](https://reference.langchain.com/python/langchain-snowflake/retrievers/SnowflakeCortexSearchRetriever/format_documents)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-snowflake/blob/1a32a1c6642b6c453e9537f35afbc7da280f8679/libs/snowflake/langchain_snowflake/retrievers.py#L21)