elasticsearch之Document APIs【Multi Get API】

环境

elasticsearch:5.5

Multi Get API

Multi Get API 允许基于indextype(可选),id(或者是路由)来获取多个文档。
抓取到的所有文档都包含在响应字段docs里面,该字段是个数组,每个元素与get api获取的文档都有相似的结构。

GET _mget
{
  "docs":[
    {
      "_index":"bank",
      "_type":"account",
      "_id":1
    },
    {
      "_index":"bank",
      "_type":"account",
      "_id":2
    }
    ]
}

得到的结果为:

{
  "docs": [
    {
      "_index": "bank",
      "_type": "account",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "account_number": 1,
        "balance": 39225,
        "firstname": "Amber",
        "lastname": "Duke",
        "age": 32,
        "gender": "M",
        "address": "880 Holmes Lane",
        "employer": "Pyrami",
        "email": "amberduke@pyrami.com",
        "city": "Brogan",
        "state": "IL"
      }
    },
    {
      "_index": "bank",
      "_type": "account",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "account_number": 2,
        "balance": 28838,
        "firstname": "Roberta",
        "lastname": "Bender",
        "age": 22,
        "gender": "F",
        "address": "560 Kingsway Place",
        "employer": "Chillium",
        "email": "robertabender@chillium.com",
        "city": "Bennett",
        "state": "LA"
      }
    }
  ]
}

mget也可以用在索引(index)的后面。(这种情况下,body不是必须的):

GET bank/_mget
{
  "docs":[
    {
      "_type":"account",
      "_id":1
    },
    {
      "_type":"account",
      "_id":2
    }
    ]
}

其结果也是和上面一样的。

也可以在路径上加上type

GET bank/account/_mget
{
  "docs":[
    {
      "_id":1
    },
    {
      "_id":2
    }
    ]
}

其结果也是一样的。
这种情况下,可以直接使用ids来简化请求:

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

Optional Type

mget API 允许_type是可选的。把其设置为_all或者留空以便获取所有类型中与id相匹配的第一个文档。

如果你不设置type,并且有多个文档的id相同,你最终只会得到匹配文档的第一个。

例如,如果你有个文档id:1, 类型(即:表)typeAtypeB都有id:1的文档,则以下请求只会返回同一个文档两次:

GET bank/_mget
{
  "ids":["1", "1"]
}

结果为:

{
  "docs": [
    {
      "_index": "bank",
      "_type": "account",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "account_number": 1,
        "balance": 39225,
        "firstname": "Amber",
        "lastname": "Duke",
        "age": 32,
        "gender": "M",
        "address": "880 Holmes Lane",
        "employer": "Pyrami",
        "email": "amberduke@pyrami.com",
        "city": "Brogan",
        "state": "IL"
      }
    },
    {
      "_index": "bank",
      "_type": "account",
      "_id": "1",
      "_version": 1,
      "found": true,
      "_source": {
        "account_number": 1,
        "balance": 39225,
        "firstname": "Amber",
        "lastname": "Duke",
        "age": 32,
        "gender": "M",
        "address": "880 Holmes Lane",
        "employer": "Pyrami",
        "email": "amberduke@pyrami.com",
        "city": "Brogan",
        "state": "IL"
      }
    }
  ]
}

这种情况下,你需要明确设置_type

GET bank/_mget
{
  "docs" : [
        {
            "_type":"typeA",
            "_id" : "1"
        },
        {
            "_type":"typeB",
            "_id" : "1"
        }
    ]
}

Source filtering

默认情况下,会为每个文档(如果存储了)都返回_source字段。就像get api一样,你可以通过_source参数只取回_source部分字段(或者都不要)。你也可以使用url参数_source_source_include_source_exclude来指定默认值,当每个文档没有说明时,就使用这些默认值。

例如:

GET _mget
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "1",
            "_source" : false
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "2",
            "_source" : ["field3", "field4"]
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "3",
            "_source" : {
                "include": ["user"],
                "exclude": ["user.location"]
            }
        }
    ]
}

//我自己的语句
{
    "docs" : [
        {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "1",
            "_source" : false
        },
        {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "2",
            "_source" : ["age", "gender"]
        },
        {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "3",
            "_source" : {
                "include": ["firstname"],
                "exclude": ["address"]//这个地方我没有相关数据,模拟不出来
            }
        }
    ]
}

结果:

{
  "docs": [
    {
      "_index": "bank",
      "_type": "account",
      "_id": "1",
      "_version": 1,
      "found": true
    },
    {
      "_index": "bank",
      "_type": "account",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
        "gender": "F",
        "age": 22
      }
    },
    {
      "_index": "bank",
      "_type": "account",
      "_id": "3",
      "_version": 1,
      "found": true,
      "_source": {
        "firstname": "Levine"
      }
    }
  ]
}

Fields

可以指定具体的存储字段,以便每个文档将其取回,这个和get api中的stored_fields是类似的。例如:

GET _mget
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "1",
            "stored_fields" : ["field1", "field2"]
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "2",
            "stored_fields" : ["field3", "field4"]
        }
    ]
}
//以下是我自己语句
{
    "docs" : [
        {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "1",
            "stored_fields" : ["firstname", "lastname"]
        },
        {
            "_index" : "bank",
            "_type" : "account",
            "_id" : "2",
            "stored_fields" : ["age", "address"]
        }
    ]
}

由于我没有store相关字段,所以显示为:

{
  "docs": [
    {
      "_index": "bank",
      "_type": "account",
      "_id": "1",
      "_version": 1,
      "found": true
    },
    {
      "_index": "bank",
      "_type": "account",
      "_id": "2",
      "_version": 1,
      "found": true
    }
  ]
}

另外,你可以在查询字符串中指定stored_fields参数,默认会应用到所有的文档中。

GET test/type/_mget?stored_fields=field1,field2
{
    "docs" : [
        {
            "_id" : "1" ①
        },
        {
            "_id" : "2",
            "stored_fields" : ["field3", "field4"] ②
        }
    ]
}

①返回field1field2
②返回field3field4

Routing

你也可以指定路由的值作为参数:

GET _mget?routing=key1
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "1",
            "_routing" : "key2"
        },
        {
            "_index" : "test",
            "_type" : "type",
            "_id" : "2"
        }
    ]
}

在这个例子中,文档test/type/2会从路由keykey1的相应的分片中获取到,但是文档test/type/1会从路由keykey2的相应的分片中获取到。

Security

参考:URL-based access control

官网地址:
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-multi-get.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值