Link Search Menu Expand Document Documentation Menu

匹配布尔前缀查询

match_bool_prefix 查询分析提供的搜索字符串,并从字符串的词项创建布尔查询。它将除最后一个词项外的所有词项作为完整单词进行匹配。最后一个词项则用作前缀。match_bool_prefix 查询返回包含完整单词词项或以该前缀词项开头的词项的文档,无论顺序如何。

以下示例展示了一个基本的 match_bool_prefix 查询

GET _search
{
  "query": {
    "match_bool_prefix": {
      "title": "the wind"
    }
  }
}

要传递附加参数,可以使用扩展语法

GET _search
{
  "query": {
    "match_bool_prefix": {
      "title": {
        "query": "the wind",
        "analyzer": "stop"
      }
    }
  }
}

示例

例如,考虑一个包含以下文档的索引

PUT testindex/_doc/1
{
  "title": "The wind rises"
}

PUT testindex/_doc/2
{
  "title": "Gone with the wind"
  
}

以下 match_bool_prefix 查询以任意顺序搜索完整单词 rises 和以 wi 开头的单词

GET testindex/_search
{
  "query": {
    "match_bool_prefix": {
      "title": "rises wi"
    }
  }
}

前述查询等同于以下布尔查询

GET testindex/_search
{
  "query": {
    "bool" : {
      "should": [
        { "term": { "title": "rises" }},
        { "prefix": { "title": "wi"}}
      ]
    }
  }
}

响应包含这两个文档

响应
{
  "took": 15,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 2,
      "relation": "eq"
    },
    "max_score": 1.73617,
    "hits": [
      {
        "_index": "testindex",
        "_id": "1",
        "_score": 1.73617,
        "_source": {
          "title": "The wind rises"
        }
      },
      {
        "_index": "testindex",
        "_id": "2",
        "_score": 1,
        "_source": {
          "title": "Gone with the wind"
        }
      }
    ]
  }
}

match_bool_prefixmatch_phrase_prefix 查询

match_bool_prefix 查询匹配任意位置的词项,而 match_phrase_prefix 查询则将词项作为完整短语进行匹配。为了说明两者之间的差异,请再次考虑前一节中的 match_bool_prefix 查询

GET testindex/_search
{
  "query": {
    "match_bool_prefix": {
      "title": "rises wi"
    }
  }
}

无论是 The wind rises 还是 Gone with the wind 都与搜索词项匹配,因此该查询会返回这两个文档。

现在,在同一索引上运行 match_phrase_prefix 查询

GET testindex/_search
{
  "query": {
    "match_phrase_prefix": {
      "title": "rises wi"
    }
  }
}

响应未返回任何文档,因为所有文档中都没有按指定顺序包含短语 rises wi

分析器

默认情况下,当您对 text 字段运行查询时,搜索文本会使用与该字段关联的索引分析器进行分析。您可以在 analyzer 参数中指定不同的搜索分析器

GET testindex/_search
{
  "query": {
    "match_bool_prefix": {
      "title": {
        "query": "rise the wi",
        "analyzer": "stop"
      }
    }
  }
}

参数

查询接受字段名称 (<field>) 作为顶级参数

GET _search
{
  "query": {
    "match_bool_prefix": {
      "<field>": {
        "query": "text to search for",
        ... 
      }
    }
  }
}

<field> 接受以下参数。除 query 外,所有参数均为可选。

参数 数据类型 描述
查询 字符串 用于搜索的文本、数字、布尔值或日期。必需。
分析器 字符串 用于对查询字符串文本进行分词的分析器。默认值是为 default_field 指定的索引时分析器。如果 default_field 未指定分析器,则 analyzer 为索引的默认分析器。有关 index.query.default_field 的更多信息,请参阅动态索引级别索引设置
模糊度 AUTO0 或正整数 在确定词项是否匹配值时,将一个词更改为另一个词所需的字符编辑(插入、删除、替换)次数。例如,winedwind 之间的距离为 1。默认值 AUTO 会根据每个词项的长度选择一个值,这对于大多数用例来说都是一个不错的选择。
模糊重写 字符串 确定 OpenSearch 如何重写查询。有效值包括 constant_scorescoring_booleanconstant_score_booleantop_terms_Ntop_terms_boost_Ntop_terms_blended_freqs_N。如果 fuzziness 参数不为 0,则查询默认使用 top_terms_blended_freqs_${max_expansions}fuzzy_rewrite 方法。默认值为 constant_score
模糊转置 布尔型 fuzzy_transpositions 设置为 true(默认值)会在 fuzziness 选项的插入、删除和替换操作中添加相邻字符的交换。例如,如果 fuzzy_transpositions 为 true(交换“n”和“i”),则 windwnid 之间的距离为 1;如果为 false(删除“n”,插入“n”),则距离为 2。如果 fuzzy_transpositions 为 false,则 rewindwnidwind 的距离相同(2),尽管从更以人为中心的角度来看,wnid 明显是拼写错误。默认值对于大多数用例来说都是一个不错的选择。
最大扩展数 正整数 查询可扩展到的最大词项数。模糊查询会“扩展到”在 fuzziness 中指定的距离内的多个匹配词项。然后 OpenSearch 会尝试匹配这些词项。默认值为 50
最小匹配数 正或负整数,正或负百分比,组合 如果查询字符串包含多个搜索词项并且您使用 or 运算符,则文档被视为匹配所需的匹配词项数。例如,如果 minimum_should_match 为 2,则 wind often rising 不匹配 The Wind Rises.。如果 minimum_should_match1,则匹配。有关详细信息,请参阅最小匹配数
运算符 字符串 如果查询字符串包含多个搜索词项,则文档被视为匹配是需要所有词项都匹配(and)还是只需要一个词项匹配(or)。有效值为 orand。默认值为 or
前缀长度 非负整数 在模糊性中不考虑的前导字符数。默认值为 0

fuzzinessfuzzy_transpositionsfuzzy_rewritemax_expansionsprefix_length 参数可以应用于为除最后一个词项之外的所有词项构建的词项子查询。它们对为最后一个词项构建的前缀查询没有任何影响。

剩余 350 字符

有问题?

想要贡献?