当前位置:   article > 正文

如何使用ES做简单的时间条件过滤+模糊查询+精确匹配+关键字排除,查询 elasticsearch查询结果包含或排除某些字段、_source查询出需要的属性名称_es模糊匹配

es模糊匹配

目录

一、时间条件过滤+模糊查询+精确匹配+关键字排除

1. 查询出包含 log_geo 的数据 “wildcard”: { “message”: “log_geo” }

2. 查询某个时间段的数据

3. 条件查询与条件排除数据

4. from 表示起始的记录的ID

5. size 表示显示的记录数

6.sort排序 desc降序、asc升序 

7.should查询在mysql中就好比是or或

8. aggs:执行聚合

9.bool query布尔查询

二、_source查询结果包含或排除某些字段


一、时间条件过滤+模糊查询+精确匹配+关键字排除

1. 查询出包含 log_geo 的数据 “wildcard”: { “message”: “log_geo” }

log_geo 前面有*表示搜索以log_geo结尾的数据

log_geo后面有* 表示搜以log_geo开始的数据

log_geo前后都有*就是通用匹配包含log_geo的记录

  1. “wildcard”:
  2. {
  3. “message”: “log_geo”
  4. }

2. 查询某个时间段的数据

  1. “range”: {
  2. “@timestamp”: {
  3. “gt”: “2018-02-08T07:00:00.056000000+00:00”,
  4. ”lt”: “2018-02-08T08:00:00.056000000+00:00
  5. }
  6. }

#注意 时区减去8小时

3. 条件查询与条件排除数据

3.1 match 包含provider 的数据

  1. {
  2. “match”:
  3. {
  4. “message”: “type:provider”
  5. }
  6. }

3.2 must_not 类似于 must 做排除使用

  1. “must_not”: {
  2. “match”: {
  3. “message”: “dateTime:2018-02-08 15:59
  4. }
  5. }

4. from 表示起始的记录的ID

5. size 表示显示的记录数

6.sort排序 desc降序、asc升序 

7.should查询在mysql中就好比是or或

但在es中使用should查询必须要和must一起使用才可以,相当于must[should A,should B],中文解释就好比是满足A或者是B条件的数据

8. aggs:执行聚合

9.bool query布尔查询

布尔查询是一个复合查询,由各个bool类型的子查询组成。

must :返回文档必须满足该条件,且提供分数
filter :返回文档必须满足该条件,但是不需要提供分数
should :返回文档可以满足0个或多个条件,且提供分数
must_not : 返回文档必须不满足条件,但是不需要提供分数


        查询语句 1,2,3可以组合,也可以单独使用,查询语句如果得不到想要的结果应该是ES的分词导致的,组合起来的语句如下:

  1. {
  2. "query": {
  3. "bool": {
  4. "must": [
  5. { "range": { "@timestamp": { "gt": "2018-02-08T07:00:00.056000000+00:00","lt": "2018-02-08T08:00:00.056000000+00:00" } } }
  6. ,
  7. { "wildcard": { "message": "*cp_geo*" } }
  8. ,
  9. { "match": { "message": "*type:platform*" } }
  10. ],
  11. "must_not": { "match": { "message": "*deviceTypeCode:DTout00000000*" } },
  12. "should": []
  13. }
  14. },
  15. "from": 0,
  16. "size": 50,
  17. "sort": { "@timestamp": "desc" },
  18. "aggs": {}
  19. }

参考原文地址:https://blog.51cto.com/u_8649447/4869008

二、_source查询结果包含或排除某些字段

        通过_source 字段中的 include 和 exclude 来指定返回结果包含哪些字段,排除哪些字段
举例:根据关系单号,查询 es,设置_source 的 include 和 exclude

实例查询语句:

  1. {
  2. "_source":{
  3. "include":[
  4. "policyNo",
  5. "policyRelationNo",
  6. "policyStatus"
  7. ],
  8. "exclude":[
  9. "salesType"
  10. ]
  11. },
  12. "query": {
  13. "bool": {
  14. "must": [
  15. {
  16. "term": {
  17. "policyRelationNo": "KR01435021"
  18. }
  19. }
  20. ],
  21. "should": [],
  22. "must_not": []
  23. }
  24. },
  25. "from": 0,
  26. "size": 10
  27. }

查询效果:

  1. {
  2. "_shards": {
  3. "total": 5,
  4. "successful": 5,
  5. "failed": 0
  6. },
  7. "hits": {
  8. "total": 19,
  9. "max_score": 11.391884,
  10. "hits": [
  11. {
  12. "_index": "search4policy-msad-dev3_20200520000000",
  13. "_type": "policy-msad-dev3",
  14. "_id": "4407038",
  15. "_score": 11.391884,
  16. "_source": {
  17. "policyRelationNo": "KR01435021",
  18. "policyNo": "B609120319",
  19. "policyStatus": 11
  20. }
  21. },
  22. {
  23. "_index": "search4policy-msad-dev3_20200520000000",
  24. "_type": "policy-msad-dev3",
  25. "_id": "4407046",
  26. "_score": 10.713255,
  27. "_source": {
  28. "policyRelationNo": "KR01435021",
  29. "policyNo": "B609120323",
  30. "policyStatus": 11
  31. }
  32. },
  33. {
  34. "_index": "search4policy-msad-dev3_20200520000000",
  35. "_type": "policy-msad-dev3",
  36. "_id": "4407044",
  37. "_score": 10.713255,
  38. "_source": {
  39. "policyRelationNo": "KR01435021",
  40. "policyNo": "B609120322",
  41. "policyStatus": 11
  42. }
  43. },
  44. {
  45. "_index": "search4policy-msad-dev3_20200520000000",
  46. "_type": "policy-msad-dev3",
  47. "_id": "4407066",
  48. "_score": 10.713255,
  49. "_source": {
  50. "policyRelationNo": "KR01435021",
  51. "policyNo": "B609120333",
  52. "policyStatus": 11
  53. }
  54. },
  55. {
  56. "_index": "search4policy-msad-dev3_20200520000000",
  57. "_type": "policy-msad-dev3",
  58. "_id": "4407058",
  59. "_score": 10.713255,
  60. "_source": {
  61. "policyRelationNo": "KR01435021",
  62. "policyNo": "B609120329",
  63. "policyStatus": 11
  64. }
  65. },
  66. {
  67. "_index": "search4policy-msad-dev3_20200520000000",
  68. "_type": "policy-msad-dev3",
  69. "_id": "4407070",
  70. "_score": 10.713255,
  71. "_source": {
  72. "policyRelationNo": "KR01435021",
  73. "policyNo": "B609120335",
  74. "policyStatus": 11
  75. }
  76. },
  77. {
  78. "_index": "search4policy-msad-dev3_20200520000000",
  79. "_type": "policy-msad-dev3",
  80. "_id": "4407056",
  81. "_score": 10.294733,
  82. "_source": {
  83. "policyRelationNo": "KR01435021",
  84. "policyNo": "B609120328",
  85. "policyStatus": 11
  86. }
  87. },
  88. {
  89. "_index": "search4policy-msad-dev3_20200520000000",
  90. "_type": "policy-msad-dev3",
  91. "_id": "4407052",
  92. "_score": 10.294733,
  93. "_source": {
  94. "policyRelationNo": "KR01435021",
  95. "policyNo": "B609120326",
  96. "policyStatus": 11
  97. }
  98. },
  99. {
  100. "_index": "search4policy-msad-dev3_20200520000000",
  101. "_type": "policy-msad-dev3",
  102. "_id": "4407062",
  103. "_score": 10.294733,
  104. "_source": {
  105. "policyRelationNo": "KR01435021",
  106. "policyNo": "B609120331",
  107. "policyStatus": 11
  108. }
  109. },
  110. {
  111. "_index": "search4policy-msad-dev3_20200520000000",
  112. "_type": "policy-msad-dev3",
  113. "_id": "4407064",
  114. "_score": 10.294733,
  115. "_source": {
  116. "policyRelationNo": "KR01435021",
  117. "policyNo": "B609120332",
  118. "policyStatus": 11
  119. }
  120. }
  121. ]
  122. },
  123. "took": 5,
  124. "timed_out": false
  125. }

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

闽ICP备14008679号