Adding Documents to an Index

Published on January 17, 2016 by

To add a document to our Elasticsearch index, let’s open Sense and issue a PUT request. When adding a document to an index, it is optional if you want to supply an ID for the document. If the ID is omitted, then Elasticsearch will generate a random ID and then use it to index the document. In this case, you should use the POST verb instead of PUT. In this example, however, I will supply the ID, so I will issue a PUT request and add the ID in the URL.

Enter the following command and issue the request.

PUT /ecommerce/product/1001
{
  "name": "Zend Framework 2: From Beginner to Professional",
  "price": "40.00",
  "description": "Learn Zend Framework 2 in just a few hours!",
  "status": "active",
  "quantity": 1,
  "categories": [{
    "name": "Software"
  }],
  "tags": ["zend framework", "zf2", "php", "programming"]
}

As you can see, adding a document is pretty straightforward, as all it takes is knowledge of basic JSON.

The document has now been added to the index and is now searchable. Let’s confirm this by opening Kibana and going to the “Discover” tab. Remember that Kibana is located at http://localhost:5601. We should be able to find the document by searching for “zend framework”.

As you can see in the results, the document that we just added shows up, and the matching parts of the document are highlighted in yellow. We are going to get into more details of writing search queries later in this course, so for now, I just wanted to show you that our document does indeed exist within the index.

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.