Multi Get API (多元获取接口)

Elasticsearch提供了一种Multi Get API,可以输入不同的index, doc_type, id, field等多种参数获取位于不同index,不同doc_type等等的记录,这在一些特定场景下比较有用,比如你需要在不同的index里面获取记录,一般做法是分开查询,多次查询。但是现在使用这个API可以在你知道id的情况下直接一次获取所需要的来自不同位置的记录。

话不多说,直接上代码学习就好了,很简单:

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

** url没有index信息,那就必须在请求数据中加上, 以下类似**

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

还可以这样:

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

甚至可以这样:

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"]
            }
        }
    ]
}
posted @ 2019-04-26 11:34  松松哥、  阅读(24)  评论(0)    收藏  举报  来源