elasticsearch索引查询及CRUD操作详解

当存储了大量的数据之后,你可能会有这样的需求,就是知道某个索引之下的相关数据有哪些。

这时候就可以使用索引的相关信息进行查询。

本文使用curl相关命令进行查看:

1、安装curl

Ubuntu:(已安装的朋友可以忽略此步骤)

$> sudo apt-get install curl

#检验是否安装成功,查看帮助
$> curl -h
Options: (H) means HTTP/HTTPS only, (F) means FTP only
     --anyauth       Pick "any" authentication method (H)
 -a, --append        Append to target file when uploading (F/SFTP)
     --basic         Use HTTP Basic Authentication (H)
     --cacert FILE   CA certificate to verify peer against (SSL)
     --capath DIR    CA directory to verify peer against (SSL)
 -E, --cert CERT[:PASSWD]  Client certificate file and password (SSL)
     --cert-status   Verify the status of the server certificate (SSL)
     --cert-type TYPE  Certificate file type (DER/PEM/ENG) (SSL)
     --ciphers LIST  SSL ciphers to use (SSL)
     --compressed    Request compressed response (using deflate or gzip)
 -K, --config FILE   Read config from FILE
     --connect-timeout SECONDS  Maximum time allowed for connection
 -C, --continue-at OFFSET  Resumed transfer OFFSET
 -b, --cookie STRING/FILE  Read cookies from STRING/FILE (H)
 -c, --cookie-jar FILE  Write cookies to FILE after operation (H)
     --create-dirs   Create necessary local directory hierarchy
     --crlf          Convert LF to CRLF in upload
     --crlfile FILE  Get a CRL list in PEM format from the given file
 -d, --data DATA     HTTP POST data (H)
     --data-raw DATA  HTTP POST data, '@' allowed (H)
     --data-ascii DATA  HTTP POST ASCII data (H)
     --data-binary DATA  HTTP POST binary data (H)
     --data-urlencode DATA  HTTP POST data url encoded (H)
     --delegation STRING  GSS-API delegation permission
     --digest        Use HTTP Digest Authentication (H)
     --disable-eprt  Inhibit using EPRT or LPRT (F)
     --disable-epsv  Inhibit using EPSV (F)
     --dns-servers   DNS server addrs to use: 1.1.1.1;2.2.2.2
     --dns-interface  Interface to use for DNS requests
     --dns-ipv4-addr  IPv4 address to use for DNS requests, dot notation
     --dns-ipv6-addr  IPv6 address to use for DNS requests, dot notation
    ......


2、查看es集群状态(健康状态)

$> curl 'localhost:9200/_cat/health?v'
epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1530753722 09:22:02  elasticsearch yellow          1         1     31  31    0    0       31             0                  -                 50.0%

green: 表示一切正常,

yellow: 表示所有的数据可用但是部分副本还没有分配

red: 表示部分数据因为某些原因不可用.

3、查看集群的节点列表

$> curl 'localhost:9200/_cat/nodes?v'
ip            heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
xxx.xxx.xx.xx           76          99   1    0.37    0.69     0.56 mdi       *      QDfLfz8


4、查看所有索引信息

$> curl 'localhost:9200/_cat/indices?v'
health status index               uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   test           QfbVSJvDSK-qCbauWoCAnQ   5   1       7819        13680     79.5mb         79.5mb
yellow open   test0          F0z34_i0TjCaPCjhfj48eg   5   1    6861313       938181      1.6gb          1.6gb
yellow open   test1          PUZAguRwSvKcPidvSxxHeg   5   1       6358           25       23mb           23mb
yellow open   test2          xqgdwTiURXqvltVH99MndQ   5   1         15            0     83.3kb         83.3kb
yellow open   test3          CYMmjlhqTKGR3TGsvooHjA   5   1     512865        82165    126.4gb        126.4gb
yellow open   .kibana        8XN555iTQXSxRaCoMFigog   1   1          7            0     41.3kb         41.3kb
yellow open   test4          RDg_0Lt7Q9mcgIHEsLSgZg   5   1       6358            0     22.9mb         22.9mb

5、创建索引

$> curl -XPUT 'localhost:9200/索引名称?pretty'


6、插入或修改数据

curl -XPUT 'localhost:9200/索引名称/type/id?pretty' -d '
  {
		"name": "John Doe"
  }'


7、获取数据

curl -XGET 'localhost:9200/索引名称/type/id?pretty'

8、删除数据

curl -XDELETE 'localhost:9200/索引名称?pretty'

9、更新字段

#更新(可新增字段)
curl -XPOST 'localhost:9200/索引名称/type/id/_update?pretty' -d '
  {
        "doc": { "name": "bboyHan", "age": 20 }
  }'

#通过脚本script更新
curl -XPOST 'localhost:9200/索引名称/type/id/_update?pretty' -d '
  {
      "script" : "ctx._source.age += 3"
  }'


其它还有批处理bulk、query-match等操作,具体还想了解的可以查询官方提供的API进行解读。

官方API地址(中文):https://www.elastic.co/guide/cn/elasticsearch/guide/current/index-doc.html


-------------------------------------------

有任何建议或问题,欢迎加微信一起学习交流,欢迎从事IT,热爱IT,喜欢深挖源代码的行业大牛加入,一起探讨。

个人微信号:bboyHan


猜你喜欢

转载自blog.csdn.net/han0373/article/details/80921613