ElasticSearch的常用API

1、在服务器上怎么查ES的信息

# 通过使用_cat可以查看支持的命令
### curl localhost:9200/_cat
eg: /_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 # verbose
# 每个命令都支持使用?v参数,来显示详细的信息:
# $ curl localhost:9200/_cat/master?v
id                     host      ip        node
QG6QrX32QSi8C3-xQmrSoA 127.0.0.1 127.0.0.1 Manslaughter # help
# 每个命令都支持使用help参数,来输出可以显示的列:
# $ curl localhost:9200/_cat/master?help
id   |   | node id
host | h | host name
ip   |   | ip address
node | n | node name # headers
# 通过h参数,可以指定输出的字段:**
# $ curl localhost:9200/_cat/master?v
id                     host      ip        node
QG6QrX32QSi8C3-xQmrSoA 127.0.0.1 127.0.0.1 Manslaughter # $ curl localhost:9200/_cat/master?h=host,ip,node
127.0.0.1 127.0.0.1 Manslaughter # 数字类型的格式化
# 很多的命令都支持返回可读性的大小数字,比如使用mb或者kb来表示。
# [es@localhost config]$ curl localhost:9200/_cat/indices
green open customer2 9im2_3hIT9-chDznVXwOtg 1 1 0 0 566b  283b
green open customer1 uaA_cmxYSU-76jT93-hs0w 1 1 1 0  7kb 3.5kb # bytes: 数值列还原为原始值. 如diskSize, 默认转为以kb/mb/gb表示, 打开后还原为原始值
# [es@localhost config]$ curl localhost:9200/_cat/indices?bytes=b
green open customer2 9im2_3hIT9-chDznVXwOtg 1 1 0 0  566  283
green open customer1 uaA_cmxYSU-76jT93-hs0w 1 1 1 0 7260 3630 ## shell
# 创建customer索引
curl -X PUT 'localhost:9200/customer?pretty'
eg:
[es@localhost config]$ curl -X PUT 'localhost:9200/customer?pretty'
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "customer"
} # 删除索引
curl -X DELETE localhost:9200/customer?pretty
eg:
[es@localhost config]$ curl -X DELETE localhost:9200/customer?pretty
{
"acknowledged" : true
} #获取全部索引【列出所有索引, 并展示索引基本信息】
curl localhost:9200/_cat/indices?v
eg:
[es@localhost config]$ curl localhost:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
green open customer1 uaA_cmxYSU-76jT93-hs0w 1 1 1 0 7kb 3.5kb
green open customer FTTKAgKKQAWpBI78aTUO9Q 1 1 1 0 15.7kb 7.8kb
--health 索引的健康状态
--status 索引状态信息open/close
--index 索引名
--uuid 索引的UUID
--pri 索引主分片数量
--rep 索引复制分片数量
--store.size 索引主分片 复制分片 总占用存储空间
--pri.store.size 索引总占用空间, 不计算复制分片 占用空间 #查看集群健康状态简短显示
curl localhost:9200/_cat/health?v
eg:
[es@localhost config]$ 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
1592809420 07:03:40 elasticsearch green 2 2 4 2 0 0 0 0 - 100.0% #查看集群健康状态详细显示
curl localhost:9200/_cluster/health?pretty
eg:
[es@localhost config]$ curl localhost:9200/_cluster/health?pretty
{
"cluster_name" : "elasticsearch",
"status" : "green",
"timed_out" : false,
"number_of_nodes" : 2,
"number_of_data_nodes" : 2,
"active_primary_shards" : 2,
"active_shards" : 4,
"relocating_shards" : 0,
"initializing_shards" : 0,
"unassigned_shards" : 0,
"delayed_unassigned_shards" : 0,
"number_of_pending_tasks" : 0,
"number_of_in_flight_fetch" : 0,
"task_max_waiting_in_queue_millis" : 0,
"active_shards_percent_as_number" : 100.0
} # 查看节点列表【列出所有node, 并展示node所在机器的运行状态信息.】
curl localhost:9200/_cat/nodes?v
eg:
[es@localhost config]$ curl localhost:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.21.131 17 94 13 0.40 0.32 0.17 dilm * localhost.localdomain
192.168.21.131 12 93 15 0.40 0.32 0.17 dilm - localhost.localdomain
--ip ip
--heap.percent 堆内存占用百分比
--ram.percent 内存占用百分比
--cpu CPU占用百分比
--master *表示节点是集群中的主节点、其他表示从节点
--name 节点名 #查看分片
curl localhost:9200/_cat/shards
eg:
[es@localhost config]$ curl localhost:9200/_cat/shards
customer1 0 p STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
customer1 0 r STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
customer 0 p STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
customer 0 r STARTED 1 3.5kb 192.168.21.131 localhost.localdomain
--prirep p表示该分片是主分片, r 表示该分片是复制分片 #各节点机器存储信息【列出所有node, 并展示所属机器的配置信息】
curl localhost:9200/_cat/allocation?v
[es@localhost config]$ curl localhost:9200/_cat/allocation?v
shards disk.indices disk.used disk.avail disk.total disk.percent host ip node
2 3.8kb 6.5gb 40.4gb 46.9gb 13 192.168.21.131 192.168.21.131 localhost.localdoma
2 3.8kb 6.5gb 40.4gb 46.9gb 13 192.168.21.131 192.168.21.131 localhost.localdoma
--shards 节点说承载的分片数
--disk.indices 索引占用的空间大小
--disk.used 节点所在机器已使用磁盘空间
--disk.avail 节点所在机器可用磁盘空间
--disk.total 节点所在机器总磁盘空间
--disk.percent 节点所在机器磁盘空间占用百分比
--host host
--ip 节点所属机器IP地址
--node 节点名 #获取所有的type
curl -X GET http://localhost:9200/_mapping?pretty
eg:
[es@localhost config]$ curl -X GET http://localhost:9200/_mapping?pretty
{
"customer" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
},
"customer1" : {
"mappings" : {
"properties" : {
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
# ```
### 集群
# 查看集群状态
curl localhost:9200/_cat/health?v # shell
#插入一条数据id为1的文档,如果已经存在该ID则替换该文档
curl -X PUT "localhost:9200/customer/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
eg:
[es@localhost config]$ curl -X PUT "localhost:9200/customer6/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
{
"_index" : "customer6",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
[es@localhost config]$ curl -X PUT "localhost:9200/customer6/_doc/1?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "John Doe"}'
{
"_index" : "customer6",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
} # 插入一个文档,不指定ID,ID自动生成
curl -X POST "localhost:9200/customer/_doc?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "Jane Doe"}'
eg:
[es@localhost config]$ curl -X POST "localhost:9200/customer8/_doc?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "Jane Doe"}'
{
"_index" : "customer8",
"_type" : "_doc",
"_id" : "7Wph23IB-KZb9yvuVPEj",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
[es@localhost config]$ curl -X POST "localhost:9200/customer8/_doc?pretty&pretty" -H 'Content-Type: application/json' -d'{"name": "Jane Doe"}'
{
"_index" : "customer8",
"_type" : "_doc",
"_id" : "7mph23IB-KZb9yvuv_Gx",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 2,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
} # 查询id为1的文档
curl -X GET localhost:9200/customer/_doc/1?pretty
eg:
[es@localhost config]$ curl -X GET localhost:9200/customer8/_doc/7mph23IB-KZb9yvuv_Gx?pretty
{
"_index" : "customer8",
"_type" : "_doc",
"_id" : "7mph23IB-KZb9yvuv_Gx",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Jane Doe"
}
}
[es@localhost config]$ curl -X GET localhost:9200/customer8/_doc/7Wph23IB-KZb9yvuVPEj?pretty
{
"_index" : "customer8",
"_type" : "_doc",
"_id" : "7Wph23IB-KZb9yvuVPEj",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Jane Doe"
}
} # 查询一个索引的所有文档
curl localhost:9200/customer/_search?pretty
eg:
[es@localhost config]$ curl localhost:9200/customer8/_search?pretty
{
"took" : 30,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "customer8",
"_type" : "_doc",
"_id" : "7Wph23IB-KZb9yvuVPEj",
"_score" : 1.0,
"_source" : {
"name" : "Jane Doe"
}
},
{
"_index" : "customer8",
"_type" : "_doc",
"_id" : "7mph23IB-KZb9yvuv_Gx",
"_score" : 1.0,
"_source" : {
"name" : "Jane Doe"
}
}
]
}
} # 条件查询
curl "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"
eg:
[es@localhost config]$ curl "localhost:9200/customer8/_search?q=*&pretty"
{
"took" : 12,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "customer8",
"_type" : "_doc",
"_id" : "7Wph23IB-KZb9yvuVPEj",
"_score" : 1.0,
"_source" : {
"name" : "Jane Doe"
}
},
{
"_index" : "customer8",
"_type" : "_doc",
"_id" : "7mph23IB-KZb9yvuv_Gx",
"_score" : 1.0,
"_source" : {
"name" : "Jane Doe"
}
}
]
}
} # 条件查询,_source查询的字段,size查询条数,sort排序,query查询条件
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match_all": {} },
"from":5,
"size":5,
"sort":{
"balance":{"order":"desc"}
},
"_source":["_id","name"]
}' # 模糊查询,match中跟要模糊查询的字段,模糊的值在value中用空格隔开,用match_phrase它返回地址中包含短语"mill lane"的所有帐户
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": { "match": {"address":"mill lane"} },
"from":5,
"size":5,
"sort":{
"balance":{"order":"desc"}
},
"_source":["_id","name"]
}' # 该bool must子句指定了所有必须为true的查询才能将文档视为匹配项。相反,此示例组成两个match查询,并返回地址中包含“ mill”或“ lane”的所有帐户:
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}' # 该bool should子句指定了一个查询列表,对于将文档视为匹配项,其中任一查询都必须为true。本示例组成两个match查询,并返回地址中既不包含“ mill”也不包含“ lane”的所有帐户
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}' # 该bool must_not子句指定了一个查询列表,对于被视为匹配的文档,没有一个查询列表必须为真。我们可以在查询中同时组合must,should和must_not子句bool。此外,我们可以bool在任何这些bool子句中组成查询,以模仿任何复杂的多级布尔逻辑
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}' # 我们可以在查询中同时组合must,should和must_not子句bool。此外,我们可以bool在任何这些bool子句中组成查询,以模仿任何复杂的多级布尔逻辑。
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}' # filter子句,这些子句允许我们使用查询来限制将要与其他子句匹配的文档,而无需更改分数的计算方式。作为示例,让我们介绍一下rangequery,它允许我们按一定范围的值过滤文档。通常用于数字或日期过滤
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}' # 执行聚合,按状态对所有帐户进行分组,然后返回按计数递减排序的前10个(默认)状态(也是默认)
curl -X GET "localhost:9200/bank/_search?pretty" -H 'Content-Type: application/json' -d'
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword"
}
}
}
}' # 更新ID为1的文档
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty&pretty" -H 'Content-Type: application/json' -d'{"doc":{"name": "Jane Doe","age":"18"}}' # 批量更新文档
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }' # 删除ID为2的文档
curl -X DELETE localhsot:9200/customer/_doc/2?pretty # 批量删除和更新
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty&pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}' # 批量将accounts.json文件中的数据导入到bank索引中
curl -H "Content-Type: application/json" -XPOST "localhost:9200/bank/_doc/_bulk?pretty&refresh" --data-binary "@accounts.json" curl -X POST "localhost:9200/twitter/_update_by_query?conflicts=proceed&pretty" -H 'Content-Type: application/json' -d
{
"query": {
"term": {
"user": "kimchy"
}
}
}

ElasticSearch的常用API的更多相关文章

  1. Elasticsearch搜索常用API(利用Kibana来操作)

    上面我们已经介绍了Elasticsearch的一些基本操作,这篇文章属于进阶篇,我们一起来学习. 前面我们创建了sdb和user文档,现在我们来看如何查询user中所有的文档呢? GET /sdb/u ...

  2. elasticsearch中常用的API

    elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...

  3. Elasticsearch 常用API

    1.   Elasticsearch 常用API 1.1.数据输入与输出 1.1.1.Elasticsearch 文档   #在 Elasticsearch 中,术语 文档 有着特定的含义.它是指最顶 ...

  4. Elasticsearch常用API

    Note:以下API基于ES 5x,6x/7x可能略有不同,具体查看官方文档 ES常用API # 查看集群node curl "http://localhost:9200/_cat/node ...

  5. ELK常用API使用方法

    以下ELK系列文章参考自http://www.tianyiqingci.com/ 总目录: Monitor API ElasticSearch聚合分析API Elasticsearch信息检索API ...

  6. python实现elasticsearch操作-CRUD API

    python操作elasticsearch常用API 目录 目录 python操作elasticsearch常用API1.基础2.常见增删改操作创建更新删除3.查询操作查询拓展类实现es的CRUD操作 ...

  7. Elasticsearch-02-入门:集群、节点、分片、索引及常用API

    2. 基础入门 2.1 重要概念 2.1.1 集群和节点 1)cluster Elasticsearch集群是由一个或多个节点组成,通过其集群名称来进行唯一标识.节点在搜索到集群之后,通过判断自身的 ...

  8. html5 canvas常用api总结(一)

    1.监听浏览器加载事件. window.addEventListener("load",eventWindowLoaded,false); load事件在html页面加载结束时发生 ...

  9. compass General 常用api学习[Sass和compass学习笔记]

    compass 中一些常用api 包括一些浏览器hack @import "compass/utilities/general" Clearfix Clearfix 是用来清除浮动 ...

  10. elasticsearch中的API

    elasticsearch中的API es中的API按照大类分为下面几种: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作 查看A ...

随机推荐

  1. JavaScript 调用Bomb后端云

                                                     用简单的代码 展示代码的魅力 Bmob 是后端云 全方位一体化的后端服务平台 提供可靠的 Server ...

  2. Jetbrains Rider 2020.1.4 安装教程

    话说Visual Studio 是世界第亿的IDE没人反对吧!!! 用惯了VS之后就不得不吐槽他的运行效率,五步一停十步一卡的岁月也不得不熬过来了,然而!!! 看到了视频里别人使用了Rider,集成了 ...

  3. bzoj 3309

    奇怪的莫比乌斯反演... 题意:定义$f(n)$表示将$n$质因数分解后质因子的最高幂次,求$\sum_{i=1}^{a}\sum_{j=1}^{b}f(gcd(i,j))$ 首先肯定是反演嘛... ...

  4. Docker之基本原理介绍

    Docker 环境搭建请移步:https://i.cnblogs.com/posts/edit;postId=14090026 First:docker能做什么? 传统的环境部署: 1.环境和项目分开 ...

  5. 【APT】Bitter APT组织针对巴基斯坦航空综合部门攻击活动分析

    前言 蔓灵花(Bitter)是一个被广泛认为来自印度的APT组织,该组织长期针对我国及巴基斯坦的政府.军工.电力.核等部门发动网络攻击,窃取敏感数据,具有较强的政治背景.本次分享一个蔓灵花组织针对巴基 ...

  6. 2.21(html)

  7. https://计算机二级

    计算机二级; 计算机二级包括二级C,二级C++,二级VB,二级VFP,二级Access,二级Java,二级office.其需要学习的内容如下: 1.二级C:DOS命令,计算机组成原理,多媒体.数据库操 ...

  8. 类内函数的override问题-方法

    Question: have a base class with a virtual function: class Base { public: virtual void Function(); } ...

  9. datetime 获取当前时间的各种格式(转)

    我们可以通过使用DataTime这个类来获取当前的时间.通过调用类中的各种方法我们可以获取不同的时间:如:日期(2008-09-04).时间(12:12:12).日期+时间(2008-09-04 12 ...

  10. 4组-Beta冲刺-4/5

    一.基本情况 队名:摸鲨鱼小队 组长博客:https://www.cnblogs.com/smallgrape/p/15604878.html github链接:https://github.com/ ...