elasticsearch6.7 05. Document APIs(8)Multi Get API

7、Multi Get API(Multi Get API)

multi GET API 允许你一次性获取多个文档,你需要指定docs数组,其中包含了所有你需要查询的文档,每个查询结构至少包含索引,类型和文档id。如果操作过程中遇到错误将会返回错误信息。返回的结果与 GET API 的结果结构类似。

如下例所示:

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

mget 也可以仅仅针对一个索引使用:

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

或者针对一个类型使用:

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

如果仅指定id参数的话,也可以用一个id数组来指定:

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

7.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"]
            }
        }
    ]
}

7.2 字段(Fileds)

为了节省内存和存储空间,你可能不启用_source,使用store保存部分字段。指定为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" 【1】
        },
        {
            "_id" : "2",
            "stored_fields" : ["field3", "field4"] 【2】
        }
    ]
}

【1】:返回field1和field2字段

【2】:返回field3和field4字段

7.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作为路由值

7.4 安全(Security)

查阅URL-based access control

猜你喜欢

转载自www.cnblogs.com/wtc1994/p/10707326.html