Elasticsearch mget
----multi-get----
Elasticsearch 的速度已经很快了,但甚至能更快。 将多个请求合并成一个,避免单独处理每个请求花费的网络延时和开销。 如果你需要从 Elasticsearch 检索很多文档,那么使用 multi-get 或者 mget
API 来将这些检索请求放在一个请求中,将比逐个文档请求更快地检索到全部文档。
mget
API 要求有一个 docs
数组作为参数,每个 元素包含需要检索文档的元数据, 包括 _index
、 _type
和 _id
。如果你想检索一个或者多个特定的字段,那么你可以通过 _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请求本身已经成功执行。
最新内容可以看我的blog: 40kuai