elasticsearch rest api遵循的格式为:

curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>

1、检查es版本信息

curl IP:9200

2、查看集群是否健康

http://IP:9200/_cat/health?v
curl -s -XGET 'http://IP:9200/_cat/health?v'
1 绿色,最健康的状态,代表所有的分片包括备份都可用
2 黄色,预警状态,所有主分片功能正常,但至少有一个副本是不能正常工作的。此时集群是可以正常工作的,但是高可用性在某种程度上会受影响。
3 红色,集群不可正常使用。某个或某些分片及其副本异常不可用,这时集群的查询操作还能执行,但是返回的结果会不准确。对于分配到这个分片的写入请求将会报错,最终会导致数据的丢失。 
curl -s -XGET -uelastic:XleBzj311mE  'http://192.168.1.200:9200/_cat/health?v'

3、查看节点列表

http://IP:9200/_cat/nodes?v
curl 'IP:9200/_cat/nodes?v'

4、列出所有索引及存储大小

http://IP:9200/_cat/indices?v
curl 'IP:9200/_cat/indices?v'--查询所有索引及数据大小

5、创建索引

创建索引名为XX,默认会有5个分片,1个索引
curl -XPUT 'IP:9200/XX?pretty'
curl -k -uelastic:RKnzx1R0NXVoYc2AdCCI -XPUT 'https://172.31.1.4:9201/vehicle?pretty'  -H "Content-Type: application/json" -d '{"settings": {"number_of_shards": 3,"number_of_replicas": 1},"mappings": {"properties": {"position": { "type": "geo_point" },"vin": { "type": "keyword" },"userId": { "type": "keyword" },"modelCode":{"type": "keyword" }}}}'
curl -XPUT 'http://192.168.1.2:9200/vehicle?pretty' -H "Content-Type: application/json" -d '{"mappings" : {"status" : {"properties" : {"aveFuelConsumption" : {"type" : "double"},"aveFuelConsumption_ts" : {"type" : "long"},"eventId" : {"type" : "long"},"modelCode" : {"type" : "string","index" : "not_analyzed"},"odometer" : {"type" : "integer"},"odometer_ts" : {"type" : "long"},"position" : {"type" : "geo_point"},"position_ts" : {"type" : "long"},"userId" : {"type" : "string"},"vin" : {"type" : "string"}}}}}'

 

6、添加一个类型

curl -XPUT 'IP:9200/XX/external/2?pretty' -d '
{
   "gwyy": "John"
}'

7、更新一个类型

curl -XPOST 'IP:9200/XX/external/1/_update?pretty' -d '
{
   "doc": {"name": "Jaf"}
}'

8、删除指定索引

curl -XDELETE 'IP:9200/_index?pretty' 

9、ES数据定期删除

如果不删除ES数据,将会导致ES存储的数据越来越多,磁盘满了之后将无法写入新的数据。这时可以使用脚本定时删除过期数据。

#!/bin/bash
#es-index-clear
#只保留20天内的日志索引
ip="192.168.1.211 192.168.1.212 192.168.1.213 192.168.1.214 192.168.1.215 192.168.1.216"
LAST_DATA=`date -d "-20 days" "+%Y.%m.%d"`
#删除上个月份所有的索引(根据自己的索引格式编写)
for i in ${ip}
do
    curl -XDELETE -uelastic:XleBzj3GmE http://${i}:9200/*-${LAST_DATA}.*
    echo "curl -XDELETE -uelastic:XleBzj3GmE http://${i}:9200/*-${LAST_DATA}.*"
done

crontab -e添加定时任务:每天的凌晨一点清除索引。

0 1 * * * /search/odin/elasticsearch/scripts/es-index-clear.sh

10、查看分片状态

curl -XGET http://localhost:9200/_cat/shards

11、查看群集状态

curl -s -XGET 'http://localhost:9200/_cluster/stats?pretty'

12、索引管理

查询全部索引状态
curl 'localhost:9200/_cat/indices?v'
查询状态为red的索引
curl -s -XGET 'http://IP:9200/_cat/indices?health=red

13、创建索引

curl -XPUT 'localhost:9200/goods_v1?pretty' 

14、查看索引

curl '172.31.15.228:9200/goods_v1?pretty'

15、删除索引

curl -XDELETE 'localhost:9200/goods_v1?pretty'

16、优化索引

curl -XPOST "http://127.0.0.1:9200/logstash-2015.10.28/_optimize"

curl -XGET 'http://localhost:9200/logstash-2015.10.28/_search?pretty=true' -d '
{
    "query" : {
        "matchAll" : {}
    }
}' 

索引刷新

curl -XPOST 'http://127.0.0.1:9200/index_name/_refresh'  #单个索引刷新
curl -XPOST 'http://127.0.0.1:9200/_refresh'             #全部索引刷新

flush释放该索引所占用的内存,并将索引数据保存在磁盘

curl -XPOST 'http://127.0.0.1:9200/index_name/_flush?force'

清理缓存

curl  -XPOST 'http://127.0.0.1:9200/_cache/clear'

17、创建别名

curl -XPOST localhost:9200/_aliases -d '
{
    "actions": [
        { "add": {
            "alias": "goods",
            "index": "goods_v1"
        }}
    ]
}'
curl -XPOST "http://192.168.1.2:9200/_aliases" -d '{"actions": [{ "add": {"alias": "vehicle","index": "vehicle1"}}]}'

18、删除并更新别名

curl -XPOST 'http://localhost:9200/_aliases' -d '
{
    "actions" : [
        { "remove" : { "index" : "goods_v2", "alias" : "goods" } },
        { "add" : { "index" : "goods_v1", "alias" : "goods" } }
    ]
}'

19、查看已有别名

curl -XGET 'localhost:9200/_cat/aliases'

20、查看别名对应的索引

curl -XGET 'localhost:9200/_alias/help'

21、查看文件描述符打开数量

curl -s -XGET '127.0.0.1:9200/_nodes/stats/process/?pretty'

22、查看分片状态

curl -XGET "http://127.0.0.1:9200/_cat/shards"

23、查看热点线程 

curl -XGET 'http://127.0.0.1:9200/_nodes/hot_threads'

24、针对不使用的index,进行close。我们需要的时候再进行open,可以节约内存和减轻系统的压力

curl -XPOST 127.0.0.1:9200/index_name/_close 
curl -XPOST 127.0.0.1:9200/index_name/_open 25 18520796770

25、查看线程状态

curl -XGET "http://127.0.0.1:9200/_nodes/thread_pool/"

26、修改密码

curl -H "Content-Type:application/json" -XPOST -u elastic 'http://192.168.36.61:9200/_xpack/security/user/elastic/_password' -d '{ "password" : "123456" }'

27、开启关闭索引生命周期策略

GET _ilm/status

POST _ilm/stop

POST _ilm/start

28、match查询语句

curl -H 'Content-Type: application/json' -uelastic:******* 'http://192.168.1.1:9200/index/_count' -H "Content-Type: application/json" -d '{"query": {"match": {"title":"中国"}}}'
curl -XPOST 192.168.1.1:9200/vehicle/_search?pretty -H "Content-Type: application/json" -d '{"query": {"match": {"modelCode": "xxx"}},"sort":{ "odometer": { "order": "desc" }},"size":5}'

29、单个索引迁移

curl -k -uelastic:RKnzx1R0N  -XPOST "https://new_ip:9200/_reindex" -H 'Content-Type:application/json'  -d '{"source":{"remote":{"host":"http://ip:9200/","username": "user","password": "password"},"index":"old_index"},"dest":{"index":"new_index"}}'

30、查询单个数据

curl -k -uelastic:RKnzx1R0 -XGET 'https://172.31.1.1:9200/vehicle/_doc/KC2B0vin0test0088?pretty'

 

 

 

  

 

相关文章: