Monday, 19 October 2015

Elasticsearch: _mget: Retrieve multiple documents

As I already said _index, _type, _id are used to uniquely identify a document. If you know these fields for the documents that you want to retrieve, you can use _mget endpoint to retrieve all the documents at once.

For example,
GET /_mget
{
  "docs":[
    {
      "_index" : "xyz",
      "_type" : "employees",
      "_id" : 1
    },
    {
      "_index" : "xyz",
      "_type" : "products",
      "_id" : 1
    }
    ]
}


Above request return following two documents.
{
   "docs": [
      {
         "_index": "xyz",
         "_type": "employees",
         "_id": "1",
         "_version": 4,
         "found": true,
         "_source": {
            "firstName": "Pradeep",
            "lastName": "Deshala",
            "hobbies": [
               "Mobile App development",
               "Gaming"
            ],
            "age": 30,
            "city": "Bangalore"
         }
      },
      {
         "_index": "xyz",
         "_type": "products",
         "_id": "1",
         "_version": 2,
         "found": true,
         "_source": {
            "price": "2560.19",
            "items_available": "131"
         }
      }
   ]
}
If you specify index in query path, You no need to specify the index while retrieving.

GET /xyz/_mget
{
  "docs":[
    {
      "_type" : "employees",
      "_id" : "2"
    },
    {
      "_type" : "products",
      "_id" : "1"
    }
  ]
}
Following is the response for above query.

{
   "docs": [
      {
         "_index": "xyz",
         "_type": "employees",
         "_id": "2",
         "_version": 1,
         "found": true,
         "_source": {
            "firstName": "Sankalp",
            "lastName": "Dubey",
            "hobbies": [
               "Shopping",
               "Swimming",
               "Reading books"
            ],
            "age": 32
         }
      },
      {
         "_index": "xyz",
         "_type": "products",
         "_id": "1",
         "_version": 2,
         "found": true,
         "_source": {
            "price": "2560.19",
            "items_available": "131"
         }
      }
   ]
}
Suppose if you want to retrieve documents from same index, and type, you just specify all documents in ids array.

GET /xyz/employees/_mget
{
 "ids" :["3", "1", "2"]
}
Above query return following documents.

{
   "docs": [
      {
         "_index": "xyz",
         "_type": "employees",
         "_id": "3",
         "_version": 1,
         "found": true,
         "_source": {
            "firstName": "Arpan",
            "lastName": "Debroy",
            "hobbies": [
               "Tattoos",
               "Fencing",
               "Shopping"
            ],
            "age": 28
         }
      },
      {
         "_index": "xyz",
         "_type": "employees",
         "_id": "1",
         "_version": 4,
         "found": true,
         "_source": {
            "firstName": "Pradeep",
            "lastName": "Deshala",
            "hobbies": [
               "Mobile App development",
               "Gaming"
            ],
            "age": 30,
            "city": "Bangalore"
         }
      },
      {
         "_index": "xyz",
         "_type": "employees",
         "_id": "2",
         "_version": 1,
         "found": true,
         "_source": {
            "firstName": "Sankalp",
            "lastName": "Dubey",
            "hobbies": [
               "Shopping",
               "Swimming",
               "Reading books"
            ],
            "age": 32
         }
      }
   ]
}
You can override the type at run time, for example.
GET /xyz/employees/_mget
{
 "docs":[
   {
     "_id" : "3"
   },
   {
     "_type" : "products",
     "_id" : 1
   }
   ]
}

In the above request, second element of docs array specifying  type externally. Response like below.
{
   "docs": [
      {
         "_index": "xyz",
         "_type": "employees",
         "_id": "3",
         "_version": 1,
         "found": true,
         "_source": {
            "firstName": "Arpan",
            "lastName": "Debroy",
            "hobbies": [
               "Tattoos",
               "Fencing",
               "Shopping"
            ],
            "age": 28
         }
      },
      {
         "_index": "xyz",
         "_type": "products",
         "_id": "1",
         "_version": 2,
         "found": true,
         "_source": {
            "price": "2560.19",
            "items_available": "131"
         }
      }
   ]
}
What if document is not found?

found attribute of the document set to “false”.
GET /xyz/employees/_mget
{
 "docs":[
   {
     "_id" : "30"
   },
   {
     "_type" : "products",
     "_id" : 1
   }
   ]
}


You will get following response.

{
   "docs": [
      {
         "_index": "xyz",
         "_type": "employees",
         "_id": "30",
         "found": false
      },
      {
         "_index": "xyz",
         "_type": "products",
         "_id": "1",
         "_version": 2,
         "found": true,
         "_source": {
            "price": "2560.19",
            "items_available": "131"
         }
      }
   ]
}







Prevoius                                                 Next                                                 Home

No comments:

Post a Comment