【elasticsearch】elasticsearch的基本使用

1.elasticsearch 的基本使用 

--查看节点 
curl http://192.168.1.7:9200/_cat/nodes
[esadmin@oracle1 config]$ curl http://192.168.1.7:9200/_cat/nodes
192.168.1.7 56 50 0 0.01 0.26 0.37 cdfhilmrstw * 192.168.1.7

2.查看分片 

curl 'http://192.168.1.7:9200/_cat/shards?v'
[esadmin@oracle1 config]$ curl 'http://192.168.1.7:9200/_cat/shards?v'
index       shard prirep state   docs  store dataset ip          node
.security-7 0     p      STARTED    3 20.9kb  20.9kb 192.168.1.7 192.168.1.7
--只有一个分片 

3.查看节点健康状态 

[esadmin@oracle1 config]$ curl http://192.168.1.7:9200/_cat/health?pretty -H 'Content-Type: application/json'
1719284823 03:07:03 elasticsearch green 1 1 1 1 0 0 0 0 - 100.0%

green 表示健康。

4.插入数据

[esadmin@oracle1 config]$ curl -XPOST http://192.168.1.7:9200/itpuxdb/_doc/?pretty -H 'Content-Type: application/json' -d '
> {
> "id" : "1",
> "name" : "itpux001",
> "city" : "深圳",
> "course" : "oracle",
> "teacher" : "风哥",
> "pxdate": "20200829"
> }'

{
  "_index" : "itpuxdb",
  "_id" : "o71gTZABnYKCubPb0PI6",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

--查询插入的数据。
[esadmin@oracle1 config]$ curl -XGET 192.168.1.7:9200/itpuxdb/_search
{"took":108,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":1,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"itpuxdb","_id":"o71gTZABnYKCubPb0PI6","_score":1.0,"_source":
{
"id" : "1",
"name" : "itpux001",
"city" : "深圳",
"course" : "oracle",
"teacher" : "风哥",
"pxdate": "20200829"
}}]}}[esadmin@oracle1 config]$

4.修改分片数 

--设置6个分片,每个分片一个副本。
curl -XPUT 192.168.1.7:9200/_template/all_index_shards -H 'Content-Type: application/json' -d '
{
"index_patterns": "*",
"settings": {
"number_of_shards": 3,
"number_of_replicas": "1"
}
}'

--需要注意,分片是设置,只对以后创建的索引有效。
--之前创建的索引将保持原来的状态。


[esadmin@oracle1 config]$ curl 'http://192.168.1.7:9200/_cat/shards?v'
index                                                         shard prirep state      docs  store dataset ip          node
.ds-.logs-deprecation.elasticsearch-default-2024.06.25-000001 0     p      STARTED       1 10.5kb  10.5kb 192.168.1.7 192.168.1.7
.security-7                                                   0     p      STARTED       3 20.9kb  20.9kb 192.168.1.7 192.168.1.7
itpuxdb                                                       0     p      STARTED       1    8kb     8kb 192.168.1.7 192.168.1.7
itpuxdb                                                       0     r      UNASSIGNED                                 
.ds-ilm-history-7-2024.06.25-000001                           0     p      STARTED       3  9.8kb   9.8kb 192.168.1.7 192.168.1.7

--由于设置了主备分片不能在同一个节点上。
--所以,出现UNASSIGNED 状态。
curl 'http://192.168.1.7:9200/_cat/shards?v'
mydb1  0     p      STARTED       0   249b    249b 192.168.1.7 192.168.1.7
mydb1  0     r      UNASSIGNED                                 
mydb1  1     p      STARTED       1    8kb     8kb 192.168.1.7 192.168.1.7
mydb1  1     r      UNASSIGNED                                 
mydb1  2     p      STARTED       1    8kb     8kb 192.168.1.7 192.168.1.7
mydb1  2     r      UNASSIGNED                                 
mydb1  3     p      STARTED       4 16.3kb  16.3kb 192.168.1.7 192.168.1.7
mydb1  3     r      UNASSIGNED                                 



--副本不能和主副本放在相同的分片。
curl -XPUT '192.168.1.7:9200/_cluster/settings' -H 'Content-Type: application/json' -d '
{
"transient": {
"cluster.routing.allocation.same_shard.host": false
}
}'

{"acknowledged":true,"persistent":{},"transient":{"cluster":{"routing":{"allocation":{"same_shard":{"host":"true"}}}}}}




--查询所有数据。
curl -XPOST 'http://192.168.1.7:9200/itpuxdb/_search?pretty' -H 'Content-Type: application/json'

--开启重分布 
curl -XPUT http://192.168.1.7:9200/_cluster/settings -H 'Content-Type: application/json' -d '{
"transient" : {
"cluster.routing.allocation.enable" : "all"
}
}'

--关闭重分布 
curl -XPUT http://192.168.1.7:9200/_cluster/settings -H 'Content-Type: application/json' -d '{
"transient" : {
"cluster.routing.allocation.enable" : "none"
}
}'


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值