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效率更高, ...
随机推荐
- win10磁盘碎片整理
如果我们想要加快win10系统运行速度的话,就需要定期整理碎片才可以,减少卡顿,提高性能. 一:注意事项 固态硬盘用户千万不要使用‘磁盘碎片整理功能’,因为使用的技术不一样,使用window自带的该功 ...
- xshell分隔符
1.分隔符设置 \ :;`!@#$%^&()+|[]{}'",<>? 2.左键点击选中,右键点击复制
- JVM7、8详解及优化
一.引言:永久代为什么被移出HotSpot JVM了? 详见:JEP 122: Remove the Permanent Generation 原因主要有两个: 1.由于Permanent Gener ...
- eclipse新建maven项目出现红叉解决办法
新建的maven项目,项目内代码及pom.xml没有任何问题,但项目上就是有红叉,这时点开Markers(Window–>show veiw–>Markers),查看错误的详细信息,信息上 ...
- Mysql两个time类型计算时间相减
round((UNIX_TIMESTAMP(finishtime)-UNIX_TIMESTAMP(starttime))/60) 得到的时间是分钟数
- 使用UUID方法生成全球唯一标识
需要生成唯一字符串,如生成应用标识等,可以直接用java.util.UUID类实现. UUID(Universally Unique Identifier)全局唯一标识符,是指在一台机器上生成的数字, ...
- python之字符串【str】
#Auther Bob#--*--conding:utf-8 --*-- #定义一个str的对象,有下面两种方法name = 'Bob abc'job = str('it')print(type(na ...
- [leetcode]158. Read N Characters Given Read4 II - Call multiple times 用Read4读取N个字符2 - 调用多次
The API: int read4(char *buf) reads 4 characters at a time from a file. The return value is the actu ...
- 20-java 对象链表空没空呢
写了一个 对象链表,往里面add了一些对象,最后我想看下链表是否为空,用 == null 为假,也看不出, 看下长度? 好吧, size() = 1: 打印 null , 那到底是不是空 啊, ...
- 基本控件设置边角图片 drawableleft
btn.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icon_galley_comment, 0, 0,0); 四个参数分别是左上右下四个方向 ...