【Elasticsearch7.0】文档接口之mget接口

  |   0 评论   |   40 浏览

基本语法

mget接口比较常用的参数有index、type、id,它的作用是根据多个id获取多个文档,返回值中包含doc文档数组,返回结果的顺序与接口入参顺序一致(如果某个特定get出现故障,则在响应中包含一个包含此错误的对象),示例如:

curl -XGET "http://127.0.0.1:9200/_mget?pretty" -H "Content-Type:application/json" -d'
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1"
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2222"
        }
    ]
}'

返回值为:

{
  "docs" : [
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 18,
      "_seq_no" : 99,
      "_primary_term" : 10,
      "found" : true,
      "_source" : {
        "foo" : "bar",
        "extra" : "test",
        "name" : "new_name",
        "counter" : 5,
        "tags" : [
          "red",
          "blue"
        ]
      }
    },
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "2222",
      "found" : false
    }
  ]
}

mget接口中index名可以在url中,如:

curl -XGET "http://127.0.0.1:9200/test/_mget?pretty" -H "Content-Type:application/json" -d'
{
    "docs" : [
        {
            "_type" : "_doc",
            "_id" : "1"
        },
        {
            "_type" : "_doc",
            "_id" : "2222"
        }
    ]
}'

地址中使用type,如:

curl -XGET "http://127.0.0.1:9200/test/_doc/_mget?pretty" -H "Content-Type:application/json" -d'
{
    "docs" : [
        {           
            "_id" : "1"
        },
        {
            "_id" : "2222"
        }
    ]
}'

可以使用更简单的语法,如:

curl -XGET "http://127.0.0.1:9200/test/_doc/_mget?pretty" -H "Content-Type:application/json" -d'
{
    "ids" : ["1", "2"]
}'

自定义结果

默认情况下,所有文档都会返回_source,可以使用_source参数来显示部分属性,可以在url中设置这些_source_source_includes_source_excludes参数,示例如:

curl -XGET "http://127.0.0.1:9200/_mget?pretty" -H "Content-Type:application/json" -d'
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "111",
            "_source" : false
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "_source" : ["counter", "tags"]
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "3",
            "_source" : {
                "include": ["user"],
                "exclude": ["message"]
            }
        }
    ]
}'

返回值为:

{
  "docs" : [
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "111",
      "_version" : 9,
      "_seq_no" : 100,
      "_primary_term" : 10,
      "found" : true
    },
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "1",
      "_version" : 18,
      "_seq_no" : 99,
      "_primary_term" : 10,
      "found" : true,
      "_source" : {
        "counter" : 5,
        "tags" : [
          "red",
          "blue"
        ]
      }
    },
    {
      "_index" : "test",
      "_type" : "_doc",
      "_id" : "3",
      "_version" : 11,
      "_seq_no" : 101,
      "_primary_term" : 10,
      "found" : true,
      "_source" : {
        "user" : "kimchy"
      }
    }
  ]
}

属性

可以为每个要获取的文档指定要检索的特定存储字段,示例如:

curl -XGET "http://127.0.0.1:9200/_mget?pretty" -H "Content-Type:application/json" -d'
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "stored_fields" : ["name", "counter"]
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2",
            "stored_fields" : ["user", "message"]
        }
    ]
}'

也可以在url中指定,示例如:

curl -XGET "http://127.0.0.1:9200/test/_doc/_mget?stored_fields=field1,field2&pretty" -H "Content-Type:application/json" -d'
{
    "docs" : [
        {
            "_id" : "1" 
        },
        {
            "_id" : "2",
            "stored_fields" : ["field3", "field4"] 
        }
    ]
}'

id=1返回field1,field2属性
id=2返回field3,field4属性

路由

可以在参数中指定路由,示例如:

curl -XGET "http://127.0.0.1:9200/_mget?routing=key1&pretty" -H "Content-Type:application/json" -d'
{
    "docs" : [
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "1",
            "routing" : "key2"
        },
        {
            "_index" : "test",
            "_type" : "_doc",
            "_id" : "2"
        }
    ]
}'

id=1被路由到key2,id=2被路由到key1。

部分响应

为了确保快速响应,如果一个或多个碎片失败,mget将返回部分结果。

也可以关注我的公众号:程序之声
图片
关注公众号,领取更多资源

本文为博主原创文章,未经博主允许不得转载。

评论

发表评论