Elasticsearch mget

----multi-get----

Elasticsearch 的速度已经很快了,但甚至能更快。 来将这些检索请求放在一个请求中,将比逐个文档请求更快地检索到全部文档。

mget API 要求有一个 docs 数组作为参数,每个_source 参数来指定这些字段的名字:

GET /_mget
{
   "docs" : [
      {
         "_index" : "website",
         "_type" :  "blog",
         "_id" :    2
      },
      {
         "_index" : "website",
         "_type" :  "pageviews",
         "_id" :    1,
         "_source": "views"
      }
   ]
}

该响应体也包含一个 docs 数组get request 请求所得到的响应体相同:

{
  "docs": [
    {
      "_index": "website",
      "_type": "blog",
      "_id": "2",
      "_version": 6,
      "found": true,
      "_source": {
        "title": "My first external blog entry",
        "text": "Starting  "
      }
    },
    {
      "_index": "website",
      "_type": "pageviews",
      "_id": "1",
      "_version": 5,
      "found": true,
      "_source": {
        "views": 5
      }
    }
  ]
}

如果检索的数据都在相同的_index(甚至相同的_type)中,则可以在URL中指定默认的/_index或者默认的/_index/_type,也可以通过单独的请求覆盖这些值:

GET /website/blog/_mget
{
   "docs" : [
      { "_id" : 2 },
      { "_type" : "pageviews", "_id" :   1 }
   ]
}
{
  "docs": [
    {
      "_index": "website",
      "_type": "blog",
      "_id": "2",
      "_version": 6,
      "found": true,
      "_source": {
        "title": "My first external blog entry",
        "text": "Starting  "
      }
    },
    {
      "_index": "website",
      "_type": "pageviews",
      "_id": "1",
      "_version": 5,
      "found": true,
      "_source": {
        "views": 5
      }
    }
  ]
}
返回结果

事实上如果所有文档的_index和_type都是相同的,你可以只传一个ids数组,而不是整个docs数组:

GET /website/blog/_mget
{
   "ids" : [ "2", "1" ,"30"]
}

这里的id为1,2 的文档是存在的,30文档是不存在的。并不会影响其他文档的检索,每个文档都是单独索引和报告的

{
  "docs": [
    {
      "_index": "website",
      "_type": "blog",
      "_id": "2",
      "_version": 6,
      "found": true,
      "_source": {
        "title": "My first external blog entry",
        "text": "Starting  "
      }
    },
    {
      "_index": "website",
      "_type": "blog",
      "_id": "1",
      "_version": 4,
      "found": true,
      "_source": {
        "title": "My first blog entry",
        "text": "Starting to get the hang of this...",
        "views": 1,
        "tags": [
          "testing"
        ]
      }
    },
    {
      "_index": "website",
      "_type": "blog",
      "_id": "30",
      "found": false
    }
  ]
}
返回结果

注意:这个通过HTTP返回的状态码为200 不能判断是否有数据返回, 需要进一步的判断found标记来判断数,因为mget请求本身已经成功执行。