Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Applies to:
Databricks Runtime 19.0 and above
Important
This feature is in Beta. Workspace admins can control access to this feature from the Previews page. See Manage Azure Databricks previews.
Returns true if search_pattern is found in any of the supplied target column values.
For information about accelerating search queries with full-text search indexes, see Full-text search indexes on Unity Catalog managed tables.
Syntax
search ( column [, ...], search_pattern [, mode => mode] )
Arguments
column: One or more searchable target expressions. A searchable expression has one of these types:STRINGwithUTF8_BINARYcollation.VARIANT.STRUCTwith at least one searchable field. The struct is automatically expanded to its searchable leaf fields at any nesting depth.ARRAYof any searchable type.
There is no implicit cast for non-string target values.
search_pattern: A foldable (constant)STRINGexpression with the value to search for.mode: An optional namedSTRINGargument that controls how matches are performed. One of:'substring'(default): Matches ifsearch_patternappears anywhere within a target value. Equivalent tocontainsfunction.'word': Matches the individual words insearch_patternagainst a target value, regardless of order.
Returns
A BOOLEAN.
trueifsearch_patternis found in any of the target column values.NULLifsearch_patternis not found in any target column value and at least one of those values isNULL.falseotherwise.
Notes
isearchfunction is the case-insensitive variant ofsearchwith otherwise identical behavior.- For a
STRUCTargument, the function searches every searchable leaf field reachable from the top-level struct, regardless of nesting depth. The same expansion applies recursively toSTRUCTfields withinVARIANTandARRAYvalues. - In
'substring'mode,VARIANTkeys and non-string scalar values inside aVARIANTare not matched. Use'word'mode or extract individual fields to search those.
Common error conditions
Examples
-- Basic examples.
> SELECT search(column, 'needle', mode => 'substring') FROM VALUES ('Needle') AS table(column);
false
> SELECT search(column, 'quick fox', mode => 'substring') FROM VALUES ('quick brown fox') AS table(column);
false
> SELECT search(column, lower('NEEDLE')) FROM VALUES ('needle') AS table(column);
true
> SELECT search(column, 'quick fox', mode => 'word') FROM VALUES ('quick brown fox') AS table(column);
true
> SELECT search(column, 'qui fox', mode => 'word') FROM VALUES ('quick fox') AS table(column);
false
-- Automatic expansion of STRUCT columns to their searchable leaf fields.
> CREATE TABLE test_table (
usage_stats STRUCT<digits: INT>,
customer_info STRUCT<
contact: STRUCT<json_field: VARIANT>,
name: STRING>)
USING DELTA;
> SELECT * FROM test_table WHERE search(usage_stats.digits, 'needle');
[DATATYPE_MISMATCH.UNEXPECTED_INPUT_TYPE]
> SELECT * FROM test_table WHERE search(customer_info, 'needle');
-- Equivalent to:
> SELECT * FROM test_table
WHERE search(customer_info.contact.json_field, customer_info.name, 'needle');
> SELECT * FROM test_table WHERE search(customer_info.contact, 'needle');
-- Equivalent to:
> SELECT * FROM test_table
WHERE search(customer_info.contact.json_field, 'needle');
-- VARIANT behavior: substring mode does not match keys or non-string scalar values.
> CREATE TABLE test_table AS
SELECT parse_json('{
"role": "user",
"id": 101,
"preferences": {"theme": "dark", "language": "en"}
}') AS column;
> SELECT search(column, 'user', mode => 'substring') FROM test_table;
true
> SELECT search(column, 'preferences', mode => 'substring') FROM test_table;
false
> SELECT search(column, '101', mode => 'substring') FROM test_table;
false
-- NULL behavior.
> SELECT search(NULL, 'needle', mode => 'substring');
NULL
> SELECT search(CAST(NULL AS STRING), 'needle', mode => 'substring');
NULL
> SELECT search('needle in haystack', NULL, mode => 'substring');
[SEARCH_REQUIRES_STRING_LITERALS_ARGUMENTS]