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對應的碎片中獲取。

相關文章
相關標籤/搜索