端口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
}