Elasticsearch 相关 api 操作
A. es 操作
1. 检查 es 集群健康状态
bash命令:curl -XGET 'localhost:9200/_cat/health?v&pretty'
kibana命令:GET /_cat/health?v
返回示例:
描述:可以看到红框范围内的值为 yellow,它代表了我们 es 服务集群的健康状态,详细描述如下。解读后我们可以了解到,我们的 yellow 状态对于日常使用没有影响,它只是因为我们的集群暂时由单节点服务器组成,没有多余的节点分配给我们的分片副本了,解决示例会在以后的文章中给出。
RED: Damnit. Some or all of (primary) shards are not ready.
YELLOW: Elasticsearch has allocated all of the primary shards, but some/all of the replicas have not been allocated.
GREEN: Great. Your cluster is fully operational. Elasticsearch is able to allocate all shards and replicas to machines within the cluster.
2. 获取集群中的节点列表
bash命令:curl -XGET 'localhost:9200/_cat /nodes?v?pretty'
kibana命令:GET /_cat/nodes?v
返回示例:
描述:注意最后的 name 即为我们某节点的唯一名称
以下描述中
所有的 kibana 命令都具有<REST HttpVerb> /<Index>/<Type>/<ID> <?pretty>格式
所有的 bash Curl 命令都具有curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'格式,请结合实际语句进行区分。
3. 创建索引
bash命令: curl -XPUT 'localhost:9200/customer?pretty&pretty'
kibana命令:PUT /customer?pretty
返回示例:
{
"acknowledged": true,
"shards_acknowledged": true
}
4. 获取索引
bash命令:curl -XGET 'localhost:9200/_cat/indices?v&pretty'
kibana命令:GET /_cat/indices?v
返回示例:
描述: 该条指令用于获取所有索引列表
5. 索引文档
bash命令:
curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
'
kibana命令:
PUT /customer/external/1?pretty
{
"name": "John Doe"
}
返回示例:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
描述:索引中可以存在不同的类型,我们刚刚便创建了类型 “external”及其文档,大家可以把它理解为关系型数据库中的表和列。索引时 ID 字段是可选的,假如我们没有指定,es 将自动为我们生成 ID(此种情况下需要使用 POST HTTPVerb)。
6. 查询文档
bash命令:curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'
kibana命令:GET /customer/external/1?pretty
返回示例:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "John Doe"
}
}
描述: 简单查询格式一般为 /index/type/id
7. 删除索引
bash命令:curl -XDELETE 'localhost:9200/customer?pretty&pretty'
kibana命令:DELETE /customer?pretty
返回示例:
{
"acknowledged": true
}
描述: 通过添加 * 通配符,我们可以删除所有形如 customer2017-3-8-11-26-58的索引。
8. 更新文档
bash命令:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
{
"doc": { "name": "Jane Doe", "age": 20 }
}
'
kibana命令:
POST /customer/external/1/_update?pretty
{
"doc": { "name": "Jane Doe", "age": 20 }
}
返回示例:
{
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 7, //修改次数
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
描述: 我们刚才针对之前录入的 customer 的某条 id 为 1 的数据进行了更新,并扩充了其属性。值得注意的是,当我们执行更新操作时,es 实际上是对索引的文档进行了删除并重建的操作,并不是真正意义上的更新。
9. 删除文档
bash命令: curl -XDELETE 'localhost:9200/customer/ external/2?pretty?pretty'
kibana命令:DELETE /customer/external/2?pretty
返回示例:
{
"found": true,
"_index": "customer",
"_type": "external",
"_id": "1",
"_version": 8,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
10. 批量查询文档
bash命令: curl -XGET 'localhost:9200/customer/ external/_search?pretty'
kibana命令:GET /customer/external/_search?pretty
返回示例:
{
"took": 18,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "customer",
"_type": "external",
"_id": "AVqm6MRTU67sF7xAeJ5R",
"_score": 1,
"_source": {
"name": "John Doe"
}
},
{
"_index": "customer",
"_type": "external",
"_id": "AVqm6MURU67sF7xAeJ5S",
"_score": 1,
"_source": {
"name": "Jane Doe"
}
}
]
}
}
描述: 刚才演示的都是根据 ID 获取单条数据,但是如果 ID 是自动生成值,这样的方式就不十分友好了,所以 es 提供了 _search 关键字来进行对索引类型中所有资源的获取操作,默认获取前十条匹配信息。其实有心的读者应该也注意到刚才我们在进行 update 操作时,指令中也有 _update 关键字,而在 kibana 的控制台中,我们还能通过它的智能提示获取更多这样的简便操作指令。如 _count,_create等。后续也将介绍使用匹配规则来查找特定的文档。
11. 字符串查询文档
bash命令: curl -XGET 'localhost:9200/customer/external/_search?q=name:Jane'
kibana命令:GET /customer/external/_search?q=name:Jane Doe?pretty
返回示例: 暂略
描述: 字符串查询即是一种条件查询,q=name:Jane 即意味着我们想要查询 external 类型中属性 name 值含有 Jane 的文档,es 会自动将相关匹配返回给我们。假如想要了解更多,请参见 Simple Query String Query。
12. DSL条件查询文档
bash命令:
curl -XGET 'localhost:9200/customer/external/_search?pretty' -H 'Content-Type: application/json' -d'
{
"query": {
"match" : {
"name":"Jane"
}
}
}
'
kibana命令:
GET /customer/external/_search?pretty
{
"query": {
"match" : {
"name":"Joe"
}
}
}
返回示例: 暂略
描述: DSL 被称为特定领域语言,如 T-SQL 就是一种 DSL。它提供了更丰富更强大的方法供给开发者使用,如以上代码和之前的字符串查询的含义便是相同,更多用法详见 Query-DSL。
13. 批量更新文档
bash命令:
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d'
{"index":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
{"name": "John Doe" }
{"index":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
{"name": "Jane Doe" }
{"update":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
'
kibana命令:
POST /customer/external/_bulk?pretty
{"index":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
{"name": "John Doe" }
{"index":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
{"name": "Jane Doe" }
{"update":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
返回示例:
{
"took": 30,
"errors": false,
"items": [
{
"index": {//ignore},
"created": false,
"status": 200
}
},
{
"index": {//ignore},
"created": true,
"status": 201
}
},
{
"update": {//ignore},
"status": 200
}
},
{
"delete": {//ignore},
"status": 200
}
}
]
}
描述: 不要被这么“多”的命令吓到了,其实我们仔细看下来,就会发现笔者只是让 es 执行了添加 id 为 xx 和 yy 的文档,然后再更新 id 为 xx 的文档内容,最后删除了 id 为 yy 的文档。一步到位,顺序执行,更不会因为中间某步出了错误便停止运行,所以有关于这个特性,希望大家在执行命令的时候要特别注意。
B. 结尾
通过上文的描述,我们已经初步了解了 es 中对于文档,类型和索引的相关操作,但是要注意,我们的努力仍然是十分粗糙的。更多的详细操作,大家可以参考官网的 api 文档,里面提到了本文省略的聚合,过滤条件查询和批量删除等十分有效的 api。
笔者之所以没提出省略的部分,只是因为这样编写文章的工程量将会比较繁重,而且考虑到开发者平时更多地会使用客户端进行远程操作,精通开发平台内专业工具的使用就已经足够应付大多数的需求了。
Elasticsearch 相关 api 操作的更多相关文章
- Elasticsearch java api操作(二)(Java High Level Rest Client)
一.说明: 一.Elasticsearch提供了两个JAVA REST Client版本: 1.java low level rest client: 低级别的rest客户端,通过http与集群交互, ...
- Elasticsearch java api操作(一)(Java Low Level Rest Client)
一.说明: 一.Elasticsearch提供了两个JAVA REST Client版本: 1.java low level rest client: 低级别的rest客户端,通过http与集群交互, ...
- selenium 相关api操作
driver.close(); //关闭程序 driver.findElement(By.id("idExpression")); //查找单个页面元素 driver.findEl ...
- elasticsearch 中文API(二)
客户端 有多个地方需要使用Java client: 在存在的集群中执行标准的index, get, delete和search 在集群中执行管理任务 当你要运行嵌套在你的应用程序中的Elasticse ...
- ElasticSearch之CURL操作(有空再去整理)
https://www.cnblogs.com/jing1617/p/8060421.html ElasticSearch之CURL操作 CURL的操作 curl是利用URL语法在命令行方式下工 ...
- 搜索引擎Elasticsearch REST API学习
Elasticsearch为开发者提供了一套基于Http协议的Restful接口,只需要构造rest请求并解析请求返回的json即可实现访问Elasticsearch服务器.Elasticsearch ...
- 第08章 ElasticSearch Java API
本章内容 使用客户端对象(client object)连接到本地或远程ElasticSearch集群. 逐条或批量索引文档. 更新文档内容. 使用各种ElasticSearch支持的查询方式. 处理E ...
- Elasticsearch Java API深入详解
0.题记 之前Elasticsearch的应用比较多,但大多集中在关系型.非关系型数据库与Elasticsearch之间的同步.以上内容完成了Elasticsearch所需要的基础数据量的供给.但想要 ...
- Elasticsearch Java API 很全的整理
Elasticsearch 的API 分为 REST Client API(http请求形式)以及 transportClient API两种.相比来说transportClient API效率更高, ...
随机推荐
- java &&与||短路的详解
短路运算符就是我们常用的“&&”.“||”,一般称为“条件操作”. class Logic{ public ststic void main(String[] args){ ...
- php拓展安装
Yaf安装配置:http://www.laruence.com/manual/yaf.install.html#yaf.installation.linux 下载Yaf的最新版本, 解压缩以后, 进入 ...
- 为什么JAVA要提供 wait/notify 机制?是为了避免轮询带来的性能损失
wait/notify 机制是为了避免轮询带来的性能损失. 为了说清道理,我们用“图书馆借书”这个经典例子来作解释. 一本书同时只能借给一个人.现在有一本书,图书馆已经把这本书借了张三. 在简单的s ...
- JDK-8不是有效的Win32应用程序
- python连接redis,redis集群
python连接redis: import redis r = redis.Redis(host='192.168.50.181',port=6002) r.set('user_phone_14900 ...
- 215. Kth Largest Element in an Array(QuickSort)
Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...
- apache中开启rewrite
1.在apache配置文件httpd.conf中找到如下行: #LoadModule rewrite_module modules/mod_rewrite.so 去掉该行前面的#号 2.在httpd. ...
- 源码安装php时出现Sorry, I cannot run apxs. Possible reasons follow:
1.可能的原因是你没有安装perl > yum install perl > yum install httpd-devel 2.在你apache安装目录下的bin下找到apxs,并用vi ...
- Django之virtualenv下安装xadmin
1.安装xadmin,通过pip 进入virtualenv pip安装xadmin pyyuc:~ yuchao$ source PycharmProjects/mxvenv/bin/activate ...
- Codeforces 709B 模拟
B. Checkpoints time limit per test:1 second memory limit per test:256 megabytes input:standard input ...