CREATE CORTEX SEARCH SERVICE

statementpartialAI & ML

docs.snowflake.com ↗

Object: CORTEX SEARCH SERVICE

Creates a new Cortex Search service or replaces an existing one.

Syntax

supportedpartialnot supportednot scored

CREATE [ OR REPLACE ] CORTEX SEARCH SERVICE [ IF NOT EXISTS ] <name>
  ON <search_column>
  [ PRIMARY KEY ( <col_name> [, ... ] ) ]
  ATTRIBUTES <col_name> [ , ... ]
  WAREHOUSE = <warehouse_name>
  TARGET_LAG = '<num> { seconds | minutes | hours | days }'
  [ EMBEDDING_MODEL = <embedding_model_name> ]
  [ REFRESH_MODE = { FULL | INCREMENTAL } ]
  [ INITIALIZE = { ON_CREATE | ON_SCHEDULE } ]
  [ FULL_INDEX_BUILD_INTERVAL_DAYS = <num> ]
  [ REQUEST_LOGGING = { TRUE | FALSE } ]
  [ AUTO_SUSPEND = <num_seconds> ]
  [ COMMENT = '<comment>' ]
AS <query>;

CREATE [ OR REPLACE ] CORTEX SEARCH SERVICE <name>
  TEXT INDEXES <text_column_name> [ , ... ]
  VECTOR INDEXES <column_specification> [ , ... ]
  [ PRIMARY KEY ( <col_name> [, ... ] ) ]
  ATTRIBUTES <col_name> [ , ... ]
  WAREHOUSE = <warehouse_name>
  TARGET_LAG = '<num> { seconds | minutes | hours | days }'
  [ REFRESH_MODE = { FULL | INCREMENTAL } ]
  [ INITIALIZE = { ON_CREATE | ON_SCHEDULE } ]
  [ FULL_INDEX_BUILD_INTERVAL_DAYS = <num> ]
  [ REQUEST_LOGGING = { TRUE | FALSE } ]
  [ AUTO_SUSPEND = <num_seconds> ]
  [ COMMENT = '<comment>' ]
AS <query>;

Parameters

NameRequiredDescription
namerequiredString that specifies the identifier (that is, the name) for the Cortex Search service; must be unique for the schema in which the service is created. In addition, the identifier must start with an alphabetic character and cannot contain spaces or special characters unless the entire identifier string is enclosed in double quotes (for example, "My object"). Identifiers enclosed in double quotes are also case-sensitive. For more information, see Identifier requirements.
ON search_columnrequiredSpecifies the text column in the base table that you wish to search on, for single-index Cortex Search. This column must be a text value.
TEXT INDEXES text_column_name [, ... ]requiredSpecifies comma-separated text columns in the base table to search on, for multi-index Cortex Search. Columns must be text values.
VECTOR INDEXES column_specification [ , ... ]requiredSpecifies columns for vector similarity searches. Column specifications include: Managed vector embeddings: text_column_name (model='embedding_model'): Specifies a text column and the embedding model used for vector generation. Must use one of the supported embedding models. If no model is specified, the default model snowflake-arctic-embed-m-v1.5 is used. User-provided vector embeddings: vector_column_name: Specifies a user-provided vector embedding column. User-provided vector embeddings with managed query embeddings: vector_column_name(query_model='embedding_model'): Specifies a user-provided vector embedding column and the embedding model used for embedding text at query time. The query_model must be one of the Snowflake-managed embedding models supported in Cortex Search. If no query_model is specified, then the user-provided vector column can only be used with a vector embedding query. For information on the behavior of vector embeddings, see Usage Notes.
ATTRIBUTES col_name [ , ... ]requiredSpecifies a comma-separated list of columns in the base table that you wish to filter on when issuing queries to the service. Attribute columns must be included in the source query, either via explicit enumeration or a wildcard (*).
WAREHOUSE = warehouse_namerequiredSpecifies the warehouse to use for running the source query, building the search index, and keeping it refreshed per the TARGET_LAG target.
TARGET_LAG = 'num { seconds | minutes | hours | days }'requiredSpecifies the maximum amount of time that the Cortex Search service content should lag behind updates to the base tables specified in the source query. Note Ensure that the target lag is shorter than the data retention period of the source tables. If the target lag exceeds the data retention period, the service may be unable to detect changes in the source data and could require recreation. For more information, see DATA_RETENTION_TIME_IN_DAYS.
PRIMARY KEY ( col_name [, ... ] )optionalSpecifies a set of columns that uniquely identify each row in the source query. The combination of values in the designated columns must be unique for each row; rows with duplicate primary key values are ignored in the resulting search index. Primary key columns must be of the TEXT data type. Services with primary keys can make use of an optimized refresh path when the underlying data changes, resulting in significant reductions to the cost and latency of a refresh. For more information, see Primary keys.
EMBEDDING_MODEL = <embedding_model_name>optionalOptional parameter that specifies the embedding model to use in the Cortex Search Service. This property cannot be altered after you create the Cortex Search Service. To modify the property, recreate the Cortex Search Service with a CREATE OR REPLACE CORTEX SEARCH SERVICE command. Some embedding models are only available in certain cloud regions for Cortex Search. For an availability list by model by region, see Cortex Search Regional Availability. Each model may incur a different cost per million input tokens processed. Refer to the Snowflake Service Consumption Table for each function’s cost in credits per million tokens. If the EMBEDDING_MODEL is not specified, the default model is used. The default model is snowflake-arctic-embed-m-v1.5.
REFRESH_MODE = { FULL | INCREMENTAL }optionalSpecifies the refresh mode for the Cortex Search Service. This property cannot be altered after you create the Cortex Search Service. To modify the property, recreate the Cortex Search Service with a CREATE OR REPLACE CORTEX SEARCH SERVICE command. Default: INCREMENTAL
  • FULL — Enforces a full refresh of the Cortex Search Service. A full refresh recomputes all embeddings and rebuilds the index on every change to the underlying source data. Given the cost of recomputing embeddings, full refresh should only be considered if incremental refresh is not supported for your workload.
  • INCREMENTAL — Enforces an incremental refresh of the Cortex Search Service. An incremental refresh applies only the changes since the last refresh, making it more efficient for large datasets with small updates. If the Cortex Search Service cannot perform an incremental refresh, service creation fails and displays an error message. Incremental refresh requires change tracking to be enabled on all underlying objects. For more information, see Change Tracking Requirements.
INITIALIZEoptionalSpecifies the behavior of the initial refresh of the Cortex Search Service. This property cannot be altered after you create the service. To modify the property, replace the cortex search service with a CREATE OR REPLACE CORTEX SEARCH SERVICE command. Default: ON_CREATE
  • ON_CREATE — Refreshes the Cortex Search Service synchronously at creation. If this refresh fails, service creation fails and displays an error message.
  • ON_SCHEDULE — Refreshes the Cortex Search Service at the next scheduled refresh. The Cortex Search Service is populated when the refresh schedule process runs. No data is populated when the Cortex Search Service is created. If you try to query the service, you might see the following error because the first scheduled refresh has not yet occurred. Your service has not yet been loaded into our serving system. Please retry your request in a few minutes.
FULL_INDEX_BUILD_INTERVAL_DAYS = numoptionalSpecifies the target interval, in days, between full index rebuilds for a Cortex Search service with primary keys defined. This property is only applicable to services that have primary keys set. Services with primary keys use an optimized refresh path where each refresh cycle (governed by TARGET_LAG) processes only the rows that changed since the last refresh, rather than re-embedding all data. Over time, these incremental updates accumulate fragmented index segments. A full index rebuild compacts these segments into a single optimized index. The rebuild does not re-embed unchanged data. This value is a soft target. Full index rebuilds may occur more frequently than the specified interval to optimize serving performance based on factors such as service target lag, change rate in the service source data, and overall service size. Default: 1
REQUEST_LOGGING = { TRUE | FALSE }optionalEnables or disables request logging for the Cortex Search Service. When enabled, the service records information about search requests, which you can query for monitoring and analysis purposes. For more information, see Monitor Cortex Search requests. Default: FALSE
AUTO_SUSPEND = num_secondsoptionalEnables automatic suspension of the service’s serving compute after the specified number of seconds of query inactivity. The service automatically resumes when it receives a query. Minimum: 1800 (30 minutes). For more information, see Auto-suspend serving on inactivity. Default: NULL (auto-suspend disabled)
COMMENT = 'comment'optionalSpecifies a comment for the service.
AS queryoptionalSpecifies a query defining the base table from which the service is created.

Usage notes

Access Control Requirements

Privilege | Object CREATE CORTEX SEARCH SERVICE | Schema in which you are creating the search service. SELECT | Tables and views that the service queries. USAGE | Warehouse that refreshes the service. Attention To create a Cortex Search Service, your role must have the required privileges to use the Cortex embedding functions. This requires granting the SNOWFLAKE.CORTEX_USER database role or the SNOWFLAKE.CORTEX_EMBED_USER database role to the service creator role.

Change Tracking Requirements

When creating a Cortex Search Service, if change tracking is not already enabled on the tables that it queries, Snowflake automatically attempts to enable change tracking on them. In order to support incremental refreshes, change tracking must be enabled with non-zero time travel retention on all underlying objects used by a Cortex Search Service. As base objects change, so does the Cortex Search Service. If you recreate a base object, you must re-enable change tracking. For more information about enabling change tracking, see Change tracking not enabled on base tables. Note For Cortex Search Services, the atomicity of CREATE OR REPLACE applies to the metadata transaction only. The search index for the new service is built after the transaction commits, so the previous service’s data is no longer available from the moment the statement commits until the new index finishes building. - The OR REPLACE and IF NOT EXISTS clauses are mutually exclusive. They can’t both be used in the same statement. - CREATE OR REPLACE <object> statements are atomic. That is, when an object is replaced, the old object is deleted and the new object is created in a single transaction.

Known gaps

Parts not fully supported, and recorded divergences from Snowflake.