curl命令操作ElasticSearch总结
端口9200和9300的关系
9200作为Http协议端口,用于节点和外部通讯。
9300作为Tcp协议端口,用于节点与节点之间、节点与TCPClient之间的通讯。
cat命令获取集群信息
cat系列提供了一系列查询ES集群状态的接口。你可以通过执行 curl -XGET localhost:9200/_cat 命令,获取所有cat系列的操作,可以在下列命令后加上?v格式化输出,也可以加上?help查看命令相关信息。结果如下:
[root@C20-23U-10 ~]# curl -XGET localhost:9200/_cat
=^.^=
/_cat/allocation 查看节点分配情况。
/_cat/shards 看分片情况。
/_cat/master 查看主节点。
/_cat/nodes 查看所有节点。
/_cat/indices 查看所用索引状态。
/_cat/segments 查看索引的分片信息。
/_cat/count 查看文档个数。
/_cat/health 查看集群健康情况。
.........
查看集群是否健康
curl -XGET localhost:9200/_cat/health?v
绿色——最健康的状态,代表所有的主分片shard和副本分片replica都可用。
黄色——所有的主分片shard可用,但是部分副本分片replica不可用。
红色——部分主分片shard不可用。(此时执行查询部分数据仍然可以查到,遇到这种情况,还是赶快解决比较好)。
查看节点版本信息
curl -XGET localhost:9200
获取所有索引信息
curl -XGET localhost:9200/_cat/indices?v
获取单个索引信息
[root@C20-23U-10 ~]# curl -XGET http://localhost:9200/fei?pretty
{
"fei" : {
"aliases" : { },
"mappings" : {
"gege" : {
"properties" : {
"name" : {
"type" : "string"
},
"sex" : {
"type" : "string"
}
}
}
},
"settings" : {
"index" : {
"creation_date" : "1559447228188",
"number_of_shards" : "5",
"number_of_replicas" : "1",
"uuid" : "ti03rgsETR6JaX-uwfiTTQ",
"version" : {
"created" : "2040599"
}
}
},
"warmers" : { }
}
}
获取所有type类型信息
curl -XGET http://localhost:9200/_mapping?pretty=true
获取指定索引的type类型信息
{
"fei" : {
"mappings" : {
"gege" : {
"properties" : {
"age" : {
"type" : "long"
},
"name" : {
"type" : "string"
},
"sex" : {
"type" : "string"
}
}
}
}
}
}
增:添加一个文档,同时索引、类型、文档id也同时生成
如果id不指定,则ES会自动帮你生成一个id,就不再演示了。
curl -XPUT http://localhost:9200/fei/gege/1?pretty -d'{
"name":"feigege",
"sex":"man"
}'
结果:
{
"_index" : "fei",
"_type" : "gege",
"_id" : "1",
"_version" : 1,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}
查:根据index,type,id查询文档信息
查询索引为fei,类型为gege, id为1的文档信息。
curl -XGET http://localhost:9200/fei/gege/1?pretty
结果:
{
"_index" : "fei",
"_type" : "gege",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "feigege",
"sex" : "man"
}
}
查:根据index,type,其他字段查询文档信息
#查询名字里有fei的人。
-XGET http://localhost:9200/fei/gege/_search?pretty=true&q=name:fei
改:修改原有的数据,注意文档的版本!
curl -XPOST http://localhost:9200/fei/gege/1?pretty -d '{
"name":"feigege",
"sex":"woman"
}'
结果(注意版本变化):
{
"_index" : "fei",
"_type" : "gege",
"_id" : "1",
"_version" : 2,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : false
}
删:删除文档,删除类型,删除索引!
删除文档:
curl -XDELETE http://localhost:9200/fei/gege/1?pretty
结果:
{
"found" : true,
"_index" : "fei",
"_type" : "gege",
"_id" : "1",
"_version" : 4,
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
}
}
删除类型:
现在的Elasticsearch已经不支持删除一个type了。
要么从新设置index,要么删除类型下的所有数据。
##删除索引
curl -XDELETE -u elastic:changeme http://localhost:9200/fei?pretty
{
"acknowledged" : true
}