ElasticSearch 6.3版本 Document APIs之Multi Get API

Multi GET API允许基于索引,类型(可选)和id(以及可能的路由)获取多个文档。响应包括一个 docs 数组,其中所有获取的文档按顺序对应于原始的多重获取请求(如果特定获取失败,则包含此错误的对象将包含在响应中)。成功获取的结构在结构上类似于 get  API 提供的文档。

GET / _mget 
{ 
    “docs”:[ 
        { 
            “ _ index ”:“test”,
            “_ type”:“_ doc”,
            “ _ id ”:“1” 
        },
        { 
            “ _ index”:“test”,
            “_ type”:“_ doc” “,
            ”_ id“:”2“ 
        } 
    ] 
}
针对索引

GET /test/_mget
{
    "docs" : [
        {
            "_type" : "_doc",
            "_id" : "1"
        },
        {
            "_type" : "_doc",
            "_id" : "2"
        }
    ]
}
针对类型

GET /test/type/_mget
{
    "docs" : [
        {
            "_id" : "1"
        },
        {
            "_id" : "2"
        }
    ]
}
在这种情况下,该ids元素可以直接用于简化请求:

GET / test / type / _mget 
{ 
    “ids”:[“1”,“2”] 
}

源过滤

默认情况下,将为每个文档返回(如果有存储)源字段。与GET API类似,您可以通过使用源代码参数只检索部分源(或者根本不检索)。您还可以使用URL参数_source_source_include_source_exclude指定默认值,默认值将在没有文档指令时使用。

GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "_source" : false
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2",
            "_source" : ["field3", "field4"]
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "3",
            "_source" : {
                "include": ["user"],
                "exclude": ["user.location"]
            }
        }
    ]
}
字段

可以指定要为每个文档检索特定存储的字段以获取,类似于Get API 的 stored_fields 参数。例如:
GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "stored_fields" : ["field1", "field2"]
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2",
            "stored_fields" : ["field3", "field4"]
        }
    ]
}
或者,您可以 stored_fields 将查询字符串中的参数指定为默认值,以应用于所有文档。
GET /test/type/_mget?stored_fields=field1,field2
{
    "docs" : [
        {
            "_id" : "1" 
        },
        {
            "_id" : "2",
            "stored_fields" : ["field3", "field4"] 
        }
    ]
}
id为1的文档返回 field1,field2;
id为2 的文档返回field3,field4.

路由

您还可以将路由值指定为参数:
GET /_mget?routing=key1
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "routing" : "key2"
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2"
        }
    ]
}
在此示例中, test/type/2 将从与路由密钥对应的分片中获取 key1 文档,但是 test/type/1 将从与路由密钥对应的分片中获取文档 key2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值