Elasticsearch Query DSL-Full text queries

 

Full text queries 全文搜索

Intervals 允许对匹配项的顺序和接近度进行细粒度控制。

 

Match 标准的全文搜索方式

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

它背后的查询机制:字段在索引时已经由分词器分词,而查询时也对语句进行分词(默认使用跟字段或index相同的分词器,也可以指定),默认的词语匹配规则是or关系(也可以设为and,and表示所有词语都要匹配,但没有顺序关系),因此搜索到的结果实际就是匹配到的词语多和少的问题,可以使用minimum_should_match控制最少匹配数量。

match块内的参数支持:

<field>
必须,查询的目标字段。

<field>块内支持的参数:

query
  必须,查询的条件,查询前会先进行分词,可以是text、number、boolean、date。
analyzer
  可选(string),对query的分词器,默认是目标字段的analyzer或index的analyzer
auto_generate_synonyms_phrase_query

(可选, Boolean,默认true)如果为true,将自动为多术语同义词创建 match phrase 。

fuzziness
(可选, string) 最大fuzziness可编辑距离。数字或AUTO。
max_expansions
(可选, integer) Maximum number of terms to which the query will expand. Defaults to 50.
prefix_length
(可选, integer) 模糊匹配保持不变的起始字符数。默认值为0。
fuzzy_transpositions
(可选, Boolean,默认true) 如果为true,则模糊匹配的编辑包括两个相邻字符(ab)的换位→ ba)。
fuzzy_rewrite

(Optional, string) Method used to rewrite the query. See the rewrite parameter for valid values and more information.

If the fuzziness parameter is not 0, the match query uses a fuzzy_rewrite method of top_terms_blended_freqs_${max_expansions} by default.

lenient
(Optional, Boolean) If true, format-based errors, such as providing a text query value for a numeric field, are ignored. Defaults to false.
operator

(可选, string) OR 或 AND ,默认OR

minimum_should_match

(可选, string) 最少匹配数量。

zero_terms_query

(Optional, string) 如果分词器删除所有tokens(例如使用stop filter时),是否不返回任何文档。默认none表示不返回,可选all表示返回全部文档

 

Match boolean prefix 实际上该名称就是Match 的 Match prefix版

对条件进行分词,转为多个terms,并对最后一个term使用前缀查询,

GET /_search
{
  "query": {
    "match_bool_prefix" : {
      "message" : "quick brown f"
    }
  }
}
等同于
GET /_search
{
  "query": {
    "bool" : {
      "should": [
        { "term": { "message": "quick" }},
        { "term": { "message": "brown" }},
        { "prefix": { "message": "f"}}
      ]
    }
  }
}

可以使用参数:

operator 、analyzer、 minimum_should_match

 

Match phrase  

以短语相邻的要求进行搜索,有顺序性,可以使用slop控制相邻度,默认slop是0。

对条件中的分词要全部匹配。

例如对于 this is a test 可以使用

{
  "query": {
    "match_phrase": {
      "message": {
          "query":"this test",
          "slop":2
      }
    }
  }
}
由于顺序性,上面的查询不会匹配 test a is this 
 
 
 

Match phrase prefix 

如同Match boolean prefix ,对最后一个词语使用前缀查询

GET /_search
{
  "query": {
    "match_phrase_prefix": {
      "message": {
        "query": "quick brown f"
      }
    }
  }
}

支持以下参数:

query 必须

analyzer(可选, string)

max_expansions(可选, integer) Maximum number of terms to which the last provided term of the query value will expand. Defaults to 50.

slop(可选, integer) Maximum number of positions allowed between matching tokens. Defaults to 0. Transposed terms have a slop of 2.

zero_terms_query (可选, string)  none默认,all

 

Combined fields 

允许对多个字段进行全文搜索。

GET /_search
{
  "query": {
    "combined_fields" : {
      "query":      "database systems",
      "fields":     [ "title", "abstract", "body"],
      "operator":   "and"
    }
  }
}

支持参数:

fields(必须, array of strings) 一组需要查询的字段,只能是text类型,他们必须具有相同的analyzer 。

query(必须, string) 

auto_generate_synonyms_phrase_query(可选, Boolean) If truematch phrase queries are automatically created for multi-term synonyms. Defaults to true.

operator(可选, string) or默认,and

minimum_should_match(可选, string) 

zero_terms_query(可选, string) none默认,all

 

Multi-match 允许对多个字段mtach查询

GET /_search
{
  "query": {
    "multi_match" : {
      "query" : "this is a test",
      "fields" : [ "subject^3", "message","*_name" ] 
    }
  }
}
如果fields不指定字段,则使用 index.query.default_field ,即默认是 ** 

multi_match 支持的类型:

best_fields

(默认) 文档匹配任意字段,评分选择最佳匹配的字段 best_fields.

most_fields

文档匹配任意字段,评分从各个匹配的字段进行组合 most_fields.

cross_fields

使用相同的分析器处理字段,就像它们是一个大字段一样。在任何字段中查找每个单词。 cross_fields.

phrase

使用 match_phrase匹配任意字段,评分选择最佳匹配的字段 phrase and phrase_prefix.

phrase_prefix

使用 match_phrase_prefix 匹配任意字段,评分选择最佳匹配的字段 phrase and phrase_prefix.

bool_prefix

使用 match_bool_prefix 匹配任意字段,评分从各个匹配的字段进行组合 bool_prefix.

 

Query string query 可以使用query_string查询创建包含通配符、跨多个字段搜索等的复杂搜索。虽然通用,但查询是严格的,如果查询字符串包含任何无效语法,则返回错误。因为它会为任何无效语法返回一个错误,所以不建议对搜索框使用query string查询。

语法:查询字符串被解析为一系列term和operator,查询字符串可以是单词或短语,例如"quick brown"将以phrase方式顺序性的搜索。

未完待续...

 

 

Simple query string 使用语法有限但容错的解析器。虽然它的语法比query string查询更受限制,但simple query string查询不会返回无效语法的错误。相反,它会忽略查询字符串的任何无效部分。

未完待续...

 

posted on 2021-11-03 15:28  icodegarden  阅读(49)  评论(0编辑  收藏  举报