Elasticsearch 相关 api 操作

时间:2022-04-22
本文章向大家介绍Elasticsearch 相关 api 操作,主要内容包括A. es 操作、2. 获取集群中的节点列表、3. 创建索引、4. 获取索引、5. 索引文档、6. 查询文档、7. 删除索引、8. 更新文档、9. 删除文档、10. 批量查询文档、11. 字符串查询文档、12. DSL条件查询文档、13. 批量更新文档、B. 结尾、基本概念、基础应用、原理机制和需要注意的事项等,并结合实例形式分析了其使用技巧,希望通过本文能帮助到大家理解应用这部分内容。

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。

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