Elasticsearch 之 REST API

30 篇文章 0 订阅

Elasticsearch提供了一个非常全面和强大的REST API,可以使用Linux curl命令发起一个HTTP请求与集群进行交互,也可以使用任何允许进行HTTP/REST调用的工具来发起请求(比如Postman;如果是GET请求也可以直接在浏览器中访问)

一个基于HTTP协议的curl请求的基本格式如下:

curl -X <VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
名称说明
VERBHTTP请求方法,取值为GET、POST、PUT、HEAD或DELETE
PROTOCOL请求协议,取值为http或https
HOSTElasticsearch集群任意一个节点的主机名
PORTElasticsearch HTTP服务端口号,默认为9200
PATH请求路径,也成为API端点
QUERY_STRING查询字符串
BODYJSON格式的请求体

集群状态API

了解当前Elasticsearch集群的健康情况

curl -X GET 'centos01:9200/_cat/health?v'

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

返回字段原文含义
epochseconds since 1970-01-01 00:00:00自标准时间(1970-01-01 00:00:00)以来的秒数
timestamptime in HH:MM:SS时分秒,utc时区
clustercluster name集群名称
statushealth status集群状态
green:所有主分片和副本分片都正常运行
yellow:所有主分片都正常运行,但不是所有副本分片都正常运行
red:存在至少一个主分片没能正常运行
node.totaltotal number of nodes节点总数
node.datanumber of nodes that can store data数据节点总数
shardstotal number of shards分片总数
prinumber of primary shards主分片总数
relonumber of relocating nodes复制节点总数
initnumber of initializing nodes初始化节点总数
unassignnumber of unassigned shards未分配分片总数
pending_tasksnumber of pending tasks待定任务总数
max_task_wait_timewait time of longest task pending等待最长任务的等待时间
active_shards_percentactive number of shards in percent活动分片百分比

我这里status为yellow,unassign等于shards,说明我的集群所有的分片都没有分配…这是因为我的磁盘内存不够了: Elasticsearch的分片默认情况下受控于Elasticsearch集群节点的数据盘磁盘空间,有4个参数进行控制。默认情况下,当数据盘使用超过85%,Elasticsearch不会将分片分配给使用磁盘超过85%的节点。

这四个控制参数分别是:

cluster.routing.allocation.disk.threshold_enabled
默认为true。设置为false禁用磁盘分配决策程序。

cluster.routing.allocation.disk.watermark.low
控制磁盘使用的低水位线。它默认为85%,这意味着Elasticsearch不会将分片分配给使用磁盘超过85%的节点。它也可以设置为绝对字节值(如500mb),以防止Elasticsearch在小于指定的可用空间量时分配分片。此设置不会影响新创建的索引的主分片,或者特别是之前从未分配过的任何分片。

cluster.routing.allocation.disk.watermark.high
控制高水印。它默认为90%,意味着Elasticsearch将尝试从磁盘使用率超过90%的节点重新定位分片。它也可以设置为绝对字节值(类似于低水印),以便在节点小于指定的可用空间量时将其从节点重新定位。此设置会影响所有分片的分配,无论先前是否分配。

cluster.routing.allocation.disk.watermark.flood_stage
控制洪水阶段水印。它默认为95%,这意味着Elasticsearch index.blocks.read_only_allow_delete对每个索引强制执行只读索引块(),该索引在至少有一个磁盘超过泛洪阶段的节点上分配了一个或多个分片。这是防止节点耗尽磁盘空间的最后手段。一旦有足够的可用磁盘空间允许索引操作继续,就必须手动释放索引块。

而我这里已经达到了90%,所以Elasticsearch不会给我分配分片:
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

这也是我把Kibana装在centos03上而不是centos01上的原因…

查看Elasticsearch集群节点信息

curl -X GET 'centos01:9200/_cat/nodes?v'

在这里插入图片描述

返回字段原文含义
idunique node idip
heap.percentused heap堆内存占用百分比
ram.percentused machine memory ratio内存占用百分比
cpurecent cpuCPU占用百分比
load_1m1m load avg1分钟的系统负载
load_5m5m load avg5分钟的系统负载
load_15m15m load avg15分钟的系统负载
node.rolem:master eligible node, d:data node, i:ingest node, -:coordinating node onlynode节点的角色
master*:current master是否是master节点
namenode name节点名称

索引API

创建索引

创建一个名为customer的索引:

curl -X PUT 'centos01:9200/customer?pretty'

在这里插入图片描述

在创建索引时添加设置,例如设置分片数量、副本数量:(默认都是1)

curl -H "Content-Type: application/json" -X PUT 'centos01:9200/customer?pretty' -d '{"settings":{"number_of_shards":3,  "number_of_replicas":2}}'

在这里插入图片描述

查询索引

curl -X GET 'centos01:9200/customer?pretty' 

在这里插入图片描述

查询全部索引:

curl -X GET 'centos01:9200/_all?pretty' 
curl -X GET 'centos01:9200/*?pretty' 

打开和关闭索引

关闭索引:

curl -X POST 'centos01:9200/customer/_close?pretty' 

在这里插入图片描述

打开索引:

curl -X POST 'centos01:9200/customer/_open?pretty' 

在这里插入图片描述

打开/关闭索引:

curl -X POST 'centos01:9200/_all/_open?pretty' 
curl -X POST 'centos01:9200/*/_open?pretty' 

curl -X POST 'centos01:9200/_all/_close?pretty' 
curl -X POST 'centos01:9200/*/_close?pretty' 

删除索引

curl -X DELETE 'centos01:9200/customer?pretty' 

在这里插入图片描述

文档API

添加文档

在索引customer中添加一个name为Zhang San的文档,该文档的id为1:

curl -H "Content-Type: application/json" -X PUT 'centos01:9200/customer/_doc/1?pretty' -d '{"name":"Zhang San"}'

在这里插入图片描述

查询文档

查询索引customer中id为1的文档:

curl -X GET 'centos01:9200/customer/_doc/1?pretty' 

在这里插入图片描述

只查询索引customer中id为1的文档的name和age字段:

curl -HEAD 'centos01:9200/customer/_doc/1?pretty&_source=name,age' 

在这里插入图片描述

只要_source的数据:

curl -X GET 'centos01:9200/customer/_doc/1/_source?pretty' 

在这里插入图片描述

更新文档

更新customer中id为1的文档的name字段为Li Si:

curl -H "Content-Type: application/json" -X PUT 'centos01:9200/customer/_doc/1?pretty' -d '{"name":"Li Si"}'

在这里插入图片描述

一次性更新整个文档例如将customer中id为1的文档更新为{“name”:“Li Si”,“age”:20}:

curl -H "Content-Type: application/json" -X POST 'centos01:9200/customer/_doc/1/_update?pretty' -d '{"doc":{"name":"Li Si","age":20}}'

在这里插入图片描述

给age字段 +5 :

curl -H "Content-Type: application/json" -X POST 'centos01:9200/customer/_doc/1/_update?pretty' -d '{"script":"ctx._source.age+=5"}'

在这里插入图片描述

删除文档

删除索引customer中id为1的文档:

curl -H "Content-Type: application/json" -X DELETE 'centos01:9200/customer/_doc/1?pretty' 

在这里插入图片描述

批处理

将id为1、name为zhangsan和id为2、name为lisi的两个文档添加到索引customer中:

curl -H "Content-Type: application/json" -X POST 'centos01:9200/customer/_doc/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name":"zhangsan"}
{"index":{"_id":"2"}}
{"name":"lisi"}
' 

在这里插入图片描述

更新id为1的文档,删除id为2的文档:

curl -H "Content-Type: application/json" -X POST 'centos01:9200/customer/_doc/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc":{"name":"wangwu"}}
{"delete":{"_id":"2"}}
'

在这里插入图片描述
在这里插入图片描述

搜索API

首先向索引customer中添加两条数据:

curl -H "Content-Type: application/json" -X POST 'centos01:9200/customer/_doc/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name":"zhangsan","age":20}
{"index":{"_id":"2"}}
{"name":"lisi","age":22}
'

在这里插入图片描述

搜索customer索引中的所有文档并将结果按照age字段降序排列:

URL方式

curl -X GET 'centos01:9200/customer/_search?q=*&sort=age:desc&pretty'

在这里插入图片描述

body体方式

curl -H "Content-Type: application/json" -X GET 'centos01:9200/customer/_search?pretty' -d '{
    "query":{
        "match_all":{ }
    },
    "sort":[
            {
                "age":"desc"
            }
    	]
    }'

在这里插入图片描述

Query DSL

上面的将搜索参数放入body体的请求方式在Elasticsearch中被称为领域特定语言(DSL)

查询(Query)

https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-queries.html

过滤(Filter)

https://www.elastic.co/guide/en/elasticsearch/reference/1.7/query-dsl-filters.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值