Elasticsearch 参考指南(Multi Get API)

Multi Get API

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

这里有一个例子:

GET /_mget
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1"
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2"
        }
    ]
}

mget端点还可以用于索引(在这种情况下,body中不需要索引):

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

和类型:

GET /test/_doc/_mget
{
    "docs" : [
        {
            "_id" : "1"
        },
        {
            "_id" : "2"
        }
    ]
}

在这种情况下,可以直接使用ids元素来简化请求:

GET /test/_doc/_mget
{
    "ids" : ["1", "2"]
}

源过滤

默认情况下,每个文档将返回_source字段(如果存储了的话),与get API类似,通过使用_source参数,你可以只检索_source的一部分(或者完全没有),你还可以使用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/_doc/_mget?stored_fields=field1,field2
{
    "docs" : [
        {
            "_id" : "1" 
        },
        {
            "_id" : "2",
            "stored_fields" : ["field3", "field4"] 
        }
    ]
}
  • "_id" : "1" => 返回field1field2
  • "stored_fields" : ["field3", "field4"] => 返回field3field4

路由

还可以将路由值指定为参数:

GET /_mget?routing=key1
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "routing" : "key2"
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2"
        }
    ]
}

在本例中,文档test/_doc/2将从与路由键key1对应的碎片中获取,但是,文档test/_doc/1将从与路由键key2对应的碎片中获取。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值