ElasticSearch常用命令与curl操作总结

ElasticSearch常用命令

查看ES是否启动

jps -l

可以查看ES进程号,例如

17323 org.elasticsearch.bootstrap.ElasticSearch

如果要关闭,可以 kill -9 进程号

kill -9 17323

查看Kibana是否启动

因为kibana是node写的,运行在node里面,因此

ps -ef | grep kibana

无法查看;需要使用

ps -ef | grep node

不过这样不够精确;最好还是在知道端口号(如9100)的情况下,使用

fuser -n tcp 9100

查看进程号,如果启动了就会有进程号;
<然后可以kill -9 进程号 关闭>

启动ES的命令

首先到bin目录下,然后执行:

./elasticsearch -d

启动kibana的命令

首先到bin目录下,然后执行:

nohup ./kibana > /dev/null 2>&1 &

ES设置密码

生产环境下,ES应该设置密码;方法如下:

  1. 找到conf目录,修改配置文件elasticsearch.yml,增加下方2行:
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
  1. 找到bin目录,运行命令:
./elasticsearch-setup-passwords interactive

然后控制台会有提示,需要设置6个密码。

  1. 重启ES。
  2. 访问时需要带上 -u 用户名:密码,如下:
curl localhost:9200 -u elastic:#123456@

kibana设置用户名密码

  1. 找到conf目录,修改配置文件kibana.yml,增加下方2行:
elasticsearch.username: "elastic"
elasticsearch.password: "#123456@"

密码是刚才在ES上设置的密码。

  1. 重启kibana
fuser -n tcp 9100
kill -9 xxxx
nohup ./kibana > /dev/null 2>&1 &

CURL操作ElasticSearch

以下假设索引名为test。

curl删除索引

curl -XDELETE -u elastic:#123456@ http://localhost:9200/test

这句删除名为test的索引。

curl创建索引

curl -u elastic:#123456@ -H 'Content-Type:application/json' -d '{
  "settings": {
    "index": {
     "number_of_shards": "1",
     "number_of_replicas": "1"
    }
  },
  "mappings": {
    "properties": {
       "userId": {"type":"long"},
       "time": {"type":"double"},
       "userName": {"type":"text","fields":{"keyword":{"type":"keyword","ignore_above":256}}}
       
     }
  }
}' -XPUT "http://localhost:9200/test"

这段代码创建了索引test。

curl查看索引相关语句

curl -u elastic:#123456@ http://localhost:9200/_cat/indices?v
curl -u elastic:#123456@ http://localhost:9200/_stats

curl -u elastic:#123456@ http://localhost:9200/_all/_mapping
curl -u elastic:#123456@ http://localhost:9200/test/_mapping

其中,查看索引映射用_mapping.

curl新增数据

curl -u elastic:#123456@ -H 'Content-Type:application/json' -d '{
  "userName": "abc",
  "userId": 123,
  "time": 5.5
}' -XPOST "http://localhost:9200/test/_doc/1"

这段代码向test索引中增加了一条数据,id为1.

curl查询数据

curl -u elastic:#123456@ -H 'Content-Type:application/json' -d '{
  "query": {
    "bool": {
       "must": [
           {
              "term": {
                   "_id": {
                       "value": "1"
                   }
               }
           }
       ]
     }
  }
}' -XGET "http://localhost:9200/test/_search"

这段代码从test索引中查询id为1的数据。

curl清空索引数据(保留索引结构)

curl -u elastic:#123456@ -H 'Content-Type:application/json' -d '{
  "query": {
    "match_all": {
     }
  }
}' -XPOST "http://localhost:9200/test/_delete_by_query"

这段代码删除test索引中的全部数据。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

追逐梦想永不停

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值