全文搜索

使用 SQL 命令进行全文搜索。SQL 插件支持 UDB-SX 中可用的全文查询子集。

Match 查询

使用 MATCH 函数搜索给定字段匹配 stringnumberdateboolean 值的文档。

语法

match(field_expression, query_expression[, option=<option_value>]*)

您可以按任意顺序指定以下选项:

  • analyzer

  • auto_generate_synonyms_phrase

  • fuzziness

  • max_expansions

  • prefix_length

  • fuzzy_transpositions

  • fuzzy_rewrite

  • lenient

  • operator

  • minimum_should_match

  • zero_terms_query

  • boost

有关参数描述和支持的值,请参阅 match 查询文档

示例 1:在 message 字段中搜索文本 “this is a test”:

GET my_index/_search
{
  "query": {
    "match": {
      "message": "this is a test"
    }
  }
}

SQL 查询:

SELECT message FROM my_index WHERE match(message, "this is a test")

PPL 查询:

SOURCE=my_index | WHERE match(message, "this is a test") | FIELDS message

示例 2:使用 operator 参数搜索 message 字段:

GET my_index/_search
{
  "query": {
    "match": {
      "message": {
        "query": "this is a test",
        "operator": "and"
      }
    }
  }
}

SQL 查询:

SELECT message FROM my_index WHERE match(message, "this is a test", operator='and')

PPL 查询:

SOURCE=my_index | WHERE match(message, "this is a test", operator='and') | FIELDS message

示例 3:使用 operatorzero_terms_query 参数搜索 message 字段:

GET my_index/_search
{
  "query": {
    "match": {
      "message": {
        "query": "to be or not to be",
        "operator": "and",
        "zero_terms_query": "all"
      }
    }
  }
}

SQL 查询:

SELECT message FROM my_index WHERE match(message, "this is a test", operator='and', zero_terms_query='all')

PPL 查询:

SOURCE=my_index | WHERE match(message, "this is a test", operator='and', zero_terms_query='all') | FIELDS message

Multi-match 查询

要在多个字段中搜索文本,请使用 MULTI_MATCH 函数。此函数映射到搜索引擎中使用的 multi_match 查询,用于返回与给定字段或多个字段中提供的文本、数字、日期或布尔值匹配的文档。

语法

MULTI_MATCH 函数通过使用 ^ 字符来提升某些字段的权重。提升是乘数,使一个字段的匹配比其他字段的匹配更重。该语法支持使用双引号、单引号、反引号或不使用任何引号来指定字段。使用星号 "*" 搜索所有字段。星号应被引号包围。

multi_match([field_expression+], query_expression[, option=<option_value>]*)

权重是可选的,在字段名后指定。可以使用 caret 字符 ^ 或空格分隔。参考以下示例:

multi_match(["Tags" ^ 2, 'Title' 3.4, `Body`, Comments ^ 0.3], ...)
multi_match(["*"], ...)

您可以按任意顺序为 MULTI_MATCH 指定以下选项:

  • analyzer

  • auto_generate_synonyms_phrase

  • cutoff_frequency

  • fuzziness

  • fuzzy_transpositions

  • lenient

  • max_expansions

  • minimum_should_match

  • operator

  • prefix_length

  • tie_breaker

  • type

  • slop

  • zero_terms_query

  • boost

有关参数描述和支持的值,请参阅 multi_match 查询文档

例如,REST API 在 firstnamelastname 字段中搜索 Dale

GET accounts/_search
{
  "query": {
    "multi_match": {
      "query": "Lane Street",
      "fields": [ "address" ],
    }
  }
}

可以通过 SQL 使用 multi_match 函数调用

SELECT firstname, lastname
FROM accounts
WHERE multi_match(['*name'], 'Dale')

multi_match PPL 函数

SOURCE=accounts | WHERE multi_match(['*name'], 'Dale') | fields firstname, lastname
firstname lastname
Dale Adams

Query string 查询

要根据运算符拆分文本,请使用 QUERY_STRING 函数。QUERY_STRING 函数支持逻辑连接符、通配符、正则表达式和近似搜索。 此函数映射到搜索引擎中使用的 query_string 查询,用于返回与给定字段或多个字段中提供的文本、数字、日期或布尔值匹配的文档。

语法

QUERY_STRING 函数的语法与 MATCH_QUERY 类似,并通过使用 ^ 字符来提升某些字段的权重。提升是乘数,使一个字段的匹配比其他字段的匹配更重。该语法支持使用双引号、单引号、反引号或不使用任何引号来指定字段。使用星号 "*" 搜索所有字段。星号应被引号包围。

query_string([field_expression+], query_expression[, option=<option_value>]*)

权重是可选的,在字段名后指定。可以使用 caret 字符 ^ 或空格分隔。参考以下示例:

query_string(["Tags" ^ 2, 'Title' 3.4, `Body`, Comments ^ 0.3], ...)
query_string(["*"], ...)

您可以按任意顺序为 QUERY_STRING 指定以下选项:

  • analyzer

  • allow_leading_wildcard

  • analyze_wildcard

  • auto_generate_synonyms_phrase_query

  • boost

  • default_operator

  • enable_position_increments

  • fuzziness

  • fuzzy_rewrite

  • escape

  • fuzzy_max_expansions

  • fuzzy_prefix_length

  • fuzzy_transpositions

  • lenient

  • max_determinized_states

  • minimum_should_match

  • quote_analyzer

  • phrase_slop

  • quote_field_suffix

  • rewrite

  • type

  • tie_breaker

  • time_zone

有关参数描述和支持的值,请参阅 query_string 查询文档

在 SQL 和 PPL 查询中使用 query_string 的示例:

REST API 搜索请求

GET accounts/_search
{
  "query": {
    "query_string": {
      "query": "Lane Street",
      "fields": [ "address" ],
    }
  }
}

可以通过 SQL 调用

SELECT account_number, address
FROM accounts
WHERE query_string(['address'], 'Lane Street', default_operator='OR')

或通过 PPL 调用

SOURCE=accounts | WHERE query_string(['address'], 'Lane Street', default_operator='OR') | fields account_number, address
account_number address
1 880 Holmes Lane
6 671 Bristol Street
13 789 Madison Street

Match phrase 查询

要搜索精确短语,请使用 MATCHPHRASEMATCH_PHRASE 函数。

语法

matchphrasequery(field_expression, query_expression)
matchphrase(field_expression, query_expression[, option=<option_value>]*)
match_phrase(field_expression, query_expression[, option=<option_value>]*)

MATCHPHRASE/MATCH_PHRASE 函数允许您按任意顺序指定以下选项:

  • analyzer

  • slop

  • zero_terms_query

  • boost

有关参数描述和支持的值,请参阅 match_phrase 查询文档

在 SQL 和 PPL 查询中使用 match_phrase 的示例:

REST API 搜索请求

GET accounts/_search
{
  "query": {
    "match_phrase": {
      "address": {
        "query": "880 Holmes Lane"
      }
    }
  }
}

可以通过 SQL 调用

SELECT account_number, address
FROM accounts
WHERE match_phrase(address, '880 Holmes Lane')

PPL 调用

SOURCE=accounts | WHERE match_phrase(address, '880 Holmes Lane') | FIELDS account_number, address
account_number address
1 880 Holmes Lane

Simple query string 查询

simple_query_string 函数映射到 UDB-SX 中的 simple_query_string 查询。它返回与给定字段或多个字段中提供的文本、数字、日期或布尔值匹配的文档。 ^ 允许您提升某些字段的权重。提升是乘数,使一个字段的匹配比其他字段的匹配更重。

语法

该语法支持使用双引号、单引号、反引号或不使用任何引号来指定字段。使用星号 "*" 搜索所有字段。星号应被引号包围。

simple_query_string([field_expression+], query_expression[, option=<option_value>]*)

权重是可选的,在字段名后指定。可以使用 caret 字符 ^ 或空格分隔。参考以下示例:

simple_query_string(["Tags" ^ 2, 'Title' 3.4, `Body`, Comments ^ 0.3], ...)
simple_query_string(["*"], ...)

您可以按任意顺序为 SIMPLE_QUERY_STRING 指定以下选项:

  • analyze_wildcard

  • analyzer

  • auto_generate_synonyms_phrase_query

  • boost

  • default_operator

  • flags

  • fuzzy_max_expansions

  • fuzzy_prefix_length

  • fuzzy_transpositions

  • lenient

  • minimum_should_match

  • quote_field_suffix

有关参数描述和支持的值,请参阅 simple_query_string 查询文档

在 SQL 和 PPL 查询中使用 simple_query_string示例

REST API 搜索请求

GET accounts/_search
{
  "query": {
    "simple_query_string": {
      "query": "Lane Street",
      "fields": [ "address" ],
    }
  }
}

可以通过 SQL 调用

SELECT account_number, address
FROM accounts
WHERE simple_query_string(['address'], 'Lane Street', default_operator='OR')

或通过 PPL 调用

SOURCE=accounts | WHERE simple_query_string(['address'], 'Lane Street', default_operator='OR') | fields account_number, address
account_number address
1 880 Holmes Lane
6 671 Bristol Street
13 789 Madison Street

Match phrase prefix 查询

要按给定前缀搜索短语,请使用 MATCH_PHRASE_PREFIX 函数,将查询字符串中的最后一个词项转换为前缀查询。

语法

match_phrase_prefix(field_expression, query_expression[, option=<option_value>]*)

MATCH_PHRASE_PREFIX 函数允许您按任意顺序指定以下选项:

  • analyzer

  • slop

  • max_expansions

  • zero_terms_query

  • boost

有关参数描述和支持的值,请参阅 match_phrase_prefix 查询文档

在 SQL 和 PPL 查询中使用 match_phrase_prefix示例

REST API 搜索请求

GET accounts/_search
{
  "query": {
    "match_phrase_prefix": {
      "author": {
        "query": "Alexander Mil"
      }
    }
  }
}

可以通过 SQL 调用

SELECT author, title
FROM books
WHERE match_phrase_prefix(author, 'Alexander Mil')

PPL 调用

source=books | where match_phrase_prefix(author, 'Alexander Mil') | fields author, title
author title
Alan Alexander Milne The House at Pooh Corner
Alan Alexander Milne Winnie-the-Pooh

Match boolean prefix 查询

使用 match_bool_prefix 函数搜索仅匹配给定字段前缀的文本的文档。

语法

match_bool_prefix(field_expression, query_expression[, option=<option_value>]*)

MATCH_BOOL_PREFIX 函数允许您按任意顺序指定以下选项:

  • minimum_should_match

  • fuzziness

  • prefix_length

  • max_expansions

  • fuzzy_transpositions

  • fuzzy_rewrite

  • boost

  • analyzer

  • operator

有关参数描述和支持的值,请参阅 match_bool_prefix 查询文档

在 SQL 和 PPL 查询中使用 match_bool_prefix 的示例:

REST API 搜索请求

GET accounts/_search
{
  "query": {
    "match_bool_prefix": {
      "address": {
        "query": "Bristol Stre"
      }
    }
  }
}

可以通过 SQL 调用

SELECT firstname, address
FROM accounts
WHERE match_bool_prefix(address, 'Bristol Stre')

PPL 调用

source=accounts | where match_bool_prefix(address, 'Bristol Stre') | fields firstname, address
firstname address
Hattie 671 Bristol Street
Nanette 789 Madison Street