Elasticsearch(ES)常用命令整理


前言

在前几篇文章大家主要进行Elasticsearch的入门学习,了解了它的基本概念和工作原理,也学习到Elasticsearch集群的角色和主要职责。接下来,本文着重介绍了Elasticsearch的常用基础命令。


一、基本命令

1.1 获取所有_cat命令

命令:curl -XGET localhost:9200/_cat

[elasticsearch@test-es7-master-0 ~]$ curl -XGET localhost: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
...

以上的命令中,你也可以后面加一个v,让输出内容表格显示表头

1.2 获取es集群服务健康状态

命令:curl -X GET “localhost:9200/_cat/health?v”

1.3 查看es节点信息

命令:curl -XGET ‘localhost:9200/_cat/nodes?v’

1.4 查看es指定节点信息

命令:curl -XGET ‘localhost:9200/_nodes/nodeName?pretty=true’

二、索引操作

1. 查看ES中所有的索引

命令:curl -X GET “ip地址:9200/_cat/indices?v”
示例:curl -X GET localhost:9200/_cat/indices?v
在这里插入图片描述

2. 新建索引

命令:curl -X PUT ‘localhost:9200/test’
示例:新建一个名字为test的 Index。创建后返回下面的json对象。“acknowledged”:true表示创建成功

curl -X PUT localhost:9200/test         
{
 "acknowledged":true,
 "shards_acknowledged":true,
 "index":"test-zp"
 }

3. 删除索引

命令:curl -X DELETE ‘localhost:9200/test’
示例:删除名为test的Index。“acknowledged”:true表示删除成功

curl -X DELETE localhost:9200/test         
{
 "acknowledged":true
 }

4. 查看指定索引信息

命令:curl -XGET “http://localhost:9200/test?pretty” 注意:test是索引名

4. 查看索引的统计信息

命令:curl -XGET “http://localhost:9200/test/_stats?pretty” 注意:test是索引名

三、文档操作 *

3.1 查询索引中的全部文档

命令:curl -X GET localhost:9200/index_name/_search?pretty
示例:curl -XGET localhost:9200/1021car_10061v1/_search?pretty 注意: ?pertty 表示让数据格式化,更好的展示
如图:显示指定索引下文档的信息
在这里插入图片描述

3.2 根据条件查询索引中的文档

单一条件搜索
1、搜索品牌是大众的汽车
命令:curl -H “Content-Type: application/json” -XPOST ‘http://localhost:9200/1021car_10061v1/_search?pretty’ -d ‘{“query”: { “match”: { “brand”: “大众” } }}’
多条件搜索
1、搜索品牌是大众,并且车型SUV的汽车(&&使用 must )
命令:curl -H “Content-Type: application/json” -XPOST ‘http://localhost:9200/1021car_10061v1/_search?pretty’ -d ‘{“query”: {“bool”: {“must”: [{ “match”: { “brand”: “大众” } },{ “match”: { “body”: “SUV”} }]}}}’
2、搜索品牌是大众或者奥迪的汽车(|| 使用 should )
命令:curl -H “Content-Type: application/json” -XPOST ‘http://localhost:9200/1021car_10061v1/_search?pretty’ -d ‘{“query”: {“bool”: {“should”: [{ “match”: { “brand”: “大众” } },{ “match”: { “brand”: “奥迪”} }]}}}’
3、搜索品牌是大众但车型不是SUV的汽车
命令:curl -H “Content-Type: application/json” -XPOST ‘http://localhost:9200/1021car_10061v1/_search?pretty’ -d ‘{“query”: {“bool”: { “must”: [{ “match”: { “brand”: “大众” } }],“must_not”: [{ “match”: { “body”: “SUV” } }]}}}’
4、统计品牌是大众的汽车数量有多少种
命令:curl -H “Content-Type: application/json” -XPOST ‘http://localhost:9200/1021car_10061v1/_count?pretty’ -d ‘{“query”: { “match”: { “brand”: “大众” } }}’

  • 9
    点赞
  • 65
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值