Elasticsearch 參考指南(Multi Get API)

Multi Get API

Multi Get API允許基於索引、類型(可選)和id(可能還有路由)獲得多個文檔,響應包括一個docs數組,其中包含所有獲取的文檔,以便與原始的multi-get請求相對應(如果特定get失敗,則在響應中包括包含此錯誤的對象),成功get的結構在結構上類似於get API提供的文檔。

這裏有一個例子:

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

mget端點還可以用於索引(在這種情況下,body中不需要索引):

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 /test/_doc/_mget?stored_fields=field1,field2
{
    "docs" : [
        {
            "_id" : "1" 
        },
        {
            "_id" : "2",
            "stored_fields" : ["field3", "field4"] 
        }
    ]
}
  • "_id" : "1" => 返回field1field2
  • "stored_fields" : ["field3", "field4"] => 返回field3field4

路由

還可以將路由值指定爲參數:

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章