集群相关

    1. --查询集群健康状态
    2. GET _cluster/health
    3. --查询所有节点
    4. GET _cat/nodes
    5. --查询索引及分片的分布
    6. GET _cat/shards--查询指定索引分片的分布GET _cat/shards/order_stpprdinf_2019-12?v
    7. --查询所有插件
    8. GET _cat/plugins

    索引相关查询

    1. --查询所有索引及容量
    2. GET _cat/indices
    3. --查询索引映射结构
    4. GET my_index/_mapping
    5. --查询所有索引映射结构
    6. GET _all
    7. --查询所有的相同前缀索引
    8. GET my-*/_search
    9. --查询所有索引模板
    10. GET _template
    11. --查询具体索引模板
    12. GET _template/my_template

    索引相关操作
    1、创建索引模板

    1. # 创建模板PUT _template/test_hot_cold_template
    2. {
    3. "index_patterns": "test_*",
    4. "settings": {
    5. "number_of_shards" : 3,
    6. "index.number_of_replicas": 1
    7. },
    8. "mappings": {
    9. "order": {
    10. "dynamic": false,
    11. "properties": {
    12. "id": {"type": "long"},
    13. "name": {"type": "keyword"}
    14. }
    15. }
    16. },
    17. "aliases": {
    18. "test": {}
    19. }
    20. }
    21. # 根据模板创建索引并写入数据
    22. POST test_hot_cold-2019-12-01/order
    23. {
    24. "id":1,
    25. "name":"cwx"
    26. }# 删除模板DELETE _template/order_stpprdinf_template

    2、直接创建索引

    1. PUT my_index
    2. {
    3. "mappings": {
    4. "doc": {
    5. "properties": {
    6. "name": {
    7. "type": "text"
    8. },
    9. "blob": {
    10. "type": "binary"
    11. }
    12. }
    13. }
    14. }
    15. }
    1. -- 增加字段put my_index/_mapping/doc
    2. {
    3. "properties": {
    4. "cwxtest": {"type": "keyword"}
    5. }
    6. }-- 数据冷备份
    7. PUT _snapshot/my_backup # my_backup 备份的名称
    8. {
    9. "type": "order",
    10. "settings": {
    11. "location": "/mount/backups/my_backup"
    12. }
    13. }

    3、写入索引

    1. PUT my_index/doc/1
    2. {
    3. "name": "Some binary blob",
    4. "blob": "U29tZSBiaW5hcnkgYmxvYg=="
    5. }

    4、删除索引

    1. DELETE my-index

    5、修改索引setting

    1. PUT /test_hot_cold-2019-12-01/_settings
    2. {
    3. "settings": {
    4. "index.routing.allocation.require.hotwarm_type": "cold"
    5. }
    6. }

    DSL query查询

    1. --1.查询所有
    2. GET _search
    3. {
    4. "query": {
    5. "match_all": {}
    6. }
    7. }
    8. --2.查询单个索引 固定属性
    9. --精确匹配
    10. GET _search
    11. {
    12. "query": {
    13. "term": { "name" : "you" }
    14. }
    15. }
    16. --模糊匹配
    17. GET _search
    18. {
    19. "query": {
    20. "match": { "name" : "you" }
    21. }
    22. }
    23. --范围查找
    24. GET _search
    25. {
    26. "query": {
    27. "range": {
    28. "age":{ "gte" : 15 , "lte" : 25 }
    29. }
    30. }
    31. } GET indexName/_search {    "query": {      "wildcard":{"relateId":"*672499460503*"}    } }
    32. --3.功能性查询
    33. --过滤
    34. GET my_index/_search
    35. {
    36. "query": {
    37. "bool": {
    38. "filter": {
    39. "term":{"age":1095}
    40. }
    41. }
    42. }
    43. }
    44. --或 or
    45. GET my - test / _search {
    46.   "query": {
    47.     "bool": {
    48.       "should": [{
    49.         "term": {
    50.           "name": "you"
    51.         }
    52.         }, {
    53.         "match": {
    54.           "age": 20
    55.         }
    56.       }]
    57.     }
    58.   }
    59. }
    60. --与 AND
    61. GET my-test/_search
    62. {
    63. "query": {
    64. "bool": {
    65. "must" : [{
    66. "match" : {
    67. "name" : "you"
    68. }
    69. },{
    70. "range":{
    71. "age":{
    72. "from" : 10 , "to" : 20
    73. }
    74. }
    75. }]
    76. }
    77. }
    78. }
    79. --必须 =
    80. GET my_index/_search
    81. {
    82. "query": {
    83. "bool": {
    84. "must" : {
    85. "range" : {
    86. "age" : { "from" : 10, "to" : 20 }
    87. }
    88. }
    89. }
    90. }
    91. }
    92. --必须不 not
    93. GET my_index/_search
    94. {
    95. "query": {
    96. "bool": {
    97. "must_not" : {
    98. "term" : {
    99. "name" : "you"
    100. }
    101. }
    102. }
    103. }
    104. }
    105. --复合查找
    106. GET my_index/_search
    107. {
    108. "query": {
    109. "bool": {
    110. "should": [{
    111. "match": {
    112. "age": 40
    113. }
    114. },
    115. {
    116. "match": {
    117. "age": 20
    118. }
    119. }],
    120. "filter": {
    121. "match":{
    122. "name":"you"
    123. }
    124. }
    125. }
    126. }
    127. }
    128. --4.索引迁移
    129. --场景 A索引 复制到B索引
    130. POST _reindex
    131. {
    132. "source": {
    133. "index": "my_index"
    134. },
    135. "dest": {
    136. "index": "new_my_index"
    137. }
    138. }
    139. --5.基于查询的删除
    140. POST test-index/_delete_by_query
    141. {
    142. "query":{
    143. "term": {
    144. "cameraId":"00000000002"
    145. }
    146. }
    147. }
    148. --查询
    149. GET test-index/_search
    150. {
    151. "query":{
    152. "term": {
    153. "cameraId":"00000000002"
    154. }
    155. }
    156. }

    按时间范围查询:

    1. GET order_stpprdinf_2019-12/_search
    2. {
    3. "size":10,
    4. "query":{
    5. "range":{
    6. "order_time":{
    7. "gte":"2019-12-11T00:00:00+08:00",
    8. "lte":"2019-12-11T23:59:59+08:00"
    9. }
    10. }
    11. }
    12. }

    按条件删除:

    1. GET order_stpprdinf_2019-12/_delete_by_query?conflicts=proceed
    2. {
    3. "query":{
    4. "range":{
    5. "order_time":{
    6. "gte":"2019-12-11T00:00:00+08:00",
    7. "lte":"2019-12-11T23:59:59+08:00"
    8. }
    9. }
    10. }
    11. }

    ElasticSearch——常用命令 - 图1

    5、设置IK分词器
    put /iktest?pretty
    {
    “settings”:{
    “analysis”:{
    “analyzer”:{
    “ik”:{
    “tokenizer”:”ik_max_word”
    }
    }
    }
    },
    “mappings”:{
    “article”:{
    “dynamic”:true,
    “properties”:{
    “subject”:{
    “type”:”text”,
    “analyzer”:”ik_max_word”
    }
    }
    }
    }
    }

    6、搜索并高亮展示
    POST /iktest/article/_search?pretty
    {
    “query”: {“match”: {
    “subject”: “希拉里和韩国”
    }},
    “highlight”: {
    “pre_tags”: [““],
    “post_tags”: [“
    “],
    “fields”: {
    “subject”: {}
    }
    }
    }