ElasticSearch — getting started

Elasticsearch is a distributed, open source search and analytics engine for all types of data, including textual, numerical, geospatial, structured, and unstructured. Known for its simple REST APIs, distributed nature, speed, and scalability, Elasticsearch is the central component of the Elastic Stack, a set of open source tools for data ingestion, enrichment, storage, analysis, and visualization.

Simply, Elasticsearch is a standalone database server, written in Java, that takes data in and stores it in a sophisticated format optimized for language based searches. The core of Elasticsearch’s intelligent search engine is Lucene. Lucene is proven, tested, and is widely considered best-of-breed in open-source search software. Elasticsearch provides a more intuitive and simple API than the bare Lucene Java API.

Downloading and running ElasticSearch

Elasticsearch can be downloaded from https://www.elastic.co/downloads/elasticsearch in various formats such as ZIP and TAR.GZ. After downloading the file, unzip the file and direct into bin folder of the unzipped folder and run elasticsearch.bat file in windows operating system. Also you can set the environment variables and run the elasticsearch in command prompt. To set environment variables, add new raw using the path of the bin folder of elasticsearch.

Check if your Server is Running

You can ensure it’s running properly by opening your browser to the URL http://localhost:9200 or run curl http://localhost:9200/ or Invoke-RestMethod http://localhost:9200 with PowerShell. You should see a page that looks something like,

Now your server is running properly on the local host. Therefore, now you can use REST API client such as Postman.

Comparing Elasticsearch terminology with traditional relational database terminology, in Elastcsearch, term Database called as Index, term Table called as Type, term Raw called as Document.

Generic pattern of RESTful call

http://host:port/[index]/[type]/[_action/id]

Indexing

Making a PUT request to the REST API using generic pattern of the RESTful call will generate an Index. Index name will be an arbitrary name. The “id” part of the request is optional. But if you don’t specify the “id”, you should use POST instead of PUT and Elasticsearch will generate “id” for you.

Example index:

Now you are going to create an index named persondata, type named person and id = 1. I have used Postman as my REST API client in this article.

Response of the PUT request will be as follows,

Other than using PUT, you can use POST method to create an index. In this case, you don’t have to specify the “id”. It will generated by Elasticsearch.

Response of the POST request

Getting

Successfully creating the index named persondata, you can test the GET API call to retrieve the data from Elasticsearch. Request should be as follows,

Response of the request will be as follows,

Updating

Update will also handle by PUT request. You have to change the body of the Json object.

Response of the update

Get response after updating the document

Deleting

You can delete the document using DELETE call.

Response of deletion

Get response after deleting the document

In this article, I have explained CRUD operations with respect to Elasticsearch. If you enjoyed this article, please recommend and share it with others.

Leave a comment

Design a site like this with WordPress.com
Get started