当前位置:   article > 正文

elasticsearch常用操作_elasticsearch 冻结索引

elasticsearch 冻结索引

es接口规范

大多数提到指数参数的 APl 支持多个指数的执行,使用

简单的测试1,测试2,测试3符号(或所有指数的_all)。它还支持通配符,因为

示例:测试* 或 测试或测试或 测试,以及"添加"(+)和"删除"(-)的能力

示例:+测试,测试3。

所有多指数 APl 支持以下网址查询字符串参数:

ignore_unavailable

控制是否忽略任何指定的指数不可用,这包括

不存在或关闭指数。可以指定真或假。

allow_no_indices

控制是否忽略任何指定的指数不可用,这包括

不存在或关闭指数。可以指定真或假。

expand_wildcards

控制什么样的具体指数通配符指数表达扩展到。如果打开是

指定然后通配符表示扩展到仅打开的指数,如果关闭是

指定然后通配符表示仅扩展封闭式指数。也两个值

(开放、封闭式)可指定扩展到所有指数。

如果没有指定,那么通配符扩展将被禁用,如果所有都指定,

通配符表示将扩展到所有指数(这相当于指定

打开,关闭)。

Common options

pretty results

当附加?pretty=true时,如果对提出的任何请求都适用,则 JSON 返回的请求将非常漂亮

格式化(仅使用它进行调试!另一种选择是设置?format=yaml,这将引起

结果将返回在 (有时) 更可读的 yaml 格式。

可读性输出

统计数据以适合人类的格式返回(例如"exists_time":"1h"或"size": "1kb") 和计算机 (如 "exists_time_in_millis": 3600000 或 "'size_in_bytes":

1024). 可以通过在查询中添加?human=false来关闭可读值

字符串。当统计数据结果被监控工具消耗时, 这是有道理的

比消费的要多人旗的默认是错误的。

日期数据格式化

索引

创建索引

  1. # 创建索引(设置副本和分片)
  2. PUT twitter
  3. {
  4. "settings" : {
  5. "index" : {
  6. "number_of_shards" : 3,
  7. "number_of_replicas" : 2
  8. }
  9.    }
  10. }
  11. // Default for number_of_shards is 5
  12. // Default for number_of_replicas is 1
  13. # 创建索引 (带mappings)
  14. PUT test
  15. {
  16. "settings" : {
  17. "number_of_shards" : 1
  18. },
  19. "mappings" : {
  20. "type1" : {
  21. "properties" : {
  22. "field1" : { "type" : "text" }
  23. }
  24. }
  25. }
  26. }
  27. # 创建索引带(带别名)
  28. PUT test
  29. {
  30. "aliases" : {
  31. "alias_1" : {},
  32. "alias_2" : {
  33. "filter" : {
  34. "term" : {"user" : "kimchy" }
  35. },
  36. "routing" : "kimchy"
  37. }
  38. }
  39. }

删除索引

DELETE /twitter
The delete index API can also be applied to more than one index, by either using a comma separated list, or on all indices (be careful!) by using _all or * as index.

获取索引

GET twitter//test_bak/_mappings,_settings,_aliases
The available features are _settings, _mappings and _aliases.

判断索引是否存在

HEAD twitter
The HTTP status code indicates if the index exists or not. A 404 means it does not exist, and 200 means it does.

打开关闭索引

  1. POST /my_index/_close
  2. POST /my_index/_open

节点与分片的关系

N >= R + 1

* 其中N是群集中的节点数,R是群集中所有索引的最大分片复制因子

打开/关闭索引

  1. POST http://localhost:9200/<index_name>/_close
  2. POST http://localhost:9200/<index_name>/_open
  3. POST http://localhost:9200/index*/_search?expand_wildcards=closed //指定关闭的索引
  4. POST http://localhost:9200/index*/_search?expand_wildcards=open //指定开启的索引
  5. POST http://localhost:9200/index*/_search?expand_wildcards=(open,closed) //指定关闭的索引

冻结/解冻索引

已经冻结的索引再次写入数据必须先解冻索引

  1. 冻结
  2. POST /index/_freeze
  3. 解冻
  4. POST /sampledata/_unfreeze

冻结索引查询

  1. POST /index/_search?ignore_throttled=false&pre_filter_shard_size=1
  2. {
  3. "query": {
  4. "match": {
  5. "name": "jane"
  6. }
  7. }
  8. }

修改索引配置

  1. PUT /twitter/_settings
  2. {
  3. "index": {
  4. "number_of_replicas": 2
  5. }
  6. }

指定索引所在分片

  1. warm hot
  2. PUT index_name/_settings
  3. {
  4. "index.routing.allocation.require.box_type": "warm"
  5. }

收缩索引

收缩指数 Apl 允许
您将现有索引缩小为主索数较少的新索引
碎片。目标索引中要求的主要碎片数量必须是
源索引中的碎片数量。例如,具有 8 个主要碎片的索引可以
缩小到 4、2 或 1 个原始碎片或具有 15 个主要碎片的索引可以缩小为 5,
3 或 1。如果指数中的碎片数量是一个质数,则只能缩小为单个
主要碎片。在收缩之前,索引中每个碎片的(原始或复制品)副本必须是
存在于同一节点上。
前置条件

为了缩小索引,必须将索引标记为仅读数和(主要或副本)

索引中每个碎片的副本必须移到同一节点,并具有健康绿色。

限制条件

指数只有满足以下要求才能缩水:

目标指数不得存在

该指数必须比目标指数具有更多的主要碎片。

目标指数中的主要碎片数量必须是

源索引中的主要碎片数量。源索引必须具有

比目标指数更多的主要碎片。

该指数的总包含文件总数不得超过 2,147,483,519 份

所有碎片, 将缩小到目标指数上的单一碎片作为

这是可以放入单个碎片的最大文档数量。

处理收缩过程的节点必须有足够的自由磁盘空间

容纳现有索引的第二个副本。

  1. PUT /my_source_index/_settings
  2. {
  3. "settings": {
  4. "index.routing.allocation.require._name": "shrink_node_name",
  5. "index.blocks.write": true
  6. }
  7. }

收缩索引类似于创建索引可以设置别名

  1. POST my_source_index/_shrink/my_target_index
  2. {
  3. "settings": {
  4. "index.number_of_replicas": 1,
  5. "index.number_of_shards": 1,
  6. "index.codec": "best_compression"
  7. },
  8. "aliases": {
  9. "my_search_indices": {}
  10. }
  11. }

创建mapping

  1. PUT twitter
  2. {
  3. "mappings": {
  4. "tweet": {
  5. "properties": {
  6. "message": {
  7. "type": "text"
  8. }
  9. }
  10. }
  11. }
  12. }
  13. PUT twitter/_mapping/user
  14. {
  15. "properties": {
  16. "name": {
  17. "type": "text"
  18. }
  19. }
  20. }
  21. PUT twitter/_mapping/tweet
  22. {
  23. "properties": {
  24. "user_name": {
  25. "type": "text"
  26. }
  27. }
  28. }

mapping 添加字段

  1. PUT INDEX/_mapping/TYPE/
  2. {
  3. "properties": {
  4. "SY_PROCESS_END_TIME_BABJ_PBZ_STDB": {
  5. "type": "keyword"
  6. }
  7. }
  8. }

获取mapping

  1. GET /twitter/_mapping/tweet
  2. # 获取多个index的mapping
  3. GET /_mapping/tweet
  4. GET /_all/_mapping/tweet
  5. # 获取所有mapping
  6. GET /_all/_mapping
  7. GET /_mapping

别名

创建别名
  1. POST /_aliases
  2. {
  3. "actions" : [
  4. { "add" : { "index" : "test1", "alias" : "alias1" } }
  5. ]
  6. }
移除别名
  1. POST /_aliases
  2. {
  3. "actions": [
  4. {
  5. "remove": {
  6. "index": "test1",
  7. "alias": "alias1"
  8. }
  9. }
  10. ]
  11. }
重命名(原子操作)
  1. POST /_aliases
  2. {
  3. "actions": [
  4. {
  5. "remove": {
  6. "index": "test1",
  7. "alias": "alias1"
  8. }
  9. },
  10. {
  11. "add": {
  12. "index": "test2",
  13. "alias": "alias1"
  14. }
  15. }
  16. ]
  17. }
批量创建别名
  1. POST /_aliases
  2. {
  3.     "actions" : [
  4. { "add" : { "index" : "test1", "alias" : "alias1" } },
  5. { "add" : { "index" : "test2", "alias" : "alias1" } }
  6. ]
  7. }
  8. POST /_aliases
  9. {
  10. "actions" : [
  11. { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } }
  12. ]
  13. }
  14. 批量移除
  15. POST /_aliases
  16. {
  17. "actions" : [
  18. { "remove" : { "indices" : ["dw_n15_cts_ori_202211*", "dw_n15_cts_ori_202212*"], "alias" : "dw_n15_cts_ori" } }
  19. ]
  20. }
  21. POST /_aliases
  22. {
  23. "actions" : [
  24. { "add" : { "index" : "test*", "alias" : "all_test_indices" } }
  25. ]
  26. }
  27. # 根据条件匹配添加别名
  28. POST /_aliases
  29. {
  30. "actions" : [
  31. {
  32. "add" : {
  33. "index" : "test1",
  34. "alias" : "alias2",
  35. "filter" : { "term" : { "user" : "kimchy" } }
  36. }
  37. }
  38. ]
  39. }
创建单一别名
  1. PUT /{index}/_alias/{name}
  2. index:
  3. The index the alias refers to. Can be any of * | _all | glob pattern | name1, name2, …
  4. DELETE /{index}/_alias/{name}
获取使用别名的索引
GET /logs_20162801/_alias/*
别名是否存在
  1. HEAD /_alias/2016
  2. HEAD /_alias/20*
  3. HEAD /logs_20162801/_alias/*

文档

根据查询结果删除文档

  1. POST twitter/_delete_by_query
  2. {
  3. "query": {
  4. "match": {
  5. "message": "some message"
  6. }
  7. }
  8. }
  9. #遇到版本冲突继续执行
  10. POST twitter/tweet/_delete_by_query?conflicts=proceed
  11. {
  12. "query": {
  13. "match_all": {}
  14. }
  15. }
  16. # 跨索引文档删除
  17. POST twitter,blog/tweet,post/_delete_by_query
  18. {
  19. "query": {
  20. "match_all": {}
  21. }
  22. }
  23. # 默认情况下,_delete_by_query使用 1000 个滚动批次。您可以更改批次大小scroll_size
  24. POST twitter/_delete_by_query?scroll_size=5000
  25. {
  26. "query": {
  27. "term": {
  28. "user": "kimchy"
  29. }
  30. }
  31. }
  32. # 返回信息
  33. {
  34.     "took" : 639,
  35.     "deleted": 0,
  36.     "batches": 1,
  37.     "version_conflicts": 2,
  38.     "retries": 0,
  39.     "throttled_millis": 0,
  40.     "failures" : [ ]
  41. }
  42. took:整个操作从开始到结束的毫秒数。
  43. deleted:成功删除的文档数量。
  44. batches:通过查询删除后拉回的滚动响应数。
  45. version_conflicts:被查询删除的版本冲突次数。
  46. retries:按查询删除的重述次数,以响应完整队列
  47. throttled_millis:请求睡眠的毫秒数符合requests_per_second。
  48. failures:所有索引故障的阵列。如果这是非空的,那么请求中止,因为这些
  49. 失败。查看冲突,了解如何
  50. 防止版本冲突中止操作。

获取状态任何与任务 APl 一起逐项删除的运行请求:

GET _tasks?detailed=true&actions=*/delete/byquery

根据查询结果修改文档(参考 _delete_by_query)

POST twitter/_update_by_query?conflicts=proceed
  1. POST twitter/_update_by_query
  2. {
  3.     "script": {
  4.         "inline": "ctx._source.likes++",
  5.         "lang": "painless"
  6.     },
  7.     "query": {
  8.         "term": {
  9.             "user": "kimchy"
  10.         }
  11.     }
  12. }
  13. indexName/RT.METRIC.LIST/_update_by_query
  14. {
  15.     "script": {
  16.     "lang": "painless",
  17.     "inline": "if (ctx._source.IS_IN== 1 ) {ctx._source.IS_IN= 0}"
  18.     },
  19.     "query": {}
  20. }

_reindex

  1. POST _reindex
  2. {
  3.     "source": {
  4.     "index": "twitter"
  5.     },
  6.     "dest": {
  7.         "index": "new_twitter"
  8.     }
  9. }
  10. #版本冲入继续执行
  11. POST _reindex
  12. {
  13.     "conflicts": "proceed",
  14.     "source": {
  15.        "index": "twitter"
  16.     },
  17.     "dest": {
  18.         "index": "new_twitter",
  19.         "op_type": "create"
  20.     }
  21. }

关闭节点

  • 关闭节点的API允许关闭集群中的一个或多个(或者全部)节点。下面是一个关闭 _local 节点的例子:

curl -XPOST 'http://localhost:9200/_cluster/nodes/_local/_shutdown
  • 通过各自的节点ID来关闭指定的节点

curl -XPOST 'http://localhost:9200/_cluster/nodes/nodeId1,nodeId2/_shutdown

主节点关闭:

curl -XPOST 'http://localhost:9200/_cluster/nodes/_master/_shutdown

关闭所有的节点:

  1. curl -XPOST 'http://localhost:9200/_shutdown'
  2. curl -XPOST 'http://localhost:9200/_cluster/nodes/_shutdown'
  3. curl -XPOST 'http://localhost:9200/_cluster/nodes/_all/_shutdown'
  4. 默认情况下,关闭命令会延迟1秒(1s)之后执行。可以通过设置 delay 参数 来指定延迟的时间
  5. curl -XPOST 'http://localhost:9200/_cluster/nodes/_local/_shutdown?delay=10s'

查看磁盘利用率

  1. curl -s '/_cat/allocation?v'
  2. shards disk.indices disk.used disk.avail disk.total disk.percent host ip node
  3. 6 10.2kb 41.6gb 4.3gb 45.9gb 90 127.0.0.1 127.0.0.1 ruan-node-1
  4. 6 10.2kb 41.6gb 4.3gb 45.9gb 90 127.0.0.1 127.0.0.1 ruan-node-2
  5. 返回的信息包括:
  6. 分片数(shards),集群中各节点的分片数相同,都是6,总数为12,所以集群的总分片数为12。
  7. 索引所占空间(disk.indices),该节点中所有索引在该磁盘所点的空间。
  8. 磁盘使用容量(disk.used),已经使用空间41.6gb
  9. 磁盘可用容量(disk.avail),可用空间4.3gb
  10. 磁盘总容量(disk.total),总共容量45.9gb
  11. 磁盘便用率(disk.percent),磁盘使用率90%。

修改磁盘使用阈值

  1. curl -XPUT '/_cluster/settings'-d
  2. '{
  3.     "transient": {
  4.         "cluster.routing.allocation.disk.watermark.low":"90%"
  5.     }
  6. }'

动态设置索引副本数

  1. PUT /<index_name>/_settings
  2. {
  3.     "number_of_replicas": 1
  4. }

查看问题索引

GET /_cluster/health?level=indices

查看分片shard原因

GET /_cluster/allocation/explain/

重新路由

POST /_cluster/reroute?retry_failed=true

清空缓存

  1. <!-- 清空所有缓存 -->
  2. POST /_cache/clear?pretty
  3. <!-- 清除单一索引缓存 -->
  4. POST /index/_cache/clear?pretty
  5. <!-- 清除多索引缓存 -->
  6. POST /index1,index2,index3/_cache/clear?pretty

设置延迟分配

设置原因

当集群节点脱离集群的时候会有以下动作产生:

  1. master将离开节点包含的所有主分片的副本分片提升为主分片

  1. 假如有足够的节点将把所有丢失的副本分片重新分配到集群中的其他节点中

  1. Rebalancing操作

如果节点离开后马上又回来(如网络不好,重启等),在这个短时间内会进行两次Rebalancing操作。操作会带来很大开销

两种设置方式

- 在配置文件中设置

index.unassigned.node_left.delayed_timeout

- 通过setting api进行设置

  1. PUT /_all/_settings
  2. {
  3.     "settings": {
  4.        "index.unassigned.node_left.delayed_timeout": "5m"
  5.     }
  6. }

当设置了延迟分配后,当节点离开集群时的动作如下:

  1. master将离开节点包含的所有主分片的副本分片提升为主分片

  1. Master记录日志,(delaying allocation for [3] unassigned shards, next check in [5m])

  1. 集群状态还是黄色,因为有分片还没有被分配(但是不影响查询写入)

  1. 节点在延迟时间内返回,直接打开副本分片

  1. 节点超时返回,集群将unassigned的副本分片进行分配,然后进行一次Rebalancing操作。

如果想立即恢复,可以把参数值设置为0。

强行分配分片

  1. curl -XPOST 'node3:9205/_cluster/reroute' -d {
  2.     "commands": [
  3.         {
  4.             "allocate_replica": {
  5.             "index": "indexName",
  6.             "shard": 分片id,
  7.             "node": "节点名"
  8.             }
  9.         }
  10.     ]
  11. }

如果主分片也未分配,则需要先分配主分片

  1. curl -XPOST 'node3:9205/_cluster/reroute' -d
  2. {
  3.     "commands": [
  4.         {
  5.             "allocate_stale_primary": {
  6.             "index": "mail_store",
  7.             "shard": 1,
  8.             "node": "slave2",
  9.             "accept_data_loss": true
  10.             }
  11.         }
  12.     ]
  13. }

使用此命令可能会导致所提供的分片ID发生数据丢失。如果稍后具有良好数据副本的节点重新加入群集,则该数据将被使用此命令强制分配的旧副本数据覆盖。为确保这些影响得到充分理解,该命令需要accept_data_loss明确设置专用字段才能true使其工作。

停止自动分配分片

  1. curl -XPUT '/indexName/_settings' -d '{
  2.     "cluster.routing.allocation.disable_allocation" : true
  3. }'

手动移动分片

  1. curl -XPOST "/_cluster/reroute' -d '{
  2.     "commands" : [{
  3.     "move" : {
  4.     "index" : "xlog",
  5.     "shard" : 0,
  6.     "from_node" : "es-0",
  7.     "to_node" : "es-3"
  8.     }
  9.     }]
  10. }'

取消分配

* 主分片和启动状态下的分片不能取消

  1. curl -XPOST "/_cluster/reroute" -d '{
  2.     "commands" : [ {
  3.         "cancel" : {
  4.         "index" : "ops",
  5.         "shard" : 0,
  6.         "node" : "es_node_one"
  7.         }
  8.     } ]
  9. }'
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/349198
推荐阅读
相关标签
  

闽ICP备14008679号