1、简介

Elastic 官方网站
Elastic 中文社区
Elasticsearch: 权威指南

2、使用

2.1、创建索引

image.png

  1. {
  2. "aliases": {
  3. "customer_remind_alias_sit": {},
  4. "customer_remind_alias_sst": {},
  5. "customer_remind_alias_test": {}
  6. },
  7. "mappings": {
  8. "properties": {
  9. "remindRuleId": {
  10. "type": "keyword"
  11. },
  12. "remindDate": {
  13. "type": "date",
  14. "ignore_malformed": true,
  15. "format": "yyyy-MM-dd"
  16. },
  17. "carIdOwnOrg": {
  18. "type": "keyword"
  19. },
  20. "wechatFollower": {
  21. "type": "keyword"
  22. },
  23. "wechatSendState": {
  24. "type": "keyword"
  25. },
  26. "wechatSendDate": {
  27. "type": "date",
  28. "ignore_malformed": true,
  29. "format": "yyyy-MM-dd"
  30. },
  31. "phoneSendState": {
  32. "type": "keyword"
  33. },
  34. "phoneSendDate": {
  35. "type": "date",
  36. "ignore_malformed": true,
  37. "format": "yyyy-MM-dd"
  38. },
  39. "smsSendState": {
  40. "type": "keyword"
  41. },
  42. "smsSendDate": {
  43. "type": "date",
  44. "ignore_malformed": true,
  45. "format": "yyyy-MM-dd"
  46. },
  47. "couponSendState": {
  48. "type": "keyword"
  49. },
  50. "picSendState": {
  51. "type": "keyword"
  52. },
  53. "operatorId": {
  54. "type": "keyword"
  55. },
  56. "state": {
  57. "type": "keyword"
  58. },
  59. "isDeleted": {
  60. "type": "keyword"
  61. },
  62. "operatorProcessState": {
  63. "type": "keyword"
  64. },
  65. "consumeAgainState": {
  66. "type": "keyword"
  67. },
  68. "consumeAgainDate": {
  69. "type": "date",
  70. "ignore_malformed": true,
  71. "format": "yyyy-MM-dd"
  72. },
  73. "finishTime": {
  74. "type": "date",
  75. "ignore_malformed": true,
  76. "format": "yyyy-MM-dd"
  77. },
  78. "customerName": {
  79. "type": "text",
  80. "fields": {
  81. "raw": {
  82. "type": "keyword"
  83. }
  84. },
  85. "analyzer": "ngram_10_analyzer"
  86. },
  87. "customerCellPhone": {
  88. "type": "text",
  89. "analyzer": "ngram_11_analyzer"
  90. },
  91. "fullCarNo": {
  92. "type": "text",
  93. "fields": {
  94. "raw": {
  95. "type": "keyword"
  96. }
  97. },
  98. "analyzer": "ngram_8_analyzer"
  99. },
  100. "remindType": {
  101. "type": "keyword"
  102. },
  103. "idOwnOrg": {
  104. "type": "keyword"
  105. },
  106. "creationtime": {
  107. "type": "date",
  108. "ignore_malformed": true,
  109. "format": "yyyy-MM-dd"
  110. },
  111. "customerId": {
  112. "type": "keyword"
  113. },
  114. "carId": {
  115. "type": "keyword"
  116. },
  117. "groupId": {
  118. "type": "keyword"
  119. },
  120. "lastServiceDate": {
  121. "type": "date",
  122. "ignore_malformed": true,
  123. "format": "yyyy-MM-dd"
  124. },
  125. "lastServiceOrg": {
  126. "type": "keyword"
  127. },
  128. "operatorSendState": {
  129. "type": "keyword"
  130. },
  131. "pkId": {
  132. "type": "keyword"
  133. },
  134. "sourceType": {
  135. "type": "keyword"
  136. },
  137. "lastInsuranceCompanyId ": {
  138. "type": "keyword"
  139. },
  140. "lastInsuranceOrgId ": {
  141. "type": "keyword"
  142. },
  143. "car_exclusive_consultants_id": {
  144. "type": "keyword"
  145. }
  146. }
  147. },
  148. "settings": {
  149. "index": {
  150. "max_ngram_diff": "19",
  151. "analysis": {
  152. "analyzer": {
  153. "ngram_10_analyzer": {
  154. "tokenizer": "ngram_10_tokenizer"
  155. },
  156. "ngram_11_analyzer": {
  157. "tokenizer": "ngram_11_tokenizer"
  158. },
  159. "ngram_8_analyzer": {
  160. "tokenizer": "ngram_8_tokenizer"
  161. }
  162. },
  163. "tokenizer": {
  164. "ngram_10_tokenizer": {
  165. "type": "ngram",
  166. "min_gram": "1",
  167. "max_gram": "20"
  168. },
  169. "ngram_11_tokenizer": {
  170. "type": "ngram",
  171. "min_gram": "1",
  172. "max_gram": "11"
  173. },
  174. "ngram_8_tokenizer": {
  175. "type": "ngram",
  176. "min_gram": "1",
  177. "max_gram": "8"
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }

2.2、状态查询

2.2.1、集群的健康状态

  1. GET _cat/health

2.2.2、节点状态

  1. GET _cat/nodes

2.2.3、 查看所有索引

  1. GET _cat/indices

2.3、常用查询

2.3.1、查询数量

  • track_total_hits=true不然超过1万,数量只给10000
  • customer_remind为索引名称
  • 限制size=0,不反回结果,只返回数量
  • should 相当于mysql中的 or
  • filter 过滤 可以理解成mysql中的 and
  • term 精确匹配 相当于mysql中的 =
  • terms 相当于mysql中的 in

    1. POST customer_remind/_search?track_total_hits=true
    2. {
    3. "query": {
    4. "bool": {
    5. "should": [
    6. {
    7. "terms": {
    8. "carIdOwnOrg": [
    9. "15519724471253495810",
    10. "15519724471253495811"
    11. ]
    12. }
    13. },
    14. {
    15. "terms": {
    16. "idOwnOrg": [
    17. "15519724471253495810"
    18. ]
    19. }
    20. }
    21. ],
    22. "filter": [
    23. {
    24. "terms": {
    25. "state": [
    26. "0",
    27. "2"
    28. ]
    29. }
    30. },
    31. {
    32. "term": {
    33. "groupId": "15519724471253495812"
    34. }
    35. }
    36. ]
    37. }
    38. },
    39. "size": 0
    40. }

    2.3.2、查询列表

  • customer_remind为索引名称

  • must 必须匹配
  • multi_match 多个字段模糊查询
  • query对应查询的关键词
  • from、size对应分页参数
  • sort 排序参数

    1. POST customer_remind/_search
    2. {
    3. "query": {
    4. "bool": {
    5. "must": [
    6. {
    7. "multi_match": {
    8. "query": "16605115059",
    9. "analyzer": "ik_smart",
    10. "type": "best_fields",
    11. "operator": "and",
    12. "fields": ["customerName","customerCellPhone"]
    13. }
    14. }
    15. ]
    16. }
    17. },
    18. "from": 0,
    19. "size": 10,
    20. "sort": {
    21. "creationtime": {
    22. "order": "desc"
    23. }
    24. }
    25. }
    1. {
    2. "query": {
    3. "bool": {
    4. "must": [
    5. {
    6. "match": {
    7. "customerName": {
    8. "analyzer": "ik_smart",
    9. "query": "AAA",
    10. "operator": "and"
    11. }
    12. }
    13. },
    14. {
    15. "match": {
    16. "customerCellPhone": {
    17. "analyzer": "ik_smart",
    18. "query": "A",
    19. "operator": "and"
    20. }
    21. }
    22. },
    23. {
    24. "match": {
    25. "fullCarNo": {
    26. "analyzer": "ik_smart",
    27. "query": "A",
    28. "operator": "and"
    29. }
    30. }
    31. }
    32. ]
    33. }
    34. },
    35. "from": 0,
    36. "size": 10,
    37. "sort": {
    38. "creationtime": {
    39. "order": "desc"
    40. }
    41. }
    42. }

    2.3.3、详情查询

  • _doc/{_id} 根据id查询详情

    1. GET customer_remind/_doc/15748138447761588306

    2.3.4、filter与must,term与match的区别

    2.3.4.1、根据字段类型不同

  • 对于 keyword类型的字段而言, 用 term 和 match 都是可以查询的;

  • 但对于 text 类型的分词字段而言,只能用match才能够查询到结果;

    2.3.4.2、根据嵌套类型查询(filter 与 must 是属于同一个级别的查询方式,都可以作为 query->bool 的属性)

  • filter: 不计算评分,查询效率高、有缓存。(推荐)

    1. term 精确匹配<br /> match 模糊匹配、倒排索引
  • must: 要计算评分,查询效率低、无缓存

    term: 精确匹配、 要评分
    match:模糊匹配、 要评分

    2.4、删除

    2.4.1、删除索引下所有的数据

    1. POST /customer_remind/_delete_by_query
    2. {
    3. "query": {
    4. "match_all": {}
    5. }
    6. }

    2.4.2、根据id删除单条数据

    1. DELETE /customer_remind/_doc/15748138447761588306

    2.5、更新

    2.5.1、局部更新

    1. POST /customer_remind/_doc/15748138447761588306/_update
    2. {
    3. "doc": {
    4. "customerName": "名"
    5. }
    6. }

    2.5.2、全量更新

    1. POST /customer_remind/_doc/15748138447761588306
    2. {
    3. "customerName": "名ss"
    4. }

    3、备注

    3.1、浏览器插件

    Elasticvue
    ElasticSearch Head

    3.2、配置

    image.png