DSL 介绍

  • 这个才是实际最常用的方式,可以构建复杂的查询条件。
  • 不用一开始就想着怎样用 Java Client 端去调用 Elasticsearch 接口。DSL 会了,Client 的也只是用法问题而已。

    DSL 语句的校验以及 score 计算原理

  • 对于复杂的查询,最好都先校验下,看有没有报错。

    1. GET /product_index/product/_validate/query?explain
    2. {
    3. "query": {
    4. "match": {
    5. "product_name": "toothbrush"
    6. }
    7. }
    8. }

    DSL 简单用法

  • 查询所有的商品:

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "match_all": {}
    5. }
    6. }
  • 查询商品名称包含 toothbrush 的商品,同时按照价格降序排序:

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "match": {
    5. "product_name": "toothbrush"
    6. }
    7. },
    8. "sort": [
    9. {
    10. "price": "desc"
    11. }
    12. ]
    13. }
  • 分页查询商品:

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "match_all": {}
    5. },
    6. "from": 0, ## 从第几个商品开始查,最开始是 0
    7. "size": 1 ## 要查几个结果
    8. }
  • 指定查询结果字段(field):

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "match_all": {}
    5. },
    6. "_source": [
    7. "product_name",
    8. "price"
    9. ]
    10. }
  • 搜索商品名称包含 toothbrush,而且售价大于 400 元,小于 700 的商品

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": {
    6. "match": {
    7. "product_name": "toothbrush"
    8. }
    9. },
    10. "filter": {
    11. "range": {
    12. "price": {
    13. "gt": 400,
    14. "lt": 700
    15. }
    16. }
    17. }
    18. }
    19. }
    20. }
  • full-text search 全文检索,倒排索引

  • 索引中只要有任意一个匹配拆分后词就可以出现在结果中,只是匹配度越高的排越前面
  • 比如查询:PHILIPS toothbrush,会被拆分成两个单词:PHILIPS 和 toothbrush。只要索引中 product_name 中只要含有任意对应单词,都会在搜索结果中,只是如果有数据同时含有这两个单词,则排序在前面。

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "match": {
    5. "product_name": "PHILIPS toothbrush"
    6. }
    7. }
    8. }
  • phrase search 短语搜索

  • 索引中必须同时匹配拆分后词就可以出现在结果中
  • 比如查询:PHILIPS toothbrush,会被拆分成两个单词:PHILIPS 和 toothbrush。索引中必须有同时有这两个单词的才会在结果中。
    ```json GET /product_index/product/_search { “query”: { “match_phrase”: {
    1. "product_name": "PHILIPS toothbrush"
    } } }

    完全匹配可能比较严,我们会希望有个可调节因子,少匹配一个也满足,那就需要使用到slop。

{ “query”: { “match_phrase”: { “content” : { “query” : “我的宝马多少马力”, “slop” : 1 } } } }

  1. - Highlight Search 高亮搜索<br />
  2. - 给匹配拆分后的查询词增加高亮的 html 标签,比如这样的结果:`"<em>PHILIPS</em> <em>toothbrush</em> HX6730/02"`<br />
  3. ```json
  4. GET /product_index/product/_search
  5. {
  6. "query": {
  7. "match": {
  8. "product_name": "PHILIPS toothbrush"
  9. }
  10. },
  11. "highlight": {
  12. "fields": {
  13. "product_name": {}
  14. }
  15. }
  16. }
  • range 用法,查询数值、时间区间:

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "range": {
    5. "price": {
    6. "gte": 30.00
    7. }
    8. }
    9. }
    10. }
  • match 还有一种情况,就是必须满足分词结果中所有的词,而不是像上面,任意一个就可以的。(这个常见,所以很重要

  • 看下面的 JSON 其实你也可以猜出来,其实上面的 JSON 和下面的 JSON 本质是:operator 的差别,上面是 or,下面是 and 关系。

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "match": {
    5. "product_name": {
    6. "query": "PHILIPS toothbrush",
    7. "operator": "and"
    8. }
    9. }
    10. }
    11. }
  • match 还还有一种情况,就是必须满足分词结果中百分比的词,比如搜索词被分成这样子:java 程序员 书 推荐,这里就有 4 个词,假如要求 50% 命中其中两个词就返回,我们可以这样:

  • 当然,这种需求也可以用 must、must_not、should 匹配同一个字段进行组合来查询

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "match": {
    5. "product_name": {
    6. "query": "java 程序员 书 推荐",
    7. "minimum_should_match": "50%"
    8. }
    9. }
    10. }
    11. }
  • multi_match 用法:

  • 查询 product_name 和 product_desc 字段中,只要有:toothbrush 关键字的就查询出来。

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "multi_match": {
    5. "query": "toothbrush",
    6. "fields": [
    7. "product_name",
    8. "product_desc"
    9. ]
    10. }
    11. }
    12. }
  • multi_match 跨多个 field 查询,表示查询分词必须出现在相同字段中。

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "multi_match": {
    5. "query": "PHILIPS toothbrush",
    6. "type": "cross_fields",
    7. "operator": "and",
    8. "fields": [
    9. "product_name",
    10. "product_desc"
    11. ]
    12. }
    13. }
    14. }
  • match_phrase 用法(短语搜索)(与 match 进行对比):

  • 对这个查询词不进行分词,必须完全匹配查询词才可以作为结果显示。

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "match_phrase": {
    5. "product_name": "PHILIPS toothbrush"
    6. }
    7. }
    8. }
  • match_phrase + slop(与 match_phrase 进行对比):

  • 在说 slop 的用法之前,需要先说明原数据是:PHILIPS toothbrush HX6730/02,被分词后至少有:PHILIPS,toothbrush,HX6730 三个 term。
  • match_phrase 的用法我们上面说了,按理说查询的词必须完全匹配才能查询到,PHILIPS HX6730 很明显是不完全匹配的。
  • 但是有时候我们就是要这种不完全匹配,只要求他们尽可能靠谱,中间有几个单词是没啥问题的,那就可以用到 slop。slop = 2 表示中间如果间隔 2 个单词以内也算是匹配的结果()。
  • 其实也不能称作间隔,应该说是移位,查询的关键字分词后移动多少位可以跟 doc 内容匹配,移动的次数就是 slop。所以 HX6730 PHILIPS 其实也是可以匹配到 doc 的,只是 slop = 5 才行。

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "match_phrase": {
    5. "product_name" : {
    6. "query" : "PHILIPS HX6730",
    7. "slop" : 1
    8. }
    9. }
    10. }
    11. }
  • match + match_phrase + slop 组合查询,使查询结果更加精准和结果更多

  • 但是 match_phrase 性能没有 match 好,所以一般需要先用 match 第一步进行过滤,然后在用 match_phrase 进行进一步匹配,并且重新打分,这里又用到了:rescore,window_size 表示对前 10 个进行重新打分
  • 下面第一个是未重新打分的,第二个是重新打分的 ```json GET /product_index/product/_search { “query”: { “bool”: {
    1. "must": {
    2. "match": {
    3. "product_name": {
    4. "query": "PHILIPS HX6730"
    5. }
    6. }
    7. },
    8. "should": {
    9. "match_phrase": {
    10. "product_name": {
    11. "query": "PHILIPS HX6730",
    12. "slop": 10
    13. }
    14. }
    15. }
    } } }

GET /product_index/product/_search { “query”: { “match”: { “product_name”: “PHILIPS HX6730” } }, “rescore”: { “window_size”: 10, “query”: { “rescore_query”: { “match_phrase”: { “product_name”: { “query”: “PHILIPS HX6730”, “slop”: 10 } } } } } }

  1. - match_phrase_prefix 用法(不常用),一般用于类似 Google 搜索框,关键字输入推荐
  2. - max_expansions 用来限定最多匹配多少个 term,优化性能
  3. - 但是总体来说性能还是很差,因为还是会扫描整个倒排索引。推荐用 edge_ngram 做该功能
  4. ```json
  5. GET /product_index/product/_search
  6. {
  7. "query": {
  8. "match_phrase_prefix": {
  9. "product_name": "PHILIPS HX",
  10. "slop": 5,
  11. "max_expansions": 20
  12. }
  13. }
  14. }
  • term 用法(与 match 进行对比)(term 一般用在不分词字段上的,因为它是完全匹配查询,如果要查询的字段是分词字段就会被拆分成各种分词结果,和完全查询的内容就对应不上了。):
  • 所以自己设置 mapping 的时候有些不分词的时候就最好设置不分词。
  • 其实 Elasticsearch 5.X 之后给 text 类型的分词字段,又默认新增了一个子字段 keyword,这个字段的类型就是 keyword,是不分词的,默认保留 256 个字符。假设 product_name 是分词字段,那有一个 product_name.keyword 是不分词的字段,也可以用这个子字段来做完全匹配查询。 ```json GET /product_index/product/_search { “query”: { “term”: {
    1. "product_name": "PHILIPS toothbrush" ##这种写法如果product_name是一个分词,将不会查找到任何数据,因为分词会将"PHILIPS toothbrush"切分成两个单词 [PHILIPS,toothbrush] 存储
    } } }

GET /product_index/product/_search { “query”: { “constant_score”: { “filter”:{ “term”: { “product_name”: “PHILIPS toothbrush” } } } } }

  1. - terms 用法,类似于数据库的 in
  2. ```json
  3. GET /product_index/product/_search
  4. {
  5. "query": {
  6. "constant_score": {
  7. "filter": {
  8. "terms": {
  9. "product_name": [ ##匹配该name包括如下值的文档
  10. "toothbrush",
  11. "shell"
  12. ]
  13. }
  14. }
  15. }
  16. }
  17. }

query 和 filter 差异

  • 只用 query:

    1. GET /product_index/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": [
    6. {
    7. "terms": {
    8. "product_name": [
    9. "PHILIPS",
    10. "toothbrush"
    11. ]
    12. }
    13. },
    14. {
    15. "range": {
    16. "price": {
    17. "gt": 12.00
    18. }
    19. }
    20. }
    21. ]
    22. }
    23. }
    24. }
  • 只用 filter:

  • 下面语句使用了 constant_score 查询,它可以包含查询或过滤,为任意一个匹配的文档指定评分 1 ,忽略 TF/IDF 信息,不需再计算评分。
  • 也还可以指定分数:https://www.elastic.co/guide/cn/elasticsearch/guide/current/ignoring-tfidf.html

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "constant_score": {
    5. "filter": {
    6. "range": {
    7. "price": {
    8. "gte": 30.00
    9. }
    10. }
    11. }
    12. }
    13. }
    14. }
  • query 和 filter 一起使用的话,filter 会先执行,看本文下面的:多搜索条件组合查询

  • 官网文档:https://www.elastic.co/guide/en/elasticsearch/guide/current/_queries_and_filters.html
  • 从搜索结果上看:
    • filter,只查询出搜索条件的数据,不计算相关度分数
    • query,查询出搜索条件的数据,并计算相关度分数,按照分数进行倒序排序
  • 从性能上看:

    • filter(性能更好,无排序),无需计算相关度分数,也就无需排序,内置的自动缓存最常使用查询结果的数据
    • query(性能较差,有排序),要计算相关度分数,按照分数进行倒序排序,没有缓存结果的功能
    • filter 和 query 一起使用可以兼顾两者的特性,所以看你业务需求。324

      多搜索条件组合查询(最常用)

  • bool 下包括:must(必须匹配,类似于数据库的 =),must_not(必须不匹配,类似于数据库的 !=),should(没有强制匹配,类似于数据库的 or),filter(过滤) ```json GET /product_index/product/_search { “query”: { “bool”: {

    1. "must": [
    2. {
    3. "match": {
    4. "product_name": "PHILIPS toothbrush"
    5. }
    6. }
    7. ],
    8. "should": [
    9. {
    10. "match": {
    11. "product_desc": "刷头"
    12. }
    13. }
    14. ],
    15. "must_not": [
    16. {
    17. "match": {
    18. "product_name": "HX6730"
    19. }
    20. }
    21. ],
    22. "filter": {
    23. "range": {
    24. "price": {
    25. "gte": 33.00
    26. }
    27. }
    28. }

    } } }

GET /product_index/product/_search { “query”: { “bool”: { “should”: [ { “term”: { “product_name”: “飞利浦” } }, { “bool”: { “must”: [ { “term”: { “product_desc”: “刷头” }, “term”: { “price”: 30 } } ] } } ] } } }

  1. - should 有一个特殊性,如果组合查询中没有 must 条件,那么 should 中必须至少匹配一个。我们也还可以通过 minimum_should_match 来限制它匹配更多个。
  2. ```json
  3. GET /product_index/product/_search
  4. {
  5. "query": {
  6. "bool": {
  7. "should": [
  8. {
  9. "match": {
  10. "product_name": "java"
  11. }
  12. },
  13. {
  14. "match": {
  15. "product_name": "程序员"
  16. }
  17. },
  18. {
  19. "match": {
  20. "product_name": "书"
  21. }
  22. },
  23. {
  24. "match": {
  25. "product_name": "推荐"
  26. }
  27. }
  28. ],
  29. "minimum_should_match": 3
  30. }
  31. }
  32. }
  • 下面还用到自定义排序。
  • 排序最好别用到字符串字段上。因为字符串字段会进行分词,Elasticsearch 默认是拿分词后的某个词去进行排序,排序结果往往跟我们想象的不一样。解决这个办法是在设置 mapping 的时候,多个这个字段设置一个 fields raw,让这个不进行分词,然后查询排序的时候使用这个 raw,具体看这里:https://www.elastic.co/guide/cn/elasticsearch/guide/current/multi-fields.html

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": [
    6. {
    7. "match": {
    8. "product_name": "PHILIPS toothbrush"
    9. }
    10. }
    11. ],
    12. "should": [
    13. {
    14. "match": {
    15. "product_desc": "刷头"
    16. }
    17. }
    18. ],
    19. "filter": {
    20. "bool": {
    21. "must": [
    22. {
    23. "range": {
    24. "price": {
    25. "gte": 33.00
    26. }
    27. }
    28. },
    29. {
    30. "range": {
    31. "price": {
    32. "lte": 555.55
    33. }
    34. }
    35. }
    36. ],
    37. "must_not": [
    38. {
    39. "term": {
    40. "product_name": "HX6730"
    41. }
    42. }
    43. ]
    44. }
    45. }
    46. }
    47. },
    48. "sort": [
    49. {
    50. "price": {
    51. "order": "desc"
    52. }
    53. }
    54. ]
    55. }
  • prefix 前缀搜索(性能较差,扫描所有倒排索引)

  • 比如有一个不分词字段 product_name,分别有两个 doc 是:iphone-6,iphone-7。我们搜索 iphone 这个前缀关键字就可以搜索到结果

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "prefix": {
    5. "product_name": {
    6. "value": "iphone"
    7. }
    8. }
    9. }
    10. }
  • 通配符搜索(性能较差,扫描所有倒排索引)

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "wildcard": {
    5. "product_name": {
    6. "value": "ipho*"
    7. }
    8. }
    9. }
    10. }
  • 正则搜索(性能较差,扫描所有倒排索引)

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "regexp": {
    5. "product_name": "iphone[0-9].+"
    6. }
    7. }
    8. }
  • fuzzy 纠错查询

  • 参数 fuzziness 默认是 2,表示最多可以纠错两次,但是这个值不能很大,不然没效果。一般 AUTO 是自动纠错。
  • 下面的关键字漏了一个字母 o。

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "match": {
    5. "product_name": {
    6. "query": "PHILIPS tothbrush",
    7. "fuzziness": "AUTO",
    8. "operator": "and"
    9. }
    10. }
    11. }
    12. }
  • boost 用法(默认是 1)。在搜索精准度的控制上,还有一种需求,比如搜索:PHILIPS toothbrush,要比:Braun toothbrush 更加优先,我们可以这样:

    1. GET /product_index/product/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": [
    6. {
    7. "match": {
    8. "product_name": "toothbrush"
    9. }
    10. }
    11. ],
    12. "should": [
    13. {
    14. "match": {
    15. "product_name": {
    16. "query": "PHILIPS",
    17. "boost": 4
    18. }
    19. }
    20. },
    21. {
    22. "match": {
    23. "product_name": {
    24. "query": "Braun",
    25. "boost": 3
    26. }
    27. }
    28. }
    29. ]
    30. }
    31. }
    32. }
  • dis_max 用法,也称作:best fields 策略。

  • 由于查询关键字是会被分词的,默认 query bool 查询多个字段的语法时候,每个字段匹配到一个或多个的时候分数比:一个字段匹配到查询分词的所有结果的分数来的大。但是对于我们来讲这样的不够精准的。所以我们希望查询字段中,匹配的关键字越多排序越靠前,而不是每个字段查询了一个分词就排前,我们可以使用 dis_max。
  • 但是使用 dis_max,一般还不够,建议再加上 tie_breaker。
  • tie_breaker 是一个小数值,在 0~1 之间用来将其他查询结果分数,乘以 tie_breaker 的值,然后再综合与 dis_max 最高分数的的分数一起进行计算。除了取 dis_max 的最高分以外,还会考虑其他的查询结果的分数。
  • 在 dis_max 基础上,为了增加精准,我们还可以加上:boost、minimum_should_match 等相关参数。其中 minimum_should_match 比较常用,因为查询字段的分词中如果只有一个分词查询上了这种结果基本是没啥用的。
  • 官网资料:https://www.elastic.co/guide/en/elasticsearch/guide/current/_best_fields.html ```json GET /product_index/product/_search { “query”: { “dis_max”: {
    1. "queries": [
    2. {
    3. "match": {
    4. "product_name": "PHILIPS toothbrush"
    5. }
    6. },
    7. {
    8. "match": {
    9. "product_desc": "iphone shell"
    10. }
    11. }
    12. ],
    13. "tie_breaker": 0.2
    } } }

GET /product_index/product/_search { “query”: { “dis_max”: { “queries”: [ { “match”: { “product_name”: { “query”: “PHILIPS toothbrush”, “minimum_should_match”: “50%”, “boost”: 3 } } }, { “match”: { “product_desc”: { “query”: “iphone shell”, “minimum_should_match”: “50%”, “boost”: 2 } } } ], “tie_breaker”: 0.3 } } } ```

其他一些辅助文档
https://www.cnblogs.com/yjf512/p/4897294.html