赞
踩
大多数提到指数参数的 APl 支持多个指数的执行,使用
简单的测试1,测试2,测试3符号(或所有指数的_all)。它还支持通配符,因为
示例:测试* 或 测试或测试或 测试,以及"添加"(+)和"删除"(-)的能力
示例:+测试,测试3。
所有多指数 APl 支持以下网址查询字符串参数:
ignore_unavailable
控制是否忽略任何指定的指数不可用,这包括
不存在或关闭指数。可以指定真或假。
allow_no_indices
控制是否忽略任何指定的指数不可用,这包括
不存在或关闭指数。可以指定真或假。
expand_wildcards
控制什么样的具体指数通配符指数表达扩展到。如果打开是
指定然后通配符表示扩展到仅打开的指数,如果关闭是
指定然后通配符表示仅扩展封闭式指数。也两个值
(开放、封闭式)可指定扩展到所有指数。
如果没有指定,那么通配符扩展将被禁用,如果所有都指定,
通配符表示将扩展到所有指数(这相当于指定
打开,关闭)。
当附加?pretty=true时,如果对提出的任何请求都适用,则 JSON 返回的请求将非常漂亮
格式化(仅使用它进行调试!另一种选择是设置?format=yaml,这将引起
结果将返回在 (有时) 更可读的 yaml 格式。
统计数据以适合人类的格式返回(例如"exists_time":"1h"或"size": "1kb") 和计算机 (如 "exists_time_in_millis": 3600000 或 "'size_in_bytes":
1024). 可以通过在查询中添加?human=false来关闭可读值
字符串。当统计数据结果被监控工具消耗时, 这是有道理的
比消费的要多人旗的默认是错误的。
# 创建索引(设置副本和分片) PUT twitter { "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } } } // Default for number_of_shards is 5 // Default for number_of_replicas is 1 # 创建索引 (带mappings) PUT test { "settings" : { "number_of_shards" : 1 }, "mappings" : { "type1" : { "properties" : { "field1" : { "type" : "text" } } } } } # 创建索引带(带别名) PUT test { "aliases" : { "alias_1" : {}, "alias_2" : { "filter" : { "term" : {"user" : "kimchy" } }, "routing" : "kimchy" } } }
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.
- POST /my_index/_close
- POST /my_index/_open
节点与分片的关系
N >= R + 1
* 其中N是群集中的节点数,R是群集中所有索引的最大分片复制因子
- POST http://localhost:9200/<index_name>/_close
- POST http://localhost:9200/<index_name>/_open
- POST http://localhost:9200/index*/_search?expand_wildcards=closed //指定关闭的索引
- POST http://localhost:9200/index*/_search?expand_wildcards=open //指定开启的索引
- POST http://localhost:9200/index*/_search?expand_wildcards=(open,closed) //指定关闭的索引
已经冻结的索引再次写入数据必须先解冻索引
- 冻结
- POST /index/_freeze
- 解冻
- POST /sampledata/_unfreeze
- POST /index/_search?ignore_throttled=false&pre_filter_shard_size=1
- {
- "query": {
- "match": {
- "name": "jane"
- }
- }
- }
- PUT /twitter/_settings
- {
- "index": {
- "number_of_replicas": 2
- }
- }
- warm hot
- PUT index_name/_settings
- {
- "index.routing.allocation.require.box_type": "warm"
- }
收缩指数 Apl 允许
您将现有索引缩小为主索数较少的新索引
碎片。目标索引中要求的主要碎片数量必须是
源索引中的碎片数量。例如,具有 8 个主要碎片的索引可以
缩小到 4、2 或 1 个原始碎片或具有 15 个主要碎片的索引可以缩小为 5,
3 或 1。如果指数中的碎片数量是一个质数,则只能缩小为单个
主要碎片。在收缩之前,索引中每个碎片的(原始或复制品)副本必须是
存在于同一节点上。
为了缩小索引,必须将索引标记为仅读数和(主要或副本)
索引中每个碎片的副本必须移到同一节点,并具有健康绿色。
指数只有满足以下要求才能缩水:
目标指数不得存在
该指数必须比目标指数具有更多的主要碎片。
目标指数中的主要碎片数量必须是
源索引中的主要碎片数量。源索引必须具有
比目标指数更多的主要碎片。
该指数的总包含文件总数不得超过 2,147,483,519 份
所有碎片, 将缩小到目标指数上的单一碎片作为
这是可以放入单个碎片的最大文档数量。
处理收缩过程的节点必须有足够的自由磁盘空间
容纳现有索引的第二个副本。
- PUT /my_source_index/_settings
- {
- "settings": {
- "index.routing.allocation.require._name": "shrink_node_name",
- "index.blocks.write": true
- }
- }
收缩索引类似于创建索引可以设置别名
- POST my_source_index/_shrink/my_target_index
- {
- "settings": {
- "index.number_of_replicas": 1,
- "index.number_of_shards": 1,
- "index.codec": "best_compression"
- },
- "aliases": {
- "my_search_indices": {}
- }
- }
PUT twitter { "mappings": { "tweet": { "properties": { "message": { "type": "text" } } } } } PUT twitter/_mapping/user { "properties": { "name": { "type": "text" } } } PUT twitter/_mapping/tweet { "properties": { "user_name": { "type": "text" } } }
- PUT INDEX/_mapping/TYPE/
- {
- "properties": {
- "SY_PROCESS_END_TIME_BABJ_PBZ_STDB": {
- "type": "keyword"
- }
- }
- }
- GET /twitter/_mapping/tweet
- # 获取多个index的mapping
- GET /_mapping/tweet
-
- GET /_all/_mapping/tweet
- # 获取所有mapping
- GET /_all/_mapping
-
- GET /_mapping
- POST /_aliases
- {
- "actions" : [
- { "add" : { "index" : "test1", "alias" : "alias1" } }
- ]
- }
- POST /_aliases
- {
- "actions": [
- {
- "remove": {
- "index": "test1",
- "alias": "alias1"
- }
- }
- ]
- }
POST /_aliases { "actions": [ { "remove": { "index": "test1", "alias": "alias1" } }, { "add": { "index": "test2", "alias": "alias1" } } ] }
POST /_aliases { "actions" : [ { "add" : { "index" : "test1", "alias" : "alias1" } }, { "add" : { "index" : "test2", "alias" : "alias1" } } ] } POST /_aliases { "actions" : [ { "add" : { "indices" : ["test1", "test2"], "alias" : "alias1" } } ] } 批量移除 POST /_aliases { "actions" : [ { "remove" : { "indices" : ["dw_n15_cts_ori_202211*", "dw_n15_cts_ori_202212*"], "alias" : "dw_n15_cts_ori" } } ] } POST /_aliases { "actions" : [ { "add" : { "index" : "test*", "alias" : "all_test_indices" } } ] } # 根据条件匹配添加别名 POST /_aliases { "actions" : [ { "add" : { "index" : "test1", "alias" : "alias2", "filter" : { "term" : { "user" : "kimchy" } } } } ] }
- PUT /{index}/_alias/{name}
- index:
- The index the alias refers to. Can be any of * | _all | glob pattern | name1, name2, …
-
- DELETE /{index}/_alias/{name}
GET /logs_20162801/_alias/*
- HEAD /_alias/2016
- HEAD /_alias/20*
- HEAD /logs_20162801/_alias/*
POST twitter/_delete_by_query { "query": { "match": { "message": "some message" } } } #遇到版本冲突继续执行 POST twitter/tweet/_delete_by_query?conflicts=proceed { "query": { "match_all": {} } } # 跨索引文档删除 POST twitter,blog/tweet,post/_delete_by_query { "query": { "match_all": {} } } # 默认情况下,_delete_by_query使用 1000 个滚动批次。您可以更改批次大小scroll_size POST twitter/_delete_by_query?scroll_size=5000 { "query": { "term": { "user": "kimchy" } } } # 返回信息 { "took" : 639, "deleted": 0, "batches": 1, "version_conflicts": 2, "retries": 0, "throttled_millis": 0, "failures" : [ ] } took:整个操作从开始到结束的毫秒数。 deleted:成功删除的文档数量。 batches:通过查询删除后拉回的滚动响应数。 version_conflicts:被查询删除的版本冲突次数。 retries:按查询删除的重述次数,以响应完整队列 throttled_millis:请求睡眠的毫秒数符合requests_per_second。 failures:所有索引故障的阵列。如果这是非空的,那么请求中止,因为这些 失败。查看冲突,了解如何 防止版本冲突中止操作。
获取状态任何与任务 APl 一起逐项删除的运行请求:
GET _tasks?detailed=true&actions=*/delete/byquery
POST twitter/_update_by_query?conflicts=proceed
POST twitter/_update_by_query { "script": { "inline": "ctx._source.likes++", "lang": "painless" }, "query": { "term": { "user": "kimchy" } } } indexName/RT.METRIC.LIST/_update_by_query { "script": { "lang": "painless", "inline": "if (ctx._source.IS_IN== 1 ) {ctx._source.IS_IN= 0}" }, "query": {} }
POST _reindex { "source": { "index": "twitter" }, "dest": { "index": "new_twitter" } } #版本冲入继续执行 POST _reindex { "conflicts": "proceed", "source": { "index": "twitter" }, "dest": { "index": "new_twitter", "op_type": "create" } }
关闭节点的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
关闭所有的节点:
- curl -XPOST 'http://localhost:9200/_shutdown'
- curl -XPOST 'http://localhost:9200/_cluster/nodes/_shutdown'
- curl -XPOST 'http://localhost:9200/_cluster/nodes/_all/_shutdown'
- 默认情况下,关闭命令会延迟1秒(1s)之后执行。可以通过设置 delay 参数 来指定延迟的时间
- curl -XPOST 'http://localhost:9200/_cluster/nodes/_local/_shutdown?delay=10s'
- curl -s '/_cat/allocation?v'
- shards disk.indices disk.used disk.avail disk.total disk.percent host ip node
- 6 10.2kb 41.6gb 4.3gb 45.9gb 90 127.0.0.1 127.0.0.1 ruan-node-1
- 6 10.2kb 41.6gb 4.3gb 45.9gb 90 127.0.0.1 127.0.0.1 ruan-node-2
- 返回的信息包括:
- 分片数(shards),集群中各节点的分片数相同,都是6,总数为12,所以集群的总分片数为12。
- 索引所占空间(disk.indices),该节点中所有索引在该磁盘所点的空间。
- 磁盘使用容量(disk.used),已经使用空间41.6gb
- 磁盘可用容量(disk.avail),可用空间4.3gb
- 磁盘总容量(disk.total),总共容量45.9gb
- 磁盘便用率(disk.percent),磁盘使用率90%。
- curl -XPUT '/_cluster/settings'-d
- '{
- "transient": {
- "cluster.routing.allocation.disk.watermark.low":"90%"
- }
- }'
- PUT /<index_name>/_settings
- {
- "number_of_replicas": 1
- }
GET /_cluster/health?level=indices
GET /_cluster/allocation/explain/
POST /_cluster/reroute?retry_failed=true
- <!-- 清空所有缓存 -->
- POST /_cache/clear?pretty
- <!-- 清除单一索引缓存 -->
- POST /index/_cache/clear?pretty
- <!-- 清除多索引缓存 -->
- POST /index1,index2,index3/_cache/clear?pretty
设置原因
当集群节点脱离集群的时候会有以下动作产生:
master将离开节点包含的所有主分片的副本分片提升为主分片
假如有足够的节点将把所有丢失的副本分片重新分配到集群中的其他节点中
Rebalancing操作
如果节点离开后马上又回来(如网络不好,重启等),在这个短时间内会进行两次Rebalancing操作。操作会带来很大开销
两种设置方式
- 在配置文件中设置
index.unassigned.node_left.delayed_timeout
- 通过setting api进行设置
- PUT /_all/_settings
- {
- "settings": {
- "index.unassigned.node_left.delayed_timeout": "5m"
- }
- }
当设置了延迟分配后,当节点离开集群时的动作如下:
master将离开节点包含的所有主分片的副本分片提升为主分片
Master记录日志,(delaying allocation for [3] unassigned shards, next check in [5m])
集群状态还是黄色,因为有分片还没有被分配(但是不影响查询写入)
节点在延迟时间内返回,直接打开副本分片
节点超时返回,集群将unassigned的副本分片进行分配,然后进行一次Rebalancing操作。
如果想立即恢复,可以把参数值设置为0。
- curl -XPOST 'node3:9205/_cluster/reroute' -d {
- "commands": [
- {
- "allocate_replica": {
- "index": "indexName",
- "shard": 分片id,
- "node": "节点名"
- }
- }
- ]
- }
- curl -XPOST 'node3:9205/_cluster/reroute' -d
- {
- "commands": [
- {
- "allocate_stale_primary": {
- "index": "mail_store",
- "shard": 1,
- "node": "slave2",
- "accept_data_loss": true
- }
- }
- ]
- }
使用此命令可能会导致所提供的分片ID发生数据丢失。如果稍后具有良好数据副本的节点重新加入群集,则该数据将被使用此命令强制分配的旧副本数据覆盖。为确保这些影响得到充分理解,该命令需要accept_data_loss明确设置专用字段才能true使其工作。
- curl -XPUT '/indexName/_settings' -d '{
- "cluster.routing.allocation.disable_allocation" : true
- }'
- curl -XPOST "/_cluster/reroute' -d '{
- "commands" : [{
- "move" : {
- "index" : "xlog",
- "shard" : 0,
- "from_node" : "es-0",
- "to_node" : "es-3"
- }
- }]
- }'
* 主分片和启动状态下的分片不能取消
- curl -XPOST "/_cluster/reroute" -d '{
- "commands" : [ {
- "cancel" : {
- "index" : "ops",
- "shard" : 0,
- "node" : "es_node_one"
- }
- } ]
- }'
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。