Retrieving Documents by ID

Published on January 17, 2016 by

Now that we have added and manipulated documents within our Elasticsearch index, let’s now see how we can retrieve data from it. The easiest way of retrieving data in Elasticsearch, is by retrieving documents by ID. You actually saw this in the previous article if you were paying attention (gotcha!).

Simply specify the ID of a document in the request URI, like below.

GET /ecommerce/product/1002

Here are the results:

{
  "_index": "ecommerce",
  "_type": "product",
  "_id": "1002",
  "_version": 3,
  "found": true,
  "_source": {
    "name": "Why Elasticsearch is Awesome",
    "price": "50.00",
    "description": "This book is all about Elasticsearch!",
    "status": "active",
    "quantity": 10,
    "categories": [
      {
        "name": "Software"
      }
    ],
    "tags": [
      "elasticsearch",
      "programming"
    ]
  }
}

Could it be any easier? Notice that the meta fields _index, _type, _id and _version are part of the result. The _source property contains the document itself.

Featured

Learn Elasticsearch today!

Take an online course and become an Elasticsearch champion!

Here is what you will learn:

  • The architecture of Elasticsearch
  • Mappings and analyzers
  • Many kinds of search queries (simple and advanced alike)
  • Aggregations, stemming, auto-completion, pagination, filters, fuzzy searches, etc.
  • ... and much more!
Elasticsearch logo
Author avatar
Bo Andersen

About the Author

I am a back-end web developer with a passion for open source technologies. I have been a PHP developer for many years, and also have experience with Java and Spring Framework. I currently work full time as a lead developer. Apart from that, I also spend time on making online courses, so be sure to check those out!

Leave a Reply

Your e-mail address will not be published.