search_word function
Applies to: Databricks SQL
Databricks Runtime 19
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 moreSTRINGconstant 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 withAND.case_sensitivity: An optionalSTRINGliteral 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 |
|---|---|
| Always. |
| Only when it uses the default |
| Its child expression is string-searchable. |
| Its value expression is string-searchable. |
| 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:
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.
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
- DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE
- SEARCH_ARGUMENT_NOT_CONSTANT
- SEARCH_ARGUMENT_NOT_LITERAL.WITH_OPTIONS
- SEARCH_INVALID_WORD_PATTERN
SEARCH_MULTIPLE_TARGETS_NOT_SUPPORTED- SEARCH_STAR_ARGUMENT_NOT_ALLOWED
SEARCH_TOO_MANY_NEEDLES- UNRECOGNIZED_PARAMETER_NAME
- WRONG_NUM_ARGS.WITHOUT_SUGGESTION
Examples
-- 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