13、Multi Get API

原文地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html

elasticsearch版本:6.5

目录地址:https://blog.csdn.net/mine_1/article/details/85623429

Multi GET API可以根据给定的index,type和id或routing查询多个文档。返回值中包含满足条件的文档的数组。如果操作过程中遇到错误将会返回错误信息。返回的结果与GET API的结果结构类似。如下面的例子:

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

mget也可以将index或type放到url中,如:

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

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

多个id也可以用ids传递,如:

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

(1)source filtering

默认情况下,_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"]
            }
        }
    ]
}

(2)Fields

指定为stored的字段可以通过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"]
        }
    ]
}

当然,您也可以放到url的stored_fields参数中,如:

GET /test/_doc/_mget?stored_fields=field1,field2
{
    "docs" : [
        {
            "_id" : "1" 
        },
        {
            "_id" : "2",
            "stored_fields" : ["field3", "field4"] 
        }
    ]
}

id为1的文档将返回field1和field2,id为2的文档将会返回field3和field4。

(3)Routing

您也可以指定routing参数:

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获得。
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值