Elasticsearch之基本API

  • 一、文档的CRUD
  • 1.index
  • 2.Create
  • 3.Read
  • 4.update
  • 5.delete
  • 二、批量操作 Bulk API
  • 批量插入
  • 批量查询


一、文档的CRUD

Type名,约定都用_doc

1.index

如果ID不存在,创建新的文档。否则先删除现有的文档,再创建新的文档,文档的版本会增加。

支持自动生成文档id和指定文档id两种方式

示例:

// 首先我们创建一个文档
put index/_doc/1
{
  "user":"程大帅",
  "comment":"ES真好玩"
}

返回:
{
  "_index" : "index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}
// result为created,_version = 1


// 此时我们对id为1的文档进行更新
put index/_doc/1
{
  "user":"程大帅2",
  "comment":"ES真好玩2"
}
返回
{
  "_index" : "index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 1
}
// result为updated,_version为2

我们尝试自动生成一个文档id,使用post请求,不指定id

post index/_doc
{
  "user":"程大帅2",
  "comment":"ES真好玩2"
}
返回
{
  "_index" : "index",
  "_type" : "_doc",
  "_id" : "K3XS0n0BGhZc0_6ZeZMe",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 7,
  "_primary_term" : 1
}
// 可以看到系统自动生成了一个id, "K3XS0n0BGhZc0_6ZeZMe"
// 我们可以获取一下
get index/_doc/K3XS0n0BGhZc0_6ZeZMe
{
  "_index" : "index",
  "_type" : "_doc",
  "_id" : "K3XS0n0BGhZc0_6ZeZMe",
  "_version" : 1,
  "_seq_no" : 7,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "程大帅2",
    "comment" : "ES真好玩2"
  }
}

2.Create

如果id存在,则会创建失败。

两种方式:

  1. 使用_create语句put index/_create/1
  2. 使用index,指定操作类型为create,put index/_doc/1?op_type=create

示例

// 上一步我们已经创建了文档,这时候再创建id为1的文档时会直接报错
put index/_create/1
{
  "user":"程大帅2",
  "comment":"ES真好玩2"
}
返回:
{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[1]: version conflict, document already exists (current version [2])",
        "index_uuid" : "uIQHbojCSZaFitYX9YFKsA",
        "shard" : "0",
        "index" : "index"
      }
    ],
    "type" : "version_conflict_engine_exception",
    "reason" : "[1]: version conflict, document already exists (current version [2])",
    "index_uuid" : "uIQHbojCSZaFitYX9YFKsA",
    "shard" : "0",
    "index" : "index"
  },
  "status" : 409
}

3.Read

获取文档操作

示例

//获取一个已存在的文档
get index/_doc/1
返回
{
  "_index" : "index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 2,
  "_seq_no" : 1,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "程大帅2",
    "comment" : "ES真好玩2"
  }
}

返回结构的_source中默认包含了文档的所有原始信息。

获取一个不存在的文档id

get index/_doc/2
返回
{
  "_index" : "index",
  "_type" : "_doc",
  "_id" : "2",
  "found" : false
}

//可以看到当我们获取一个不存在的文档时,返回的found为false。

4.update

使用POST
文档必须存在,更新操作只会对相应更新的字段做操作

示例

POST index/_update/1
{
  "doc":{
      "user":"程大帅3"
  }
}

此时我们再执行查询
get index/_doc/1
{
  "_index" : "index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 5,
  "_seq_no" : 4,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "user" : "程大帅3",
    "comment" : "ES真好玩2"
  }
}
//可以看到,只有user字段做了修改。

我们再尝试下对不存在的id做修改操作
POST index/_update/3
{
  "doc":{
      "user":"程大帅3"
  }
}
报错,提示没有找到
{
  "error" : {
    "root_cause" : [
      {
        "type" : "document_missing_exception",
        "reason" : "[_doc][3]: document missing",
        "index_uuid" : "uIQHbojCSZaFitYX9YFKsA",
        "shard" : "0",
        "index" : "index"
      }
    ],
    "type" : "document_missing_exception",
    "reason" : "[_doc][3]: document missing",
    "index_uuid" : "uIQHbojCSZaFitYX9YFKsA",
    "shard" : "0",
    "index" : "index"
  },
  "status" : 404
}

5.delete

delete语句作用于index时是删除索引,所用于document时是删除文档。

示例

delete index/_doc/1

返回
{
  "_index" : "index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 6,
  "result" : "deleted",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 5,
  "_primary_term" : 1
}

二、批量操作 Bulk API

支持再一次API调用中,对不同的索引进行操作

支持index、create、update、delete四种操作类型

可以在URL中指定index,也可以在请求的Payload中进行操作。

操作中某条数据失败,不会影响到其他操作

返回结果中包含了每一条执行的结果

批量插入

示例

POST _bulk
{ "index":{ "_index":"test", "_id":"1" } }
{ "field1":"value1" }
{ "delete":{ "_index":"test", "_id":"2" } }
{ "create":{ "_index":"test2", "_id":"3" } }
{ "field1":"value1" }
{ "update":{ "_index":"test", "_id":"1" } }
{ "doc":{ "field2":"value2" } }

返回
{
  "took" : 809,
  "errors" : false,
  "items" : [
    {
      "index" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "delete" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "2",
        "_version" : 1,
        "result" : "not_found",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 1,
        "_primary_term" : 1,
        "status" : 404
      }
    },
    {
      "create" : {
        "_index" : "test2",
        "_type" : "_doc",
        "_id" : "3",
        "_version" : 1,
        "result" : "created",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 0,
        "_primary_term" : 1,
        "status" : 201
      }
    },
    {
      "update" : {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "1",
        "_version" : 2,
        "result" : "updated",
        "_shards" : {
          "total" : 2,
          "successful" : 1,
          "failed" : 0
        },
        "_seq_no" : 2,
        "_primary_term" : 1,
        "status" : 200
      }
    }
  ]
}

通过上述操作相应结果可以看到每一条命令的操作类型以及操作返回码。

批量查询

那么我们再进行一次获取
示例:

get /_mget
{
  "docs":[
    {
      "_index":"test",
      "_id":"1"
    },
    {
      "_index":"test",
      "_id":"2"
    },
        {
      "_index":"test2",
      "_id":"1"
    }
    ]
}

结果:
{
  "docs" : [
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 2,
      "_seq_no" : 2,
      "_primary_term" : 1,
      "found" : true,
      "_source" : {
        "field1" : "value1",
        "field2" : "value2"
      }
    },
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "2",
      "found" : false
    },
    {
      "_index" : "test2",
      "_type" : "_doc",
      "_id" : "1",
      "found" : false
    }
  ]
}

同时,ES也提供了msearch操作来进行批量查询,可以对不同的索引进行查询

post index/_msearch
{}
{"query":{"match_all":{}},"from" : 0, "size" : 10}
{"index":"test"}
{"query":{"match_all":{}},"from" : 0, "size" : 10}

返回
{
  "took" : 2,
  "responses" : [
    {
      "took" : 2,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 2,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "index",
            "_type" : "_doc",
            "_id" : "KnXR0n0BGhZc0_6ZWpOi",
            "_score" : 1.0,
            "_source" : {
              "user" : "程大帅2",
              "comment" : "ES真好玩2"
            }
          },
          {
            "_index" : "index",
            "_type" : "_doc",
            "_id" : "K3XS0n0BGhZc0_6ZeZMe",
            "_score" : 1.0,
            "_source" : {
              "user" : "程大帅2",
              "comment" : "ES真好玩2"
            }
          }
        ]
      },
      "status" : 200
    },
    {
      "took" : 2,
      "timed_out" : false,
      "_shards" : {
        "total" : 1,
        "successful" : 1,
        "skipped" : 0,
        "failed" : 0
      },
      "hits" : {
        "total" : {
          "value" : 1,
          "relation" : "eq"
        },
        "max_score" : 1.0,
        "hits" : [
          {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "_score" : 1.0,
            "_source" : {
              "field1" : "value1",
              "field2" : "value2"
            }
          }
        ]
      },
      "status" : 200
    }
  ]
}