Skip to main content

Full-text search indexes on Unity Catalog managed tables

Beta

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:

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.

important

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_substring or search_word functions. 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 MODIFY permission for the table referenced in the search index.
  • You must have the CREATE TABLE permission on the parent schema. A schema owner or user with the MANAGE privilege can grant you CREATE TABLE privileges 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, or MAP. STRING columns use the UTF8_BINARY collation.
  • A STRUCT column contains at least one STRING, VARIANT, or ARRAY leaf 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:

SQL
CREATE SEARCH INDEX log_idx
ON logs (message, error_detail);

The full syntax is:

SQL
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.

note

If CREATE SEARCH INDEX fails, run it again.

Options​

The OPTIONS clause accepts these keys:

Key

Values

Default

Description

tokenizer

ngram, split

ngram

How the text is tokenized for indexing. See Select a tokenizer for your use case.

ngram_size

integer in [3, 10]

5

Length of the n-grams produced. Only valid when tokenizer = 'ngram'.

min_token_length

integer >= 1

3

Minimum length of tokens to keep. Tokens shorter than this are dropped during indexing. Only valid when tokenizer = 'split'.

Key

Values

Default

Description

tokenizer

ngram, split

ngram

How the text is tokenized for indexing. See Select a tokenizer for your use case.

ngram_size

integer in [3, 10]

5

Length of the n-grams produced. Only valid when tokenizer = 'ngram'.

min_token_length

integer >= 1

3

Minimum length of tokens to keep. Tokens shorter than this are dropped during indexing. Only valid when tokenizer = 'split'.

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

ngram

Substring matching with search_substring.

Splits text into overlapping n-grams of length ngram_size.

split

Word and phrase matching with search_word.

Splits text into UAX #29 words.

Tokenizer

Use case

Description

ngram

Substring matching with search_substring.

Splits text into overlapping n-grams of length ngram_size.

split

Word and phrase matching with search_word.

Splits text into UAX #29 words.

To create an n-gram index with an n-gram size of 4:

SQL
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:

SQL
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 the ngram tokenizer.
  • search_word: Matches complete words and phrases. Uses an index with the split tokenizer.

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:

SQL
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:

  • expr is one expression of type STRING, VARIANT, STRUCT, ARRAY, or MAP, the same types that indexing allows. Only one target expression is supported. The functions search only STRING values that use the default UTF8_BINARY collation and ignore other values in expr. If expr has no searchable values, the result is false.
  • search_pattern is one or more constant STRING expressions, such as 'error' or lower('ERROR'). Every term must match within one string-searchable value in expr. You can pass up to 5 search patterns per call. To search for more, combine several calls with AND.
  • case optionally specifies 'sensitive' (the default) or 'insensitive' matching for search_substring and search_word.

Returns​

The search functions return a BOOLEAN value with three-valued logic:

  • null if a search_pattern is null and every other search_pattern is valid.
  • true if at least one string-searchable value matches every term.
  • null if no string-searchable value matches every term and at least one string-searchable value is null.
  • false otherwise.

Examples​

The following examples show common search queries:

SQL
-- 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​

important

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:

SQL
DESCRIBE INDEX log_idx;

Drop an index​

To drop an index, run:

SQL
DROP INDEX log_idx;

To avoid an error for missing indexes, use:

SQL
DROP INDEX IF EXISTS log_idx;
note

If you drop the base table, the command also drops the full-text search indexes.

Limitations​

Full-text search indexes have these limitations:

  • An ngram index accelerates only search_substring, and a split index accelerates only search_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).