Wednesday, 30 September 2015

Retrieve documents from Elastic search

In previous post, I explained how to insert documents into elastic search. It is time to retrieve documents from elastic search.

Use HTTP GET request, by addressing full path(index/type/id) of the document, to get document.

Following request returns employee document 2.
GET /xyz/employees/2


Following is the response for employee 2.

{
   "_index": "xyz",
   "_type": "employees",
   "_id": "2",
   "_version": 1,
   "found": true,
   "_source": {
      "firstName": "Sankalp",
      "lastName": "Dubey",
      "hobbies": [
         "Shopping",
         "Swimming",
         "Reading books"
      ],
      "age": 32
   }
}

As you observe the response, actual data contains in _source field, remaining is some metadata for this document. Don't worry much about metadata, we can discuss this later.

Brief information for metadata is given below.
_index: Where the document lives. An index is similar to database in RDBMS world. When you insert a document into an index, if index is not exists already, it is created at the time of insertion. Index name must be in lowercase, can’t begin with inderscore, must not contain commas.

_type: Same type of documents reside in type. Type is similar to table in RDBMS world. Type name can be in lowercase (or) upper case can’t begin with inderscore, must not contain commas.

_id: The unique identifier for the document in given type, index. While creating document, if you don’t provide _id field, Elasticsearch create it for you. Ids generated by elastic search are 22 characters long

_version: Every document in elastic search has a version number. Whenever you made updates to a document, the version number is incremented.

"found": true
specifies that document is found.  If found is false, then it means document is not found.



Prevoius                                                 Next                                                 Home

No comments:

Post a Comment