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 ...
随机推荐
- STM32 GPIO配置(寄存器)生成工具
在写程序的时候需要用寄存器配置GPIO方向.模式,每一个都需要去计算 感觉相当麻烦,所以写了一个用来计算的小工具 链接:https://pan.baidu.com/s/1PEn0Q0IiA5mJJbs ...
- 微信小程序云函数中管理短信验证码的完整SDK工具
微信小程序云开发管理短信验证码并不是一件容易的事情,它不像其他web服务器开发中可以将验证码存放到session或者cookie中,你只能将其存到云开发的数据库中,实现起来非常困难.一个简单的需要短信 ...
- python编程中的if __name__ == 'main': 的作用
python的文件有两种使用的方法,第一是直接作为脚本执行,第二是import到其他的python脚本中被调用(模块重用)执行. 因此if __name__ == 'main': 的作用就是控制这两种 ...
- 在SQLServer中将数据从高版本导入低版本的方法
一般的软件都是向下兼容的,高版本通常都是可以兼容低版本.但是如果想将高版本数据库中的数据导入到低版本中,直接采用常规的备份还原或是分离附加操作就会因为结构不同而报错. 要想实现数据从高版本到低版本,除 ...
- AX2012 使用HTML自定义popup内样式
在Class Box下新增方法如下: public client static DialogButton yesNoHTML( str _text, DialogButton _defaultButt ...
- webpack1.x 打包文件过大优化
1. 图片单独打包 module: { loaders: [ { test: /\.(jpe?g|png|gif|svg)$/, loader: 'url?limit=8192&name=./ ...
- 【Android异常】关于静态注册BroadcastReceiver接收不到系统广播的问题
如果你静态注册的广播无法接收到消息,请先检查下:你的安卓版本是不是8.0+ 前言Google官方声明:Beginning with Android 8.0 (API level 26), the sy ...
- 1402:Vigenère密码
[题目描述] 6世纪法国外交家Blaise de Vigenère设计了一种多表密码加密算法--Vigenère密码.Vigenère密码的加密解密算法简单易用,且破译难度比较高,曾在美国南北战争中为 ...
- Linux系列---【设置ssh连接超时时间】
设置ssh连接超时时间 shell工具总是不断的超时,上个厕所就断开了,很不方便,这里根据自己习惯设置一下. echo "export TMOUT=600" >> /e ...
- CentOS7 yum方式安装mysql 5.7
1.检查服务器上有没有安装mysqlyum安装方式:yum list installed mysql*yum卸载 yum remove 已安装的包卸载完安装包后,删除安装文件rm -rf /var/l ...