Elasticsearch 相关 api 操做

A. es 操做

1. 检查 es 集群健康状态

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

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

  • 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.json

  • GREEN: Great. Your cluster is fully operational. Elasticsearch is able to allocate all shards and replicas to machines within the cluster.api

2. 获取集群中的节点列表

bash命令:curl -XGET 'localhost:9200/_cat /nodes?v?pretty'
kibana命令:GET /_cat/nodes?v
返回示例:
此处输入图片的描述
描述:注意最后的 name 即为咱们某节点的惟一名称bash

如下描述中
全部的 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
返回示例:app

{
  "acknowledged": true,
  "shards_acknowledged": true
}

4. 获取索引

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

5. 索引文档

bash命令:elasticsearch

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。

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

相关文章
相关标签/搜索