Skip to main content

search_word function

Applies to: check marked yes Databricks SQL check marked yes Databricks Runtime 19

Beta

This feature is in Beta.

Returns true if every term in search_pattern occurs at word boundaries within one string-searchable value in expr.

Syntax​

search_word(expr, search_pattern [, search_pattern ...] [, case => case_sensitivity])

Arguments​

  • expr: The expression to search. Only one target expression is supported.
  • search_pattern: One or more STRING constant expressions, each containing a term to search for. You can pass up to 5 search patterns per call. To search for more, combine several calls with AND.
  • case_sensitivity: An optional STRING literal that controls case sensitivity. Valid values are 'sensitive' and 'insensitive', regardless of letter case. The default is 'sensitive'.

Returns​

A BOOLEAN value that indicates whether every search term matches at word boundaries within one string-searchable value in expr.

Notes​

With case => 'insensitive', matching follows the case-insensitive rules of the UTF8_LCASE collation. Input strings must still use the default UTF8_BINARY collation.

search_word matches each term as a contiguous literal substring with word boundaries at both ends. Boundaries are determined in the context of the searched string using International Components for Unicode (ICU) word segmentation, based on Unicode Standard Annex #29.

Each search_pattern argument is a separate term. Every term must match within the same string-searchable value, but the terms can occur in any order and at any distance from each other. Terms are interpreted literally, including quotation marks.

Each non-null term must contain at least one word token when tokenized on its own. A term such as '!!!' or ' ' raises SEARCH_INVALID_WORD_PATTERN.

The following types are string-searchable:

Type

String-searchable when

VOID, VARIANT

Always.

STRING

Only when it uses the default UTF8_BINARY collation. The function ignores STRING values with any other collation. See Collation.

ARRAY

Its child expression is string-searchable.

MAP

Its value expression is string-searchable.

STRUCT

At least one of its field expressions is string-searchable.

Type

String-searchable when

VOID, VARIANT

Always.

STRING

Only when it uses the default UTF8_BINARY collation. The function ignores STRING values with any other collation. See Collation.

ARRAY

Its child expression is string-searchable.

MAP

Its value expression is string-searchable.

STRUCT

At least one of its field expressions is string-searchable.

For STRUCT expressions, search_word ignores fields that are not string-searchable. For VARIANT expressions, it searches only leaf nodes of type VOID or STRING.

A STRING column declared without a COLLATE clause uses its table's default collation, which the table can inherit from its schema. If that collation is not UTF8_BINARY, search_word ignores the column, so a search over only that column returns false. To search the column, cast it to STRING COLLATE UTF8_BINARY. Full-text search indexes don't support columns with other collations, so a search over the cast column doesn't use an index.

The function does not implicitly cast non-STRING expressions to STRING expressions.

The return value is determined as follows:

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

Queries that use search_word can benefit from a full-text search index that uses the split tokenizer. See Full-text search indexes on Unity Catalog managed tables.

Common error conditions​

Examples​

SQL
-- Match one complete word, but not a partial word.
> SELECT search_word(column, 'needle')
FROM VALUES ('needle in haystack'), ('needlework kit') AS table(column);
true
false

-- Match separate terms in any order.
> SELECT search_word('a life of battery saving', 'battery', 'life');
true

> SELECT search_word('a life of battery saving', 'battery', 'lifetime');
false

-- A single multi-word term must match a contiguous phrase.
> SELECT search_word('a life of battery saving', 'battery life');
false

> SELECT search_word('battery life is short', 'battery life');
true

-- Ignore letter case.
> SELECT search_word('Quick Brown Fox', 'fox', 'quick', case => 'insensitive');
true

-- A column with a default collation other than UTF8_BINARY is ignored.
> CREATE TABLE notes (body STRING) DEFAULT COLLATION UTF8_LCASE;
> INSERT INTO notes VALUES ('asked about pricing');
> SELECT search_word(body, 'pricing') FROM notes;
false

-- Cast the column to UTF8_BINARY to search it.
> SELECT search_word(CAST(body AS STRING COLLATE UTF8_BINARY), 'pricing') FROM notes;
true

-- NULL behavior.
> SELECT search_word(CAST(NULL AS STRING), 'needle');
NULL