🥖ElasticSearch-07-高级查询之全文搜索

DSL查询极为常用的是对文本进行搜索,也叫做叫全文搜索,这里主要涉及的是我们之前说过的matchqueryInterval

🧿Match类型

第一类:match 类型

1️⃣ match 查询的步骤


  • 准备一些数据

这里我们准备一些数据,通过实例看match 查询的步骤

  1. PUT /test-dsl-match
  2. { "settings": { "number_of_shards": 1 }}
  3. POST /test-dsl-match/_bulk
  4. { "index": { "_id": 1 }}
  5. { "title": "The quick brown fox" }
  6. { "index": { "_id": 2 }}
  7. { "title": "The quick brown fox jumps over the lazy dog" }
  8. { "index": { "_id": 3 }}
  9. { "title": "The quick brown fox jumps over the quick dog" }
  10. { "index": { "_id": 4 }}
  11. { "title": "Brown fox brown dog" }
  • 查询数据
  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "QUICK!"
  6. }
  7. }
  8. }

Elasticsearch 执行上面这个 match 查询的步骤是:

  1. 检查字段类型

标题 title 字段是一个 string 类型( analyzed )已分析的全文字段,这意味着查询字符串本身也应该被分析。

  1. 分析查询字符串

将查询的字符串 QUICK! 传入标准分析器中,输出的结果是单个项 quick 。因为只有一个单词项,所以 match 查询执行的是单个底层 term 查询。

  1. 查找匹配文档

用 term 查询在倒排索引中查找 quick 然后获取一组包含该项的文档,本例的结果是文档:1、2 和 3 。

  1. 为每个文档评分

用 term 查询计算每个文档相关度评分 _score ,这是种将词频(term frequency,即词 quick 在相关文档的 title 字段中出现的频率)和反向文档频率(inverse document frequency,即词 quick 在所有文档的 title 字段中出现的频率),以及字段的长度(即字段越短相关度越高)相结合的计算方式。

  • 验证结果

ElasticSearch-07-高级查询之全文搜索 - 图1

2️⃣ match多个词深入


我们在上文中复合查询中已经使用了match多个词,比如“Quick pets”; 这里我们通过例子带你更深入理解match多个词

  • match多个词的本质

查询多个词”BROWN DOG!”

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": "BROWN DOG"
  6. }
  7. }
  8. }

ElasticSearch-07-高级查询之全文搜索 - 图2

因为 match 查询必须查找两个词( [“brown”,”dog”] ),它在内部实际上先执行两次 term 查询,然后将两次查询的结果合并作为最终结果输出。为了做到这点,它将两个 term 查询包入一个 bool 查询中

所以上述查询的结果,和如下语句查询结果是等同的

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "should": [
  6. {
  7. "term": {
  8. "title": "brown"
  9. }
  10. },
  11. {
  12. "term": {
  13. "title": "dog"
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }

ElasticSearch-07-高级查询之全文搜索 - 图3

  • match多个词的逻辑

上面等同于should(任意一个满足),是因为 match还有一个operator参数,默认是or, 所以对应的是should。

所以上述查询也等同于

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": {
  6. "query": "BROWN DOG",
  7. "operator": "or"
  8. }
  9. }
  10. }
  11. }

那么我们如果是需要and操作呢,即同时满足呢?

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": {
  6. "query": "BROWN DOG",
  7. "operator": "and"
  8. }
  9. }
  10. }
  11. }

等同于

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "term": {
  8. "title": "brown"
  9. }
  10. },
  11. {
  12. "term": {
  13. "title": "dog"
  14. }
  15. }
  16. ]
  17. }
  18. }
  19. }

ElasticSearch-07-高级查询之全文搜索 - 图4

3️⃣ 控制match的匹配精度


如果用户给定 3 个查询词,想查找至少包含其中 2 个的文档,该如何处理?将 operator 操作符参数设置成 and 或者 or 都是不合适的。

match 查询支持 minimum_should_match 最小匹配参数,这让我们可以指定必须匹配的词项数用来表示一个文档是否相关。我们可以将其设置为某个具体数字,更常用的做法是将其设置为一个百分数,因为我们无法控制用户搜索时输入的单词数量:

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match": {
  5. "title": {
  6. "query": "quick brown dog",
  7. "minimum_should_match": "75%"
  8. }
  9. }
  10. }
  11. }

当给定百分比的时候, minimum_should_match 会做合适的事情:

  1. 在之前三词项的示例中, 75% 会自动被截断成 66.6% ,即三个里面两个词。无论这个值设置成什么,至少包含一个词项的文档才会被认为是匹配的。

ElasticSearch-07-高级查询之全文搜索 - 图5

当然也等同于

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "should": [
  6. {
  7. "match": {
  8. "title": "quick"
  9. }
  10. },
  11. {
  12. "match": {
  13. "title": "brown"
  14. }
  15. },
  16. {
  17. "match": {
  18. "title": "dog"
  19. }
  20. }
  21. ],
  22. "minimum_should_match": 2
  23. }
  24. }
  25. }

ElasticSearch-07-高级查询之全文搜索 - 图6

4️⃣ 其它match类型


  • match_pharse

match_phrase在前文中我们已经有了解,我们再看下另外一个例子。

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "title": {
  6. "query": "quick brown"
  7. }
  8. }
  9. }
  10. }

ElasticSearch-07-高级查询之全文搜索 - 图7

很多人对它仍然有误解的,比如如下例子:

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "title": {
  6. "query": "quick brown f"
  7. }
  8. }
  9. }
  10. }

这样的查询是查不出任何数据的,因为前文中我们知道了match本质上是对term组合,match_phrase本质是连续的term的查询,所以f并不是一个分词,不满足term查询,所以最终查不出任何内容了。

ElasticSearch-07-高级查询之全文搜索 - 图8

  • match_pharse_prefix

那有没有可以查询出quick brown f的方式呢?ELasticSearch在match_phrase基础上提供了一种可以查最后一个词项是前缀的方法,这样就可以查询quick brown f

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match_phrase_prefix": {
  5. "title": {
  6. "query": "quick brown f"
  7. }
  8. }
  9. }
  10. }

ElasticSearch-07-高级查询之全文搜索 - 图9

ps: prefix的意思不是整个text的开始匹配,而是最后一个词项满足term的prefix查询而已

  • match_bool_prefix

除了match_phrase_prefix,ElasticSearch还提供了match_bool_prefix查询

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "match_bool_prefix": {
  5. "title": {
  6. "query": "quick brown f"
  7. }
  8. }
  9. }
  10. }

ElasticSearch-07-高级查询之全文搜索 - 图10)

它们两种方式有啥区别呢?match_bool_prefix本质上可以转换为:

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "bool" : {
  5. "should": [
  6. { "term": { "title": "quick" }},
  7. { "term": { "title": "brown" }},
  8. { "prefix": { "title": "f"}}
  9. ]
  10. }
  11. }
  12. }

所以这样你就能理解,match_bool_prefix查询中的quick,brown,f是无序的。

  • multi_match

如果我们期望一次对多个字段查询,怎么办呢?ElasticSearch提供了multi_match查询的方式

  1. GET /ralph_index/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "Mullen",
  6. "fields": ["address","*name"]
  7. }
  8. }
  9. }

查询地址和名字中包含mullen的数据,*表示前缀匹配字段。如果不用*,就是”fields”: ["address","*firstname","lastname"]

ElasticSearch-07-高级查询之全文搜索 - 图11

🧿 query string类型

第二类:query string 类型

1️⃣ query_string

此查询使用语法根据运算符(例如AND或)来解析和拆分提供的查询字符串。然后查询在返回匹配的文档之前独立分析每个拆分的文本。

可以使用该query_string查询创建一个复杂的搜索,其中包括通配符,跨多个字段的搜索等等。尽管用途广泛,但查询是严格的,如果查询字符串包含任何无效语法,则返回错误。

例如:

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "query_string": {
  5. "query": "(lazy dog) OR (brown dog)",
  6. "default_field": "title"
  7. }
  8. }
  9. }

这里查询结果,你需要理解本质上查询这四个分词(term)or的结果而已,所以doc 3和4也在其中

ElasticSearch-07-高级查询之全文搜索 - 图12

对构筑知识体系已经够了,但是它其实还有很多参数和用法,更多请参考官网

2️⃣ query_string_simple

该查询使用一种简单的语法来解析提供的查询字符串并将其拆分为基于特殊运算符的术语。然后查询在返回匹配的文档之前独立分析每个术语。

尽管其语法比query_string查询更受限制 ,但simple_query_string 查询不会针对无效语法返回错误。而是,它将忽略查询字符串的任何无效部分

举例:

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "simple_query_string": {
  5. "query": "\"over the\" + (lazy | quick) + dog",
  6. "fields": [
  7. "title"
  8. ],
  9. "default_operator": "and"
  10. }
  11. }
  12. }

ElasticSearch-07-高级查询之全文搜索 - 图13

更多请参考官网

🧿 Interval类型

第三类:interval类型

Intervals是时间间隔的意思,本质上将多个规则按照顺序匹配。

比如:

  1. GET /test-dsl-match/_search
  2. {
  3. "query": {
  4. "intervals" : {
  5. "title" : {
  6. "all_of" : {
  7. "ordered" : true,
  8. "intervals" : [
  9. {
  10. "match" : {
  11. "query" : "quick",
  12. "max_gaps" : 0,
  13. "ordered" : true
  14. }
  15. },
  16. {
  17. "any_of" : {
  18. "intervals" : [
  19. { "match" : { "query" : "jump over" } },
  20. { "match" : { "query" : "quick dog" } }
  21. ]
  22. }
  23. }
  24. ]
  25. }
  26. }
  27. }
  28. }
  29. }

ElasticSearch-07-高级查询之全文搜索 - 图14

因为interval之间是可以组合的,所以它可以表现的很复杂。更多请参考官网

文件转载