ElasticSearch的常用API
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的更多相关文章
- Elasticsearch搜索常用API(利用Kibana来操作)
上面我们已经介绍了Elasticsearch的一些基本操作,这篇文章属于进阶篇,我们一起来学习. 前面我们创建了sdb和user文档,现在我们来看如何查询user中所有的文档呢? GET /sdb/u ...
- elasticsearch中常用的API
elasticsearch中常用的API分类如下: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作,查看索引信息等 查看API: ...
- Elasticsearch 常用API
1. Elasticsearch 常用API 1.1.数据输入与输出 1.1.1.Elasticsearch 文档 #在 Elasticsearch 中,术语 文档 有着特定的含义.它是指最顶 ...
- Elasticsearch常用API
Note:以下API基于ES 5x,6x/7x可能略有不同,具体查看官方文档 ES常用API # 查看集群node curl "http://localhost:9200/_cat/node ...
- ELK常用API使用方法
以下ELK系列文章参考自http://www.tianyiqingci.com/ 总目录: Monitor API ElasticSearch聚合分析API Elasticsearch信息检索API ...
- python实现elasticsearch操作-CRUD API
python操作elasticsearch常用API 目录 目录 python操作elasticsearch常用API1.基础2.常见增删改操作创建更新删除3.查询操作查询拓展类实现es的CRUD操作 ...
- Elasticsearch-02-入门:集群、节点、分片、索引及常用API
2. 基础入门 2.1 重要概念 2.1.1 集群和节点 1)cluster Elasticsearch集群是由一个或多个节点组成,通过其集群名称来进行唯一标识.节点在搜索到集群之后,通过判断自身的 ...
- html5 canvas常用api总结(一)
1.监听浏览器加载事件. window.addEventListener("load",eventWindowLoaded,false); load事件在html页面加载结束时发生 ...
- compass General 常用api学习[Sass和compass学习笔记]
compass 中一些常用api 包括一些浏览器hack @import "compass/utilities/general" Clearfix Clearfix 是用来清除浮动 ...
- elasticsearch中的API
elasticsearch中的API es中的API按照大类分为下面几种: 文档API: 提供对文档的增删改查操作 搜索API: 提供对文档进行某个字段的查询 索引API: 提供对索引进行操作 查看A ...
随机推荐
- Excel Vlookup用法和常见报错#REF! #Value!
VLOOKUP(E2,$A$2:$C$5,2,FALSE) E2 为选中查找的条件 $A$2:$C$5 1为需要查找的区域,这个区域一般是固定的,所以要加上$符号 2这个区域可以在前面加上SHEET2 ...
- 目前为止最完善专业的微信小程序商城
购买商业版(368元): http://market.zhenzikj.com/detail/82.html帮助文档:https://www.kancloud.cn/crmeb/crmeb/66242 ...
- 20202411 2020-2021-2 《Python程序设计》实验二报告
20202411 2020-2021-2 <Python程序设计>实验二报告 课程:<Python程序设计> 班级: 2024 姓名: 陈书桓 学号:20202411 实验教师 ...
- 前端vue框架上手记录
---恢复内容开始--- Vue CLI 是一个基于 Vue.js 进行快速开发的完整系统,提供: 通过 @vue/cli 搭建交互式的项目脚手架. 通过 @vue/cli + @vue/cli-se ...
- esxi的一些命令
今天本想在vcenter上建个分布式交换机,没想到在迁移主机端口时,由于主机只连了一个物理网卡,导致在迁移到分布式交换机而失去连接,几近周折,从网上找了几篇不错的文章,与众同乐 假如一台ESX主机先通 ...
- zynq 中断
#include "stdio.h"#include "xparameters.h"#include "xgpiops.h"#include ...
- nuxt.js的导航守卫
一.使用router.js 重构项目时还行使用原来的router.js也是可以的,需要下载插件惊醒配置,这时候vue-cli中怎么用,nuxt中就怎么使用导航守卫,几乎一样 二.使用nuxt.js 1 ...
- PageHeplper使用
1.引入POM 1 <dependency> 2 <groupId>com.github.pagehelper</groupId> 3 <artifactId ...
- 解决Tomcat 启动,http://localhost:8080无法访问 问题
注意:1:tomact能正常启动 2 : 通过startup启动后可以正常访问8080界面: 3: 通过eclipse启动后无法正常访问8080界面: Tomcat能在eclipse里面能正常启动,但 ...
- Android 自定义SeekBar (一)
一.前言 巩固自定义view基础用,本次尝试构建一个拖动条组件.代码参考于 https://github.com/woxingxiao/BubbleSeekBar ,精简其中高度可重用的部分,仅保留基 ...