掘金 后端 ( ) • 2022-08-07 09:22

携手创作,共同成长!这是我参与「掘金日新计划 · 8 月更文挑战」的第8天,点击查看活动详情

elasticsearch交互方式

1.elasticsearch交互方式

curl命令:

​最繁琐

​最复杂

​最容易出错

​不需要安装任何软件,值需要有curl命令

es-head插件

​查看数据方便

​操作相对容易

​需要node环境

kibana

​查看数据以及报表格式丰富

​操作很简单

​需要java环境和安装配置kibana

1.1.查看es基本信息

[root@elastic ~]# curl 192.168.81.210:9200/_cat
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates

1.2.查看es分片信息

下面的输出可以看出是分了5个片,一个主分片一个副本分片

[root@elastic ~]# curl 192.168.81.210:9200/_cat/shards
testinfo 2 p STARTED    0 261b 192.168.81.240 node-1
testinfo 2 r UNASSIGNED                       
testinfo 1 p STARTED    0 261b 192.168.81.240 node-1
testinfo 1 r UNASSIGNED                       
testinfo 3 p STARTED    0 261b 192.168.81.240 node-1
testinfo 3 r UNASSIGNED                       
testinfo 4 p STARTED    0 261b 192.168.81.240 node-1
testinfo 4 r UNASSIGNED                       
testinfo 0 p STARTED    0 261b 192.168.81.240 node-1
testinfo 0 r UNASSIGNED  

1.3.使用curl命令创建索引

开启127.0.0.1访问地址 [root@elastic ~]# vim /etc/elasticsearch/elasticsearch.yml network.host: 192.168.81.240,127.0.0.1 [root@elastic ~]# systemctl restart elasticsearch

注意创建索引时,curl命令的参数一定要保持顺序

命令格式:curl -XPUT 'es地址:prot/索引名?pretty'

1.创建索引
[root@elastic ~]# curl -XPUT '192.168.81.210:9200/testinfo?pretty'
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "testinfo"
}

2.查看索引
[root@elastic ~]# curl 192.168.81.210:9200/_cat/indices
yellow open testinfo iucttBn4SsWpFQA24V8pYg 5 1 0 0 1.2kb 1.2kb

1.4.使用curl向索引插入数据

命令格式:

​curl -XPUT 'es地址:port/索引/类型/id?pretty' -H 'COntent-Type: application/json' -d'{值}'

[root@elastic ~]# curl -XPUT '192.168.81.210:9200/testinfo/user/1?pretty' -H 'COntent-Type: application/json' -d'
{
"first_name" : "jiang",
"last_name" : "xiaolong",
"age" : 99,
"about" : "I like linux", "interests": [ "sports", "music" ]
}
 '
{
  "_index" : "testinfo",
  "_type" : "user",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
在创建一个id为2的文本
[root@elastic ~]# curl -XPUT '192.168.81.210:9200/testinfo/user/2?pretty' -H 'COntent-Type: application/json' -d'
{
"first_name" : "jiang",
"last_name" : "xl",
"age" : 77,
"about" : "I like linux", "interests": [ "sports", "music" ]
}'

刚刚创建的两个数据都是指定id的我们来写一个不指定id的

格式:

​curl -XPOST 'es地址:port/索引/类型?pretty/' -H 'COntent-Type: application/json' -d'{值}'

​curl -XPOST 'es地址:port/索引/类型/' -H 'COntent-Type: application/json' -d'{值}'

​这个输出是一行不如第一种

[root@elaticsearch ~]# curl -X POST '192.168.81.210:9200/testinfo/user?pretty' -H 'COntent-Type: application/json' -d'
{
"first_name" : "jiang",
"last_name" : "xl",
"age" : 77,
"about" : "I like linux", "interests": [ "sports", "music" ]
}'
{
  "_index" : "testinfo",
  "_type" : "user",
  "_id" : "p-58inYBlNQwpkFxBxiY",#随机生成的
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}


再创建一个
[root@elastic ~]# curl -XPOST '192.168.81.210:9200/testinfo/user?pretty' -H 'COntent-Type: application/json' -d'
{
"first_name" : "zhang",
"last_name" : "jia",
"age" : 77,
"about" : "I like linux", "interests": [ "sports", "music" ]
}'