Elasticsearch 5.4 Indices(索引) API
来源:互联网 发布:网络用语ac是什么意思 编辑:程序博客网 时间:2024/06/05 07:29
- 前言
- 一索引管理
- 1 创建索引
- 2 删除索引
- 3 查看索引信息
- 4 索引是否存在
- 5 关闭打开索引
- 6 索引收缩
- 7 翻滚索引
- 二mapping管理
- 1 设置mapping
- 2 查看mapping
- 3 获取字段mapping
- 4 类型是否存在
- 三别名管理
- 1 索引别名设置
- 四索引配置
- 1 获取索引设置
- 2 更新索引设置
- 3 分析器
- 4 索引模板
- 五监控管理
- 1 索引统计信息
- 2 索引段
- 3 索引恢复
- 4 索引分片存储
- 六状态管理
- 1 清除缓存
- 2 刷新
- 3 flush
- 4 强制段合并force merge
前言
声明:本博客根据ELasticsearch官网文档翻译整理,转载请注明出处:http://blog.csdn.net/napoay
索引API可以用于管理单个索引、索引设置、别名、映射和索引模板。
一、索引管理
1.1 创建索引
创建索引
PUT twitter
默认分片为5,副本为1.
创建索引并指定分片数和副本数:
PUT twitter{ "settings" : { "index" : { "number_of_shards" : 3, "number_of_replicas" : 2 } }}
或者简写为:
PUT twitter{ "settings" : { "number_of_shards" : 3, "number_of_replicas" : 2 }}
创建索引并指定mapping:
PUT test{ "settings" : { "number_of_shards" : 1 }, "mappings" : { "type1" : { "properties" : { "field1" : { "type" : "text" } } } }}
1.2 删除索引
DELETE /twitter
1.3 查看索引信息
查看所有的settings、别名、mapping,命令:
GET /twitter
添加参数过滤信息:
GET twitter/_settings,_mappings
1.4 索引是否存在
如果想知道集群中是否存在某个索引,可以使用以下命令:
HEAD twitter
如果存在,返回状态码200:
200 - OK
如果不存在,返回状态码404:
404 - Not Found
1.5 关闭/打开索引
对于不使用的索引,关闭索引可以节省开销,但是索引关闭以后读写操作是无法进行的。
打开索引:
POST /my_index/_close
关闭索引:
POST /my_index/_open
可以同时关闭多个索引,如果其中有索引不存在会报异常,可以使用ignore_unavailable=true
参数忽略不存在索引。
1.6 索引收缩
shrink index AP可以把一个索引变成一个更少分片的索引,但是收缩后的分片数必须是原始分片数的因子(因子就是所有可以整除这个数的数,不包括这个数自身),比如有8个分片的索引可以收缩为4、2、1,有15个分片的索引可以收缩为5、3、1,如果分片数为素数(7、11等),那么只能收缩为1个分片。 收缩索引之前,索引中的每个分片都要在同一个节点上。
收缩索引的完成过程:
- 首先,创建了一个新的目标索引,设置与源索引相同,但新索引的分片数量较少。
- 然后把源索引的段到硬链接到目标索引。(如果文件系统不支持硬链接,那么所有段都被复制到新索引中,这是一个耗费更多时间的过程。)
- 最后,新的索引恢复使用,好像它是一个刚刚重新开放的封闭索引。
搜索之前,使索引为只读状态并使分片重新分配到同一个节点:
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": {} }}
1.7 翻滚索引
二、mapping管理
2.1 设置mapping
put mapping可以给一个已存在的索引增加type的mapping,也可以给一个存在的type增加字段的mapping。
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" } }}
一般情况下字段的mapping设置是不可以更新的,有几个特例除外:
- properties嵌套属性可以新增
- ignore_above 参数的值可以更新
PUT my_index { "mappings": { "user": { "properties": { "name": { "properties": { "first": { "type": "text" } } }, "user_id": { "type": "keyword" } } } }}PUT my_index/_mapping/user{ "properties": { "name": { "properties": { "last": { "type": "text" } } }, "user_id": { "type": "keyword", "ignore_above": 100 } }}
2.2 查看mapping
查看一个索引的mapping:
GET /twitter/_mapping
查看一个索引的一个type的mapping:
GET /twitter/_mapping/tweet
查看所有索引的mapping:
GET /_mapping
或者:
GET /_all/_mapping
2.3 获取字段mapping
get field mapping api可以查看索引的一个或多个字段的mapping,设置创建一个索引做测试:
PUT publications{ "mappings": { "article": { "properties": { "id": { "type": "text" }, "title": { "type": "text"}, "abstract": { "type": "text"}, "author": { "properties": { "id": { "type": "text" }, "name": { "type": "text" } } } } } }}
GET publications/_mapping/article/field/titleGET publications/_mapping/article/field/idGET publications/_mapping/article/field/author.id
2.4 类型是否存在
查看索引是否存在某个type:
HEAD twitter/_mapping/tweet
返回值为200说明存在,404说明不存在。
三、别名管理
3. 1 索引别名设置
可以给一个或多个索引设置别名,但是别名不能和已有索引名称相同。
给索引名为test1的索引设置别名为alias1:
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" } } ]}
也可以使用通配符,一下所有以test开头的索引都设置别名为all_test_indices:
POST /_aliases{ "actions" : [ { "add" : { "index" : "test*", "alias" : "all_test_indices" } } ]}
四、索引配置
4.1 获取索引设置
查看索引的settings:
GET /twitter/_settings
查看多个索引的settings:
GET /twitter,kimchy/_settingsGET /_all/_settingsGET /log_2013_*/_settings
4.2 更新索引设置
修改副本:
PUT /twitter/_settings{ "index" : { "number_of_replicas" : 2 }}
修改settings用于提高Bulk的导入性能,bulk之前设置刷新时间为-1,也就是bulk导入期间不再刷新:
PUT /twitter/_settings{ "index" : { "refresh_interval" : "-1" }}
bulk导入之后恢复刷新时间并强制段合并
PUT /twitter/_settings{ "index" : { "refresh_interval" : "1s" }}POST /twitter/_forcemerge?max_num_segments=5
4.3 分析器
查看分词标准分词结果:
GET _analyze{ "analyzer" : "standard", "text" : "this is a test"}
查看IK分词结果:
GET _analyze{ "analyzer" : "ik_smart", "text" : "北京今天局地高温"}
4.4 索引模板
索引模板可以自动匹配新创建的索引。
PUT _template/template_1{ "template": "te*", "settings": { "number_of_shards": 1 }, "mappings": { "type1": { "_source": { "enabled": false }, "properties": { "host_name": { "type": "keyword" }, "created_at": { "type": "date", "format": "EEE MMM dd HH:mm:ss Z YYYY" } } } }}
删除索引模板:
DELETE /_template/template_1
查看索引模板:
GET /_template
查看一个或多个:
GET /_template/template_1GET /_template/template_1,template_2
五、监控管理
5.1 索引统计信息
GET /_statsGET /index1,index2/_stats
以上命令返回的索引的相关信息非常多,可以通过参数过滤https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-stats.html
5.2 索引段
segment是比Lucene索引更小的单位,通过segment可以获取更多的关于分片和索引的信息。
查看索引的段信息:
GET test/_segments
返回结果:
{ "_shards": { "total": 5, "successful": 5, "failed": 0 }, "indices": { "test": { "shards": { "0": [ { "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" }, "num_committed_segments": 0, "num_search_segments": 0, "segments": {} } ], "1": [ { "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" }, "num_committed_segments": 0, "num_search_segments": 0, "segments": {} } ], "2": [ { "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" }, "num_committed_segments": 0, "num_search_segments": 0, "segments": {} } ], "3": [ { "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" }, "num_committed_segments": 1, "num_search_segments": 1, "segments": { "_1": { "generation": 1, "num_docs": 1, "deleted_docs": 0, "size_in_bytes": 3727, "memory_in_bytes": 2588, "committed": true, "search": true, "version": "6.5.0", "compound": true } } } ], "4": [ { "routing": { "state": "STARTED", "primary": true, "node": "3dQd1RRVTMiKdTckM68nPQ" }, "num_committed_segments": 1, "num_search_segments": 1, "segments": { "_0": { "generation": 0, "num_docs": 1, "deleted_docs": 0, "size_in_bytes": 3206, "memory_in_bytes": 2042, "committed": true, "search": true, "version": "6.5.0", "compound": true } } } ] } } }}
统计索引占段内存:
curl -s "http://localhost:9200/_cat/segments/test?v&h=shard,segment,size,size.memory" |awk '{sum += $NF} END {print sum}'
5.3 索引恢复
GET index1,index2/_recovery?humanGET _recovery?human&detailed=true
5.4 索引分片存储
六、状态管理
6.1 清除缓存
POST /twitter/_cache/clearPOST /kimchy,elasticsearch/_cache/clearPOST /_cache/clear
6.2 刷新
POST /twitter/_refreshPOST /kimchy,elasticsearch/_refreshPOST /_refresh
6.3 flush
POST twitter/_flushPOST kimchy,elasticsearch/_flushPOST _flush
6.4 强制段合并(force merge)
POST /twitter/_forcemergePOST /kimchy,elasticsearch/_forcemergePOST /_forcemerge
- Elasticsearch 5.4 Indices(索引) API
- Elasticsearch java API (26) Indices 指标管理
- elasticsearch api 创建索引
- ElasticSearch中,集群(Cluster),节点(Node),分片(Shard),Indices(索引),replicas(备份)之间的关系
- elasticsearch 索引恢复 查看 API
- ElasticSearch Java Api -删除索引
- Elasticsearch Java API之清空索引
- 通过HTTP RESTful API 操作elasticsearch索引
- elasticsearch清空索引缓存 java api
- ElasticSearch Java Api(二) -检索索引库
- ElasticSearch Java Api(三) -更新索引库
- ElasticSearch Java Api(四) -删除索引
- elasticsearch建立索引操作的API
- Elasticsearch java api(五) Bulk批量索引
- [ElasticSearch]Java API 之 索引管理
- elasticsearch建立索引操作的API
- ElasticSearch Java Api(一) -创建索引
- ElasticSearch Java Api(二) -检索索引库
- 如何训练你的神经网络?
- android 好的代码习惯
- 构造函数的执行顺序
- 计蒜之道-2017复赛-腾讯消消乐(状压DP)
- 《软件工程》学习总结
- Elasticsearch 5.4 Indices(索引) API
- CardView(1)
- hdoj2079 选课时间(题目已修改,注意读题) (母函数)
- git踩坑 及 一些需要注意的地方
- 并行化资源池队列 2 —— 无锁化的无界队列
- 阿里云配置MYSQL远程连接
- NumPy 特殊数组与通用函数
- 设计模式之访问者模式(Visitor)
- 安装openni2驱动xtion