[关闭]
@wddpct 2017-03-26T08:35:22.000000Z 字数 5294 阅读 2468

Elasticsearch 相关 api 操作

Elasticsearch


A. es 操作

1. 检查 es 集群健康状态

bash命令:curl -XGET 'localhost:9200/_cat/health?v&pretty'
kibana命令:GET /_cat/health?v
返回示例:
此处输入图片的描述

描述:可以看到红框范围内的值为 yellow,它代表了我们 es 服务集群的健康状态,详细描述如下。解读后我们可以了解到,我们的 yellow 状态对于日常使用没有影响,它只是因为我们的集群暂时由单节点服务器组成,没有多余的节点分配给我们的分片副本了,解决示例会在以后的文章中给出。

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
返回示例:

  1. {
  2. "acknowledged": true,
  3. "shards_acknowledged": true
  4. }

4. 获取索引

bash命令:curl -XGET 'localhost:9200/_cat/indices?v&pretty'
kibana命令:GET /_cat/indices?v
返回示例:
此处输入图片的描述
描述: 该条指令用于获取所有索引列表

5. 索引文档

bash命令:

  1. curl -XPUT 'localhost:9200/customer/external/1?pretty&pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "name": "John Doe"
  4. }
  5. '

kibana命令:

  1. PUT /customer/external/1?pretty
  2. {
  3. "name": "John Doe"
  4. }

返回示例:

  1. {
  2. "_index": "customer",
  3. "_type": "external",
  4. "_id": "1",
  5. "_version": 1,
  6. "result": "created",
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. },
  12. "created": true
  13. }

描述:索引中可以存在不同的类型,我们刚刚便创建了类型 “external”及其文档,大家可以把它理解为关系型数据库中的表和列。索引时 ID 字段是可选的,假如我们没有指定,es 将自动为我们生成 ID(此种情况下需要使用 POST HTTPVerb)。

6. 查询文档

bash命令:curl -XGET 'localhost:9200/customer/external/1?pretty&pretty'
kibana命令:GET /customer/external/1?pretty
返回示例:

  1. {
  2. "_index": "customer",
  3. "_type": "external",
  4. "_id": "1",
  5. "_version": 1,
  6. "found": true,
  7. "_source": {
  8. "name": "John Doe"
  9. }
  10. }

描述: 简单查询格式一般为 /index/type/id

7. 删除索引

bash命令:curl -XDELETE 'localhost:9200/customer?pretty&pretty'
kibana命令:DELETE /customer?pretty
返回示例:

  1. {
  2. "acknowledged": true
  3. }

描述: 通过添加 * 通配符,我们可以删除所有形如 customer2017-3-8-11-26-58的索引。

8. 更新文档

bash命令:

  1. curl -XPOST 'localhost:9200/customer/external/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "doc": { "name": "Jane Doe", "age": 20 }
  4. }
  5. '

kibana命令:

  1. POST /customer/external/1/_update?pretty
  2. {
  3. "doc": { "name": "Jane Doe", "age": 20 }
  4. }

返回示例:

  1. {
  2. "_index": "customer",
  3. "_type": "external",
  4. "_id": "1",
  5. "_version": 7, //修改次数
  6. "result": "updated",
  7. "_shards": {
  8. "total": 2,
  9. "successful": 1,
  10. "failed": 0
  11. }
  12. }

描述: 我们刚才针对之前录入的 customer 的某条 id 为 1 的数据进行了更新,并扩充了其属性。值得注意的是,当我们执行更新操作时,es 实际上是对索引的文档进行了删除并重建的操作,并不是真正意义上的更新。

9. 删除文档

bash命令: curl -XDELETE 'localhost:9200/customer/ external/2?pretty?pretty'
kibana命令:DELETE /customer/external/2?pretty
返回示例:

  1. {
  2. "found": true,
  3. "_index": "customer",
  4. "_type": "external",
  5. "_id": "1",
  6. "_version": 8,
  7. "result": "deleted",
  8. "_shards": {
  9. "total": 2,
  10. "successful": 1,
  11. "failed": 0
  12. }
  13. }

10. 批量查询文档

bash命令: curl -XGET 'localhost:9200/customer/ external/_search?pretty'
kibana命令:GET /customer/external/_search?pretty
返回示例:

  1. {
  2. "took": 18,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "failed": 0
  8. },
  9. "hits": {
  10. "total": 2,
  11. "max_score": 1,
  12. "hits": [
  13. {
  14. "_index": "customer",
  15. "_type": "external",
  16. "_id": "AVqm6MRTU67sF7xAeJ5R",
  17. "_score": 1,
  18. "_source": {
  19. "name": "John Doe"
  20. }
  21. },
  22. {
  23. "_index": "customer",
  24. "_type": "external",
  25. "_id": "AVqm6MURU67sF7xAeJ5S",
  26. "_score": 1,
  27. "_source": {
  28. "name": "Jane Doe"
  29. }
  30. }
  31. ]
  32. }
  33. }

描述: 刚才演示的都是根据 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命令:

  1. curl -XGET 'localhost:9200/customer/external/_search?pretty' -H 'Content-Type: application/json' -d'
  2. {
  3. "query": {
  4. "match" : {
  5. "name":"Jane"
  6. }
  7. }
  8. }
  9. '

kibana命令:

  1. GET /customer/external/_search?pretty
  2. {
  3. "query": {
  4. "match" : {
  5. "name":"Joe"
  6. }
  7. }
  8. }

返回示例: 暂略
描述: DSL 被称为特定领域语言,如 T-SQL 就是一种 DSL。它提供了更丰富更强大的方法供给开发者使用,如以上代码和之前的字符串查询的含义便是相同,更多用法详见 Query-DSL

13. 批量更新文档

bash命令:

  1. curl -XPOST 'localhost:9200/customer/external/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d'
  2. {"index":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
  3. {"name": "John Doe" }
  4. {"index":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
  5. {"name": "Jane Doe" }
  6. {"update":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
  7. {"doc": { "name": "John Doe becomes Jane Doe" } }
  8. {"delete":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
  9. '

kibana命令:

  1. POST /customer/external/_bulk?pretty
  2. {"index":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
  3. {"name": "John Doe" }
  4. {"index":{"_id":"AVqm6MURU67sF7xAeJ5S"}}
  5. {"name": "Jane Doe" }
  6. {"update":{"_id":"AVqm6MRTU67sF7xAeJ5R"}}
  7. {"doc": { "name": "John Doe becomes Jane Doe" } }
  8. {"delete":{"_id":"AVqm6MURU67sF7xAeJ5S"}}

返回示例:

  1. {
  2. "took": 30,
  3. "errors": false,
  4. "items": [
  5. {
  6. "index": {//ignore},
  7. "created": false,
  8. "status": 200
  9. }
  10. },
  11. {
  12. "index": {//ignore},
  13. "created": true,
  14. "status": 201
  15. }
  16. },
  17. {
  18. "update": {//ignore},
  19. "status": 200
  20. }
  21. },
  22. {
  23. "delete": {//ignore},
  24. "status": 200
  25. }
  26. }
  27. ]
  28. }

描述: 不要被这么“多”的命令吓到了,其实我们仔细看下来,就会发现笔者只是让 es 执行了添加 id 为 xx 和 yy 的文档,然后再更新 id 为 xx 的文档内容,最后删除了 id 为 yy 的文档。一步到位,顺序执行,更不会因为中间某步出了错误便停止运行,所以有关于这个特性,希望大家在执行命令的时候要特别注意。

B. 结尾

通过上文的描述,我们已经初步了解了 es 中对于文档,类型和索引的相关操作,但是要注意,我们的努力仍然是十分粗糙的。更多的详细操作,大家可以参考官网的 api 文档,里面提到了本文省略的聚合,过滤条件查询和批量删除等十分有效的 api。

笔者之所以没提出省略的部分,只是因为这样编写文章的工程量将会比较繁重,而且考虑到开发者平时更多地会使用客户端进行远程操作,精通开发平台内专业工具的使用就已经足够应付大多数的需求了。

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注