Elasticsearch交互方式
2020-06-21 本文已影响0人
南南宫问天
交互方式一:
curl命令
最繁琐 最复杂 最容易出错 不需要安装任何软件,只需要有curl命令
[root@db01 ~]# curl 172.16.210.53:9200/_cat/nodes
172.16.210.53 16 96 0 0.00 0.01 0.05 mdi * node-1
es-head插件
查看数据方便 操作相对容易 需要node环境
在谷歌商店搜索ElasticSearch Head
![](https://img.haomeiwen.com/i21497969/c9a0ebacef83db45.png)
再点击添加至Chrome
![](https://img.haomeiwen.com/i21497969/0ab5d95db21d51de.png)
![](https://img.haomeiwen.com/i21497969/589a0a5a671f6158.png)
输入要查看的es主机地址和端口再连接,就可以看见es的主机了
![](https://img.haomeiwen.com/i21497969/9d5d744fbb8fed07.png)
![](https://img.haomeiwen.com/i21497969/febeab725993adf0.png)
kibana
查看数据以及报表格式丰富 操作很简单 需要java环境和配置kibana
Elasticsearch的增删改查
[root@db01 ~]# curl -XPUT 172.16.210.53:9200/vipinfo?pretty ##添加一条vipinfo的索引 ?pretty以json格式返回信息
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "vipinfo"
}
回到web界面的es-head插件查看
![](https://img.haomeiwen.com/i21497969/73cf49bf968df7b5.png)
插入数据
###使用下面这条命令可插入一行数据
curl -XPUT '172.16.210.53:9200/vipinfo/user/1?pretty' -H 'Content-Type: application/json' -d'
{
"fisrst_name" : "John",
"last_name" : "Smith",
"aget" : 25,
"about" : "I love to go rock climbing", "interests": [ "sports", "music"]
}
'
![](https://img.haomeiwen.com/i21497969/b3e72a92d051ae65.png)
此时回到web界面,可以查看插入的数据了
![](https://img.haomeiwen.com/i21497969/a901fe1719357b58.png)
使用下面命令再插入两行数据
curl -XPUT '172.16.210.53:9200/vipinfo/user/2?pretty' -H 'Content-Type: application/json' -d'
{
"fisrst_name" : "Jane",
"last_name" : "Smith",
"aget" : 32,
"about" : "I like to collect rock albums", "interests": [ "music"]
}
'
curl -XPUT '172.16.210.53:9200/vipinfo/user/3?pretty' -H 'Content-Type: application/json' -d'
{
"fisrst_name" : "DOUGLAS",
"last_name" : "Fir",
"aget" : 35,
"about" : "I like to build cabinets", "interests": [ "forestry"]
}
'
回到web界面查看
![](https://img.haomeiwen.com/i21497969/f011ebeddf8db6de.png)
搜索数据
![](https://img.haomeiwen.com/i21497969/b4e65ba0af404203.png)
再次插入一条数据,这次插入的这个数据,事先没有创建索引,看能否插入
curl -XPUT '172.16.210.53:9200/xgx/18jw5/1?pretty' -H 'Content-Type: application/json' -d'
{
"boy" : 20,
"teacher" : "lifen",
"girl" : 5
}'
![](https://img.haomeiwen.com/i21497969/dc067cf0889b4570.png)
![](https://img.haomeiwen.com/i21497969/ad67e6bb7555270b.png)
可以发现是能插入的,得出结论Elasticsearch插入数据不一定要有这个索引存在,插入数据的时候,这个索引不存在会给你自动创建
查找数据
[root@db01 ~]# curl -XGET 172.16.210.53:9200/vipinfo/_search?pretty ##可以查看vininfo索引里的所有数据
[root@db01 ~]# curl -XGET 172.16.210.53:9200/vipinfo/user/1?pretty ##查找id为1的数据
[root@db01 ~]# curl -XGET '172.16.210.53:9200/vipinfo/user/_search?q=last_name:Smith&pretty' ##条件查找 查找last_name等于Smith的数据
也可以使用es-head插件查找
单条件
![](https://img.haomeiwen.com/i21497969/0bdd1e1afd792d2b.png)
多条件
![](https://img.haomeiwen.com/i21497969/dca1332ce92997b8.png)
也可以使用复合查询
![](https://img.haomeiwen.com/i21497969/73e5f365ffbe2134.png)
删除数据,就只需要把PUT
改成DELETE
![](https://img.haomeiwen.com/i21497969/e5c6c2124da36050.png)
再次查看数据,就发现id为1的数据被删除了
![](https://img.haomeiwen.com/i21497969/48e37c4b16af800f.png)