Elasticsearch 参考指南(Multi Get API)

Multi Get API

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

这里有一个例子:url

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

mget端点还能够用于索引(在这种状况下,body中不须要索引):code

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

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

路由

还能够将路由值指定为参数:io

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对应的碎片中获取。

相关文章
相关标签/搜索