美文网首页
Elasticsearch交互方式

Elasticsearch交互方式

作者: 南南宫问天 | 来源:发表于2020-06-21 16:57 被阅读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

image.png

再点击添加至Chrome

image.png 再点击右上角的放大镜图标 image.png

输入要查看的es主机地址和端口再连接,就可以看见es的主机了


image.png
image.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插件查看

image.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"]
}
'  
image.png

此时回到web界面,可以查看插入的数据了


image.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界面查看

image.png

搜索数据


image.png

再次插入一条数据,这次插入的这个数据,事先没有创建索引,看能否插入

curl -XPUT '172.16.210.53:9200/xgx/18jw5/1?pretty' -H 'Content-Type: application/json' -d'
{ 
    "boy" : 20,
    "teacher" : "lifen",
    "girl" : 5
 }'
image.png image.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插件查找

单条件

image.png

多条件

image.png

也可以使用复合查询


image.png

删除数据,就只需要把PUT改成DELETE

image.png

再次查看数据,就发现id为1的数据被删除了


image.png

相关文章

网友评论

      本文标题:Elasticsearch交互方式

      本文链接:https://www.haomeiwen.com/subject/dgsaxktx.html