1、相關查詢語句
1.檢測叢集是否健康
叢集狀态
[[email protected]-01 ~]# curl -XGET -u elastic:123456 'localhost:9200/_cat/health?v'
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1592817451 17:17:31 cluster-es green 2 2 60 30 0 0 0 0 - 100.0%
目前es叢集狀态為green
注:有的會由于加載得索引資料比較多索引顯示為yellow,正常情況下,叢集得健康狀态分為三種:
green 最健康得狀态,說明所有的分片包括備份都可用
yellow 基本的分片可用,但是備份不可用(或者是沒有備份)
red 部分的分片可用,表明分片有一部分損壞。此時執行查詢部分資料仍然可以查到,遇到這種情況,還是趕快解決比較好
從上邊檢視的叢集狀态中還包括一些其他資訊:
叢集名字時elasticsearch,叢集的節點個數為2,活動分片百分比為100%
2.檢視分片狀态
[[email protected]-01 ~]# curl -X GET -u elastic:123456 'http://localhost:9200/_cluster/health?pretty'
{
"cluster_name" : "cluster-es",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 2,
"active_primary_shards" : 30,
"active_shards" : 60,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
3.檢視許可證使用期限
[[email protected]-01 ~]# curl -XGET -u elastic:123456 'http://localhost:9200/_license'
{
"license" : {
"status" : "active",
"uid" : "5227c16c-b7c8-4cd0-ad80-ab3d6200bca7",
"type" : "trial",
"issue_date" : "2020-06-22T08:18:57.356Z",
"issue_date_in_millis" : 1592813937356,
"expiry_date" : "2020-07-22T08:18:57.356Z",
"expiry_date_in_millis" : 1595405937356,
"max_nodes" : 1000,
"issued_to" : "es_cluster",
"issuer" : "elasticsearch",
"start_date_in_millis" : -1
}
}
經結果可知,x-pack插件許可證試用期一個月到期,将不可使用;
4.檢視版本号(這裡的版本:5.2.2)
[[email protected]-01 ~]# curl -XGET -u elastic:123456 'localhost:9200'
{
"name" : "node-01",
"cluster_name" : "es_cluster",
"cluster_uuid" : "4usMS_xdSyaA91AMUl734A",
"version" : {
"number" : "5.2.2",
"build_hash" : "f9d9b74",
"build_date" : "2017-02-24T17:26:45.835Z",
"build_snapshot" : false,
"lucene_version" : "6.4.1"
},
"tagline" : "You Know, for Search"
}
5.擷取叢集的節點清單
[[email protected]-01 ~]# curl -XGET -u elastic:123456 'localhost:9200/_cat/nodes?v'
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.0.164 50 96 8 0.40 0.41 0.41 mdi * 192.168.0.164
192.168.0.165 27 96 23 0.55 0.54 0.57 di - 192.168.0.165
6.列出所有索引
[[email protected]-01 ~]# curl -XGET -u elastic:123456 'localhost:9200/_cat/indices?v'
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open .security qsSmPcwOTQ6SLnYDsj-Pkw 1 0 1 0 2.8kb 2.8kb
green open mytest TXHPYUPHTpuj3Q0Ww8Lj-g 5 0 1 0 5.4kb 5.4kb
green open .monitoring-es-2-2020.06.22 EzdBgDbqTuChat-dFk5kig 1 0 4896 56 3.3mb 3.3mb
green open .monitoring-data-2 RNfUinAYQICA9I5KNkJ2HQ 1 0 2 0 37kb 37kb
7.建立新索引
[[email protected]-01 ~]# curl -XPUT -u elastic:123456 'localhost:9200/customer?pretty'
{
"acknowledged" : true,
"shards_acknowledged" : true
}
8.插入和擷取資料
現在我麼插入一些資料到叢集索引。我們必須給ES指定是以的類型。如下語句:“external” type, ID:1:
主體為JSON格式的語句: { “name”: “Bertram” }
[[email protected]-01 ~]# curl -XPUT -u elastic:123456 'localhost:9200/customer/external/1?pretty' -d '{ "name":"Bertram" }'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true
}
傳回結果為:create:true 表示插入成功。
擷取GET,語句如下:
[[email protected]-01 ~]# curl -XGET -u elastic:123456 'localhost:9200/customer/external/1?pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "Bertram"
}
}
其中含義為:擷取customer索引下類型為external,id為1的資料,pretty參數表示傳回結果格式美觀。
9.删除索引 DELETE
[[email protected]-01 ~]# curl -XDELETE -u elastic:123456 'localhost:9200/customer?pretty'
{
"acknowledged" : true
}
10.修改資料
[[email protected]-01 ~]# curl -XPUT -u elastic:123456 'localhost:9200/customer/external/1?pretty' -d '{ "name":"Bertram修改" }'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : false
}
11.更新資料
[[email protected]-01 ~]# curl -XPOST -u elastic:123456 'localhost:9200/customer/external/1/_update?pretty' -d '{"doc": {"name": "Bertram修改", "age": 20 }}'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
}
}
12.根據關鍵詞搜尋資料
[[email protected]-01 ~]# curl -u elastic:123456 'localhost:9200/customer/_search?q=result:updated&pretty'
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
}
}
13.查詢某個索引中的所有資料
[[email protected]-01 ~]# curl -u elastic:123456 'localhost:9200/customer/_search?q=*&pretty'
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "Bertram修改",
"age" : 20
}
}
]
}
}
等價于
[[email protected]-01 ~]# curl -XPOST -u elastic:123456 http://localhost:9200/customer/_search?pretty -d '{"query":{ "match_all":{}}}'
{
"took" : 5,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 2,
"max_score" : 1.0,
"hits" : [
{
"_index" : "customer",
"_type" : "external",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"name" : "Bertram修改"
}
},
{
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "chj"
}
}
]
}
}
14.删除資料
[[email protected]-01 ~]# curl -XDELETE -u elastic:123456 'localhost:9200/customer/external/1?pretty'
{
"found" : true,
"_index" : "customer",
"_type" : "external",
"_id" : "1",
"_version" : 5,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
}
}
注:customer索引名,external mappings類型,1為id
15.清空某索引下的全部資料
curl:清理如下
注:customer索引名,external mappings類型
web界面清理:
POST customer/external/_delete_by_query
{
"query": {"match_all": {}}
}
16.number_of_shards
number_of_shards
隻能在建立 index 指定,後期無法修改
建立 testindex
curl -XPUT 'http://192.168.1.128:9200/testindex' -H 'Content-Type: application/json' -d '
{
"settings": {
"number_of_shards": 5,
"number_of_replicas" : 2
}
}'
檢視 testindex 索引
curl -XGET 'http://192.168.1.128:9200/testindex/_settings?pretty'
{
"testindex" : {
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "5",
"provided_name" : "testindex",
"creation_date" : "1629957135326",
"number_of_replicas" : "2",
"uuid" : "JGdxP7HcRkCEGDtVFmdMrA",
"version" : {
"created" : "7130299"
}
}
}
}
}
17.number_of_replicas
number_of_replicas
可以線上修改
curl -XPUT 'http://192.168.1.128:9200/testindex/_settings' -H 'Content-Type: application/json' -d '
{
"index" : {
"number_of_replicas" : 0
}
}'
遇到問題解決方法:
1.檢視分片,叢集狀态為yellow
[[email protected] elasticsearch-head]# curl -X GET -u elastic:123456 'http://localhost:9200/_cluster/health?pretty'
{
"cluster_name" : "es_cluster",
"status" : "yellow",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 8,
"active_shards" : 8,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 7,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 53.333333333333336
}
由于我們是單節點部署elasticsearch,而預設的分片副本數目配置為1,而相同的分片不能在一個節點上,是以就存在副本分片指定不明确的問題,是以顯示為yellow,我們可以通過在elasticsearch叢集上添加一個節點來解決問題,如果你不想這麼做,你可以删除那些指定不明确的副本分片(當然這不是一個好辦法)但是作為測試和解決辦法還是可以嘗試的,下面我們試一下删除副本分片的辦法
解決辦法
這個時候再次檢視叢集的狀态狀态變成了green
[[email protected] elasticsearch-head]# curl -X GET -u elastic:123456 'http://localhost:9200/_cluster/health?pretty'
{
"cluster_name" : "es_cluster",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 1,
"number_of_data_nodes" : 1,
"active_primary_shards" : 8,
"active_shards" : 8,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
}
參考:文章