Elasticsearch REST API小记
集群健康状态
[root@elastic1
~]#
epoch
1462196271
21:37:51
列出所有目录
[root@elastic1
~]#
health
status index pri rep docs.count docs.deleted store.size
pri.store.size
[root@elastic1
~]#
{
}
[root@elastic1 ~]# curl 'http://192.168.8.101:9200/_cat/indices?v'
health
status index
yellow
open
提示:health为yellow, 是因为目前是单节点,等加入了replica之后就会转为green
查询Document
[root@elastic1
~]#
{
}'
{
}
[root@elastic1
~]#
{
}
删除Index
https://www.elastic.co/guide/en/elasticsearch/reference/current/_delete_an_index.html
[root@elastic1
~]#
{
}
[root@elastic1 ~]# curl 'http://192.168.8.101:9200/_cat/indices?v'
health status index pri rep docs.count docs.deleted store.size pri.store.size
修改数据
https://www.elastic.co/guide/en/elasticsearch/reference/current/_modifying_your_data.html
[root@elastic1 ~]# curl -XPUT 'http://192.168.8.101:9200/customer/external/1?pretty' -d '
{
}'
{
}
[root@elastic1 ~]# curl -XPUT 'http://192.168.8.101:9200/customer/external/1?pretty' -d '
{
}'
{
}
ID部分(上面标红的部分)是可选的,上面两条只会修改内容,不会新增,要新增需要不同的ID,如下
[root@elastic1 ~]# curl -XPUT 'http://192.168.8.101:9200/customer/external/2?pretty' -d '
{
}'
{
}
采用POST方式时,没有指定ID,Elasticsearch会随机生成一个ID
[root@elastic1 ~]# curl -XPOST 'http://192.168.8.101:9200/customer/external?pretty' -d '
{
}'
{
}
[root@elastic1 ~]# curl 'http://192.168.8.101:9200/_cat/indices?v'
health
status index
yellow
open
更新Document
[root@elastic1 ~]# curl -XPOST 'http://192.168.8.101:9200/customer/external/1/_update?pretty' -d '
{
}'
{
}
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting.html
也可以通过script来更新,前提是要先启用script
config/elasticsearch.yml中加入最简的两行即可,更严格的动作限制见官网说明
script.inline: true
script.indexed: true
[root@elastic1 ~]# curl -XPOST 'http://192.168.8.101:9200/customer/external/1/_update?pretty' -d '
{
}'
{
}
删除Document
[root@elastic1 ~]# curl -XDELETE 'http://192.168.8.101:9200/customer/external/2?pretty'
{
}
提示:delete-by-query插件能删除所有匹配查询到的Document
批处理
[root@elastic1 ~]# curl -XPOST 'http://192.168.8.101:9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
{
}
[root@elastic1 ~]# curl -XPOST 'http://192.168.8.101:9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
{
}