Elasticsearch 5.x (三) Elasticsearch REST API

本文详细介绍了Elasticsearch 5.x版本的REST API,涵盖文档API、搜索API、索引API、查看API和集群API的各种操作,如增删改查、搜索建议、索引创建与管理、集群健康状态监控等。通过实例展示了如何使用curl命令执行相关操作。

elasticsearch中的API:官网链接:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs.html#docs

es中的API按照大类分为下面几种:
文档API: 提供对文档的增删改查操作
搜索API: 提供对文档进行某个字段的查询
索引API: 提供对索引进行操作
查看API: 按照更直观的形式返回数据,更适用于控制台请求展示
集群API: 对集群进行查看和操作的API

文档API
Index API: 创建并建立索引
Get API: 获取文档
DELETE API: 删除文档
UPDATE API: 更新文档
Multi Get API: 一次批量获取文档
Bulk API: 批量操作,批量操作中可以执行增删改查
DELETE By Query API: 根据查询删除
Term Vectors: 词组分析,只能针对一个文档
Multi termvectors API: 多个文档的词组分析
multiGet的时候内部的行为是将一个请求分为多个,到不同的node中进行请求,再将结果合并起来。
如果某个node的请求查询失败了,那么这个请求仍然会返回数据,只是返回的数据只有请求成功的节点的查询数据集合。
词组分析的功能能查出比如某个文档中的某个字段被索引分词的情况。

搜索API
基本搜索接口: 搜索的条件在url中
DSL搜索接口: 搜索的条件在请求的body中
搜索模版设置接口: 可以设置搜索的模版,模版的功能是可以根据不同的传入参数,进行不同的实际搜索
搜索分片查询接口: 查询这个搜索会使用到哪个索引和分片
Suggest接口: 搜索建议接口,输入一个词,根据某个字段,返回搜索建议。
批量搜索接口: 把批量请求放在一个文件中,批量搜索接口读取这个文件,进行搜索查询
Count接口: 只返回符合搜索的文档个数
文档存在接口: 判断是否有符合搜索的文档存在
验证接口: 判断某个搜索请求是否合法,不合法返回错误信息
解释接口: 使用这个接口能返回某个文档是否符合某个查询,为什么符合等信息
抽出器接口: 简单来说,可以用这个接口指定某个文档符合某个搜索,事先未文档建立对应搜索

索引API
创建索引接口(POST my_index)
删除索引接口(DELETE my_index)
获取索引信息接口(GET my_index)
索引是否存在接口(HEAD my_index)
打开/关闭索引接口(my_index/_close, my_index/_open)
设置索引映射接口(PUT my_index/_mapping)
获取索引映射接口(GET my_index/_mapping)
获取字段映射接口(GET my_index/_mapping/field/my_field)
类型是否存在接口(HEAD my_index/my_type)
删除映射接口(DELTE my_index/_mapping/my_type)
索引别名接口(_aliases)
更新索引设置接口(PUT my_index/_settings)
获取索引设置接口(GET my_index/_settings)
分析接口(_analyze): 分析某个字段是如何建立索引的
建立索引模版接口(_template): 为索引建立模版,以后新创建的索引都可以按照这个模版进行初始化
预热接口(_warmer): 某些查询可以事先预热,这样预热后的数据存放在内存中,增加后续查询效率
状态接口(_status): 索引状态
批量索引状态接口(_stats): 批量查询索引状态
分片信息接口(_segments): 提供分片信息级别的信息
索引恢复接口(_recovery): 进行索引恢复操作
清除缓存接口(_cache/clear): 清除所有的缓存
输出接口(_flush)
刷新接口(_refresh)
优化接口(_optimize): 对索引进行优化
升级接口(_upgrade): 这里的升级指的是把索引升级到lucence的最新格式

查看API
查看别名接口(_cat/aliases): 查看索引别名
查看分配资源接口(_cat/allocation)
查看文档个数接口(_cat/count)
查看字段分配情况接口(_cat/fielddata)
查看健康状态接口(_cat/health)
查看索引信息接口(_cat/indices)
查看master信息接口(_cat/master)
查看nodes信息接口(_cat/nodes)
查看正在挂起的任务接口(_cat/pending_tasks)
查看插件接口(_cat/plugins)
查看修复状态接口(_cat/recovery)
查看线城池接口(_cat/thread_pool)
查看分片信息接口(_cat/shards)
查看lucence的段信息接口(_cat/segments)

集群API
查看集群健康状态接口(_cluster/health)
查看集群状况接口(_cluster/state)
查看集群统计信息接口(_cluster/stats)
查看集群挂起的任务接口(_cluster/pending_tasks)
集群重新路由操作(_cluster/reroute)
更新集群设置(_cluster/settings)
节点状态(_nodes/stats)
节点信息(_nodes)
节点的热线程(_nodes/hot_threads)
关闭节点(\nodes/_master/_shutdown)


介绍下配置文件和Elasticsearch REST API


配置使用:
ElasticSearch是先把索引的内容保存到内存之中,当内存不够时再把索引持久化到硬盘中,同时它还有一个队列,是在系统空闲时自动把索引写到硬盘中。
node
1. 如果你想让节点从不选举为主节点,只用来存储数据,可作为负载器
# node.master: false
# node.data: true

2. 如果想让节点成为主节点,且不存储任何数据,并保有空闲资源,可作为协调器
# node.master: true
# node.data: false

3. 如果想让节点既不称为主节点,又不成为数据节点,只作为搜索器,从节点中获取数据,生成搜索结果等
# node.master: false
# node.data: false

index
# 设置索引的分片数,默认为5
#index.number_of_shards: 5

# 设置索引的副本数,默认为1:
#index.number_of_replicas: 1

# "number_of_shards" 是索引创建后一次生成的,后续不可更改设置
# "number_of_replicas" 是可以通过API去实时修改设置的

path
# 配置文件存储位置
# path.conf: /path/to/conf

# 数据存储位置(单个目录设置)
# path.data: /path/to/data
# 多个数据存储位置,有利于性能提升
# path.data: /path/to/data1,/path/to/data2

# 临时文件的路径
# path.work: /path/to/work

# 日志文件的路径
# path.logs: /path/to/logs

# 插件安装路径
# path.plugins: /path/to/plugins

memory
# 当JVM开始写入交换空间时(swapping)ElasticSearch性能会低下,你应该保证它不会写入交换空间
# 设置这个属性为true来锁定内存,同时也要允许elasticsearch的进程可以锁住内存,linux下可以通过 `ulimit -l unlimited` 命令
# bootstrap.mlockall: true

# 确保 ES_MIN_MEM 和 ES_MAX_MEM 环境变量设置为相同的值,以及机器有足够的内存分配给Elasticsearch
# 注意:内存也不是越大越好,一般64位机器,最大分配内存别才超过32G

network  and  http
# 设置节点间交互的tcp端口,默认是9300
# transport.tcp.port: 9300

# 设置是否压缩tcp传输时的数据,默认为false,不压缩
# transport.tcp.compress: true

# 设置对外服务的http端口,默认为9200
# http.port: 9200

# 设置请求内容的最大容量,默认100mb
# http.max_content_length: 100mb

# 使用http协议对外提供服务,默认为true,开启
# http.enabled: false

#master选举最少的节点数,这个一定要设置为整个集群节点个数的一半加1,即N/2+1
#discovery.zen.minimum_master_nodes: 1


Elasticsearch REST API


curl 'http://192.28.80.11: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
1484115952 14:25:52  es-cluster green           2         2     10   5    0    0        0             0                  -                100.0%

curl 'http://192.28.80.11:9200/_cat/indices?v'  列出目录
health status index                           uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   .monitoring-es-2-2017.01.11     zgVABnpYRsSAKYzd3Gteuw   1   1        599           60      1.1mb        611.5kb
green  open   .monitoring-kibana-2-2017.01.10 mWxgna3UTIycG2aHoFKyYg   1   1        356            0      410kb          205kb
green  open   .kibana                         e_fivoO7RnyvJHOqGO6Eng   1   1          1            0      6.3kb          3.1kb
green  open   .monitoring-es-2-2017.01.10     LE7xJh5jQYufXRFUZ-hI2Q   1   1       3433           24        3mb          1.5mb
green  open   .monitoring-data-2              Y2m_qK1iSDu4SEIy7xYRpw   1   1          3            0     14.3kb          7.1kb

curl -XPUT 'http://192.28.80.11:9200/customer?pretty'  创建添加index 名为customer
{
  "acknowledged" : true,
  "shards_acknowledged" : true
}
curl -XDELETE 'http://192.28.80.11:9200/customer?pretty' 删除index
提示:health为yellow, 因为单节点,如果加入了replica之后就会转为green。

curl -XPUT 'http://192.28.80.11:9200/customer/external/1?pretty' -d '        #put写数据或者修改数据  
>  {
>
>   "name": "John Doe"
>
> }'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}
ID部分是可选的,如果是修改内容,不会新增,要新增需要不同的ID。
curl -XGET 'http://192.28.80.11:9200/customer/external/1?pretty'        #get读数据
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "name" : "John Doe"
  }
}

curl -XPOST 'http://192.28.80.11:9200/customer/external?pretty' -d '     #post方式是随机id       
>  {
>
>   "name": "Michal Doe"
>
> }'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "AVmMVSl25t4FL7WKqjRv",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  },
  "created" : true
}

curl -XPOST 'http://192.28.80.11:9200/customer/external/1/_update?pretty' -d '        #update文档    
>  {
>
>   "doc": { "name": "Jane Doe", "age": 20 }
>
> }'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 2,
    "failed" : 0
  }
}
curl -XGET 'http://192.28.80.11:9200/customer/external/1?pretty'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 2,
  "found" : true,
  "_source" : {
    "name" : "Jane Doe",
    "age" : 20
  }
}


curl -XDELETE 'http://192.28.80.11:9200/customer/external/2?pretty'  #删除文档
curl -XPOST 'http://192.28.80.11:9200/customer/external/_bulk?pretty' -d '  批处理:
curl -XPOST http://192.28.80.11:9200/es-cluster/node-1/_shutdown


不一一的举例子了,详细见官网即可。


未完待续 ……

【原创】原创文章,更多关注敬请关注微信公众号。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值