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应该设置密码;方法如下:
- 找到conf目录,修改配置文件elasticsearch.yml,增加下方2行:
xpack.security.enabled: true
xpack.security.transport.ssl.enabled: true
- 找到bin目录,运行命令:
./elasticsearch-setup-passwords interactive
然后控制台会有提示,需要设置6个密码。
- 重启ES。
- 访问时需要带上 -u 用户名:密码,如下:
curl localhost:9200 -u elastic:#123456@
kibana设置用户名密码
- 找到conf目录,修改配置文件kibana.yml,增加下方2行:
elasticsearch.username: "elastic"
elasticsearch.password: "#123456@"
密码是刚才在ES上设置的密码。
- 重启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索引中的全部数据。
|