ElasticSerch 通过MultiGet实现批量获取文档

批量获取文档

使用es提供的 Multi Get APi
** Multi GET Api可以通过索引名, 类型名, 文档id 一次得到一个文档集合, 文档可以来自同一个索引库,也可以来自不同的索引库 **

通过Kibana 批量获取

上篇文章中我们学习了如何添加文档。 这次我们添加 id 为 1 , 2 ,3 的用户

然后通过_mget 进行批量获取

GET /_mget
{
     
  "docs":[
      {
     
        "_index":"lib",
        "_type":"user",
        "_id":1
      },
      {
     
        "_index":"lib",
        "_type":"user",
        "_id":2
      },
      {
     
        "_index":"lib",
        "_type":"user",
        "_id":3
      }
    ]
}
返回信息
{
     
  "docs": [
    {
     
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
     
        "name": "liu",
        "sex": "nv",
        "age": 18
      }
    },
    {
     
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
     
        "name": "gao",
        "sex": "nan",
        "age": 18
      }
    },
    {
     
      "_index": "lib",
      "_type": "user",
      "_id": "3",
      "_version": 1,
      "found": true,
      "_source": {
     
        "name": "wang",
        "sex": "nan",
        "age": 18
      }
    }
  ]
}

** 也可以通过_source指定具体的字段 **

GET /_mget
{
     
  "docs":[
      {
     
        "_index":"lib",
        "_type":"user",
        "_id":1,
        "_source":"age"
      } ,
      {
     
        "_index":"lib",
        "_type":"user",
        "_id":2,
        "_source":["name","sex"]      
      }
   ]
}

返回结果

{
     
  "docs": [
    {
     
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
     
        "age": 18
      }
    },
    {
     
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
     
        "sex": "nan",
        "name": "gao"
      }
    }
  ]
}

获取同索引下面同类型的不同文档,简化

GET /lib/user/_mget
{
     
  "docs":[
    {
     
      "_id":1,
      "_source":"age"
    },
    {
     
      "_id":2
    }
  ]
}

返回结果

{
     
  "docs": [
    {
     
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
     
        "age": 18
      }
    },
    {
     
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
     
        "name": "gao",
        "sex": "nan",
        "age": 18
      }
    }
  ]
}

通过ids数组形式来获取

GET /lib/user/_mget
{
     
  "ids":["1","2"]
}

返回结果

{
     
  "docs": [
    {
     
      "_index": "lib",
      "_type": "user",
      "_id": "1",
      "_version": 2,
      "found": true,
      "_source": {
     
        "name": "liu",
        "sex": "nv",
        "age": 18
      }
    },
    {
     
      "_index": "lib",
      "_type": "user",
      "_id": "2",
      "_version": 1,
      "found": true,
      "_source": {
     
        "name": "gao",
        "sex": "nan",
        "age": 18
      }
    }
  ]
}

Kibana 控制台的语句

GET /_mget
{
     
  "docs":[
      {
     
        "_index":"lib",
        "_type":"user",
        "_id":1
      },
      {
     
        "_index":"lib",
        "_type":"user",
        "_id":2
      },
      {
     
        "_index":"lib",
        "_type":"user",
        "_id":3
      }
    ]
}




GET /_mget
{
     
  "docs":[
      {
     
        "_index":"lib",
        "_type":"user",
        "_id":1,
        "_source":"age"
      } ,
      {
     
        "_index":"lib",
        "_type":"user",
        "_id":2,
        "_source":["name","sex"]      
      }
   ]
}

GET /lib/user/_mget
{
     
  "docs":[
    {
     
      "_id":1,
      "_source":"age"
    },
    {
     
      "_id":2
    }
  ]
}

GET /lib/user/_mget
{
     
  "ids":["1","2"]
}

你可能感兴趣的:(ElasticSearch,ElasticSearch,Kibana)