当前位置:   article > 正文

Elasticsearch复合查询(一)_{ "unmapped_type": "boolean" }

{ "unmapped_type": "boolean" }

JSON 文档格式

  1. {
  2. "_index":"zipkin-2017-09-06",
  3. "_type":"span",
  4. "_id":"AV5WSb1lKwYfgxikh_Fp",
  5. "_score":null,
  6. "_source":{
  7. "timestamp_millis":1504686226897,
  8. "traceId":"58d858be36d2493e",
  9. "id":"eb5e8ee2ff39eaa7",
  10. "name":"close",
  11. "parentId":"47622e0c4229a48b",
  12. "timestamp":1504686226897000,
  13. "duration":2,
  14. "binaryAnnotations":[
  15. {
  16. "key":"ip",
  17. "value":"127.0.0.1",
  18. "endpoint":{
  19. "serviceName":"redis",
  20. "ipv4":"127.0.0.1",
  21. "port":20880
  22. }
  23. },
  24. {
  25. "key":"lc",
  26. "value":"unknown",
  27. "endpoint":{
  28. "serviceName":"redis",
  29. "ipv4":"127.0.0.1",
  30. "port":20880
  31. }
  32. },
  33. {
  34. "key":"service",
  35. "value":"redis",
  36. "endpoint":{
  37. "serviceName":"redis",
  38. "ipv4":"127.0.0.1",
  39. "port":20880
  40. }
  41. }
  42. ]
  43. },
  44. "fields":{
  45. "timestamp_millis":[
  46. 1504686226897
  47. ]
  48. },
  49. "sort":[
  50. 1504686226897
  51. ]
  52. }

1.OR条件查询格式

  1. {
  2. "query":{
  3. "bool":{
  4. "should":[
  5. {},
  6. {},
  7. {},
  8. ...
  9. ]
  10. }
  11. },
  12. "size":400,
  13. "from":0,
  14. "sort":[
  15. {
  16. "timestamp":{
  17. "order":"desc",
  18. "unmapped_type":"boolean"
  19. }
  20. }
  21. ]
  22. }

 should条件的意思就只要匹配到里面其中一个条件就可以命中, 如

  1. {
  2. "query":{
  3. "bool":{
  4. "should":[
  5. {
  6. "match":{
  7. "traceId":"6edb691b4bc775b1"
  8. }
  9. },
  10. {
  11. "match":{
  12. "traceId":"7e5b391r4bc775b1"
  13. }
  14. }
  15. ]
  16. }
  17. },
  18. "size":400,
  19. "from":0,
  20. "sort":[
  21. {
  22. "timestamp":{
  23. "order":"desc",
  24. "unmapped_type":"boolean"
  25. }
  26. }
  27. ]
  28. }


 只要traceId等于其中一个值就可以命中

2.AND 条件查询格式

  1. {
  2. "query":{
  3. "bool":{
  4. "must":[
  5. {},
  6. {},
  7. {},
  8. ...
  9. ]
  10. }
  11. },
  12. "size":400,
  13. "from":0,
  14. "sort":[
  15. {
  16. "timestamp":{
  17. "order":"desc",
  18. "unmapped_type":"boolean"
  19. }
  20. }
  21. ]
  22. }

must条件的意思就是必须匹配里面的所有条件才可以命中,如 

  1. {
  2. "query":{
  3. "bool":{
  4. "must":[
  5. {
  6. "range":{
  7. "timestamp":{
  8. "gte":1504581280866000,
  9. "lte":1504581280878000,
  10. "format":"date_time_no_millis"
  11. }
  12. }
  13. },
  14. {
  15. "match":{
  16. "traceId":"6edb691b4bc775b1"
  17. }
  18. }
  19. ],
  20. "must_not":{
  21. "exists":{
  22. "field":"parentId"
  23. }
  24. }
  25. }
  26. },
  27. "size":400,
  28. "from":0,
  29. "sort":[
  30. {
  31. "timestamp":{
  32. "order":"desc",
  33. "unmapped_type":"boolean"
  34. }
  35. }
  36. ]
  37. }


 必须匹配traceId=6edb691b4bc775b1, 并且时间范围在1504581280866000,1504581280878000 must条件的意思就是必须匹配里面的所有条件才可以命中,如

3.是否含有某key

  1. {
  2. "must_not":{
  3. "exists":{
  4. "field":"parentId"
  5. }
  6. }
  7. }

 must_not意思是查询必须没有parenId这个key的数据

  1. {
  2. "query":{
  3. "bool":{
  4. "must":[
  5. {
  6. "range":{
  7. "timestamp":{
  8. "gte":1504581280866000,
  9. "lte":1504581280878000,
  10. "format":"date_time_no_millis"
  11. }
  12. }
  13. },
  14. {
  15. "match":{
  16. "traceId":"6edb691b4bc775b1"
  17. }
  18. }
  19. ],
  20. "must_not":{
  21. "exists":{
  22. "field":"parentId"
  23. }
  24. }
  25. }
  26. },
  27. "size":400,
  28. "from":0,
  29. "sort":[
  30. {
  31. "timestamp":{
  32. "order":"desc",
  33. "unmapped_type":"boolean"
  34. }
  35. }
  36. ]
  37. }

PS: 不管是must,should,must_not都是平级的,包含在bool里面

4.嵌套查询

  1. {
  2. "query":{
  3. "bool":{
  4. "must":[
  5. {
  6. "range":{
  7. "timestamp":{
  8. "gte":1504581280866000,
  9. "lte":1504581280878000,
  10. "format":"date_time_no_millis"
  11. }
  12. }
  13. },
  14. {
  15. "match":{
  16. "traceId":"6edb691b4bc775b1"
  17. }
  18. },
  19. {
  20. "nested":{
  21. "path":"binaryAnnotations",
  22. "query":{
  23. "bool":{
  24. "must":[
  25. {
  26. "match":{
  27. "binaryAnnotations.key":"service"
  28. }
  29. },
  30. {
  31. "match":{
  32. "binaryAnnotations.value":"WebRequest"
  33. }
  34. }
  35. ]
  36. }
  37. }
  38. }
  39. }
  40. ],
  41. "must_not":{
  42. "exists":{
  43. "field":"parentId"
  44. }
  45. }
  46. }
  47. },
  48. "size":400,
  49. "from":0,
  50. "sort":[
  51. {
  52. "timestamp":{
  53. "order":"desc",
  54. "unmapped_type":"boolean"
  55. }
  56. }
  57. ]
  58. }

nested嵌套查询和其他match,range条件一样,是包含在must,should这些条件里面

  1. {
  2. "nested":{
  3. "path":"binaryAnnotations",
  4. "query":{
  5. "bool":{
  6. "must":[
  7. {
  8. "match":{
  9. "binaryAnnotations.key":"service"
  10. }
  11. },
  12. {
  13. "match":{
  14. "binaryAnnotations.value":"WebRequest"
  15. }
  16. }
  17. ]
  18. }
  19. }
  20. }
  21. }

我们的JSON文档里有binaryAnnotations这个key, 而value是一个数组, 嵌套查询必须指定path,在我们这里就是binaryAnnotations,然后里面再使用query查询,query里面的语法和外层的一样 nested嵌套查询和其他match,range条件一样,是包含在must,should这些条件里面

5.复合条件嵌套查询

假设我们要查询binaryAnnotations  里面两个并行的条件

  1. {
  2. "query":{
  3. "bool":{
  4. "must":[
  5. {
  6. "range":{
  7. "timestamp":{
  8. "gte":1504581280866000,
  9. "lte":1504581280878000,
  10. "format":"date_time_no_millis"
  11. }
  12. }
  13. },
  14. {
  15. "match":{
  16. "traceId":"6edb691b4bc775b1"
  17. }
  18. },
  19. {
  20. "nested":{
  21. "path":"binaryAnnotations",
  22. "query":{
  23. "bool":{
  24. "must":[
  25. {
  26. "match":{
  27. "binaryAnnotations.key":"service"
  28. }
  29. },
  30. {
  31. "match":{
  32. "binaryAnnotations.value":"WebRequest"
  33. }
  34. }
  35. ]
  36. }
  37. }
  38. }
  39. },
  40. {
  41. "nested":{
  42. "path":"binaryAnnotations",
  43. "query":{
  44. "bool":{
  45. "must":[
  46. {
  47. "match":{
  48. "binaryAnnotations.key":"ip"
  49. }
  50. },
  51. {
  52. "match":{
  53. "binaryAnnotations.value":"127.0.0.1"
  54. }
  55. }
  56. ]
  57. }
  58. }
  59. }
  60. }
  61. ],
  62. "must_not":{
  63. "exists":{
  64. "field":"parentId"
  65. }
  66. }
  67. }
  68. },
  69. "size":400,
  70. "from":0,
  71. "sort":[
  72. {
  73. "timestamp":{
  74. "order":"desc",
  75. "unmapped_type":"boolean"
  76. }
  77. }
  78. ]
  79. }

6.去重查询

  1. {
  2. "query":{
  3. "bool":{
  4. "must":[
  5. {
  6. "match":{
  7. "name":"query"
  8. }
  9. }
  10. ]
  11. }
  12. },
  13. "aggs":{
  14. "traceId":{
  15. "terms":{
  16. "field":"traceId",
  17. "size":10
  18. }
  19. }
  20. },
  21. "size":10,
  22. "from":0,
  23. "sort":[
  24. {
  25. "timestamp":{
  26. "order":"desc",
  27. "unmapped_type":"boolean"
  28. }
  29. }
  30. ]
  31. }

去重要使用aggs 语句,和query查询平级,这里的意思是获取name=query 的记录并且用traceId去重

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/凡人多烦事01/article/detail/499018
推荐阅读
相关标签
  

闽ICP备14008679号