Full-text search indexes on Unity Catalog managed tables
This feature is in Beta. To use it, a workspace admin must turn on SQL: Full-Text Search Index for Unity Catalog managed tables from the Previews page. See Manage Databricks previews.
Finding rows that contain a specific word or substring, such as an error code in log messages, can require scanning large amounts of text. A full-text search index accelerates lookups on one or more text columns of a managed Delta Lake or Iceberg table. Choose the index tokenizer for the search function you use:
ngramsupportssearch_substringfor substring matching.splitsupportssearch_wordfor word and phrase matching.
When you query an indexed column with the corresponding function, Databricks uses the index to skip files that are guaranteed not to contain matching rows. This significantly reduces the amount of data scanned, especially for selective lookups.
Indexes created during the Beta release aren't guaranteed to be compatible with later releases. When the feature reaches Public Preview, you must drop existing indexes and create new ones.
When to use full-text search indexes
Create a full-text search index when:
- You regularly search one or more text columns of a large managed table for words or substrings.
- The search pattern appears in a small fraction of the table's files. Indexes accelerate these selective lookups the most.
- You can write the queries using the
search_substringorsearch_wordfunctions. See Query data using search functions.
Don't create an index on a table that uses a feature listed in Limitations, such as row-level security or column masks. Databricks ignores the index on those tables.
Requirements
Full-text search indexes have requirements for compute, base table and schema permissions, and base table configuration.
Compute
Full-text search indexes require Databricks Runtime 19, and you must enable this Beta feature in your workspace settings. See Manage Databricks previews.
Permissions
To create a search index:
- You must have the
MODIFYpermission for the table referenced in the search index. - You must have the
CREATE TABLEpermission on the parent schema. A schema owner or user with the MANAGE privilege can grant youCREATE TABLEprivileges on the schema.
Table configuration
Before you create a full-text search index, the base table must meet all of these requirements:
- You must create the index in the same catalog and schema as the base table.
- The table is a managed Delta Lake table or a managed Iceberg table.
- Row tracking is enabled (
delta.enableRowTracking = true). See Row tracking in Databricks. - Predictive optimization is enabled. See Enable predictive optimization.
- Indexed columns are of type
STRING,VARIANT,STRUCT,ARRAY, orMAP.STRINGcolumns use theUTF8_BINARYcollation. - A
STRUCTcolumn contains at least oneSTRING,VARIANT, orARRAYleaf field at any nesting depth; other leaf fields are ignored. - The table doesn't use any features from the list of limitations, including: OpenSharing, shallow cloning, attribute-based access controls, row-level security policies, and column masks. See Limitations.
For information about table protocol requirements, which apply to both Delta Lake and Iceberg tables, see Delta Lake feature compatibility and protocols.
Create a full-text search index
You can create up to four indexes on a single table, each on a different column.
Use CREATE SEARCH INDEX to create an index over one or more text columns. The following example indexes two text columns of an existing log table:
CREATE SEARCH INDEX log_idx
ON logs (message, error_detail);
The full syntax is:
CREATE SEARCH INDEX [IF NOT EXISTS] index_name
ON table_name ( column_name [, column_name ...] )
[OPTIONS ( option_key = option_value [, ... ] )]
index_name must be unique within the schema and can't match an existing table name.
To control how the text is tokenized, see Options.
If CREATE SEARCH INDEX fails, run it again.
Options
The OPTIONS clause accepts these keys:
Key | Values | Default | Description |
|---|---|---|---|
|
|
| How the text is tokenized for indexing. See Select a tokenizer for your use case. |
| integer in |
| Length of the n-grams produced. Only valid when |
| integer |
| Minimum length of tokens to keep. Tokens shorter than this are dropped during indexing. Only valid when |
For detailed information about invalid option errors, see SEARCH_INDEX_INVALID_PARAMETERS error condition.
Select a tokenizer for your use case
Search indexes have two tokenizer options, depending on your use case:
Tokenizer | Use case | Description |
|---|---|---|
| Substring matching with | Splits text into overlapping n-grams of length |
| Word and phrase matching with | Splits text into UAX #29 words. |
To create an n-gram index with an n-gram size of 4:
CREATE SEARCH INDEX log_ngram_idx
ON logs (message)
OPTIONS (tokenizer = 'ngram', ngram_size = 4);
To create a split index with a minimum token length of 2:
CREATE SEARCH INDEX log_word_idx
ON logs (message)
OPTIONS (tokenizer = 'split', min_token_length = 2);
Query data using search functions
Full-text search indexes accelerate queries that use the following SQL functions:
search_substring: Matches literal substrings. Uses an index with thengramtokenizer.search_word: Matches complete words and phrases. Uses an index with thesplittokenizer.
The functions require Databricks Runtime 19.
search_substring and search_word are case-sensitive by default. Set case => 'insensitive' to ignore letter case using the rules of the UTF8_LCASE collation.
The functions also work without an index. Search indexes do not affect results.
Indexes accelerate queries the most when the search pattern appears in a small fraction of the table's files.
Use the following syntax:
search_substring(expr, search_pattern [, search_pattern ...] [, case => 'sensitive' | 'insensitive'])
search_word(expr, search_pattern [, search_pattern ...] [, case => 'sensitive' | 'insensitive'])
Arguments
The search functions accept the following arguments:
expris one expression of typeSTRING,VARIANT,STRUCT,ARRAY, orMAP, the same types that indexing allows. Only one target expression is supported. The functions search onlySTRINGvalues that use the defaultUTF8_BINARYcollation and ignore other values inexpr. Ifexprhas no searchable values, the result isfalse.search_patternis one or more constantSTRINGexpressions, such as'error'orlower('ERROR'). Every term must match within one string-searchable value inexpr. You can pass up to 5 search patterns per call. To search for more, combine several calls withAND.caseoptionally specifies'sensitive'(the default) or'insensitive'matching forsearch_substringandsearch_word.
Returns
The search functions return a BOOLEAN value with three-valued logic:
nullif asearch_patternisnulland every othersearch_patternis valid.trueif at least one string-searchable value matches every term.nullif no string-searchable value matches every term and at least one string-searchable value isnull.falseotherwise.
Examples
The following examples show common search queries:
-- Case-insensitive substring search.
SELECT * FROM logs
WHERE search_substring(message, 'connection refused', case => 'insensitive');
-- Substring search: matches both terms in the same message, in any order.
SELECT * FROM logs
WHERE search_substring(message, 'connection', 'refused');
-- Word search: matches rows containing all three words in any order.
SELECT * FROM audit_logs
WHERE search_word(message, 'user', 'admin', 'login');
Automatic index refresh
Predictive optimization keeps full-text search indexes up to date as the base table changes. It adds entries for new rows and removes entries for deleted rows. Refreshes run in the background, so recently written data can take some time to be indexed.
You can't run REFRESH INDEX manually.
If you turn off predictive optimization on the base table, its indexes stop refreshing. Queries stay correct, but they scan the data the index doesn't cover, so they slow down as the table changes. To resume refreshes, turn predictive optimization back on.
Index refreshes run as predictive optimization operations on serverless compute and are billed to your account. See Compute and billing.
To track index refreshes, query the predictive optimization system table for the REFRESH_INDEX operation type. These rows carry the index name and ID in table_name and table_id. See Predictive optimization system table reference.
Manage indexes
Databricks maintains query correctness, regardless of index freshness. When a table contains non-indexed data, the query uses the existing index to accelerate access to the indexed records and uses a table scan for the non-indexed records.
Use these operations to manage full-text search indexes:
Describe or view an index
To view information about an index:
DESCRIBE INDEX log_idx;
Drop an index
To drop an index, run:
DROP INDEX log_idx;
To avoid an error for missing indexes, use:
DROP INDEX IF EXISTS log_idx;
If you drop the base table, the command also drops the full-text search indexes.
Limitations
Full-text search indexes have these limitations:
- An
ngramindex accelerates onlysearch_substring, and asplitindex accelerates onlysearch_word. - Renaming an indexed column on the base table, or changing its data type, is not supported.
- Tables with OpenSharing are not supported. If you add the base table as an OpenSharing source or target after creating the index, Databricks ignores the search index.
- Tables with shallow clones are not supported. If you add the base table as a shallow clone source after creating the index, Databricks ignores the search index.
- Tables with attribute-based access controls, column masks, or row-level security policies are not supported. If you add any of these controls to a table with a search index, Databricks ignores the search index. See Core concepts for attribute-based access control (ABAC).