Getting Started with ElasticSearch : Creating Indices, Inserting Values and Retrieving Data
Elastic is a search server based on lucene and provides a distributable full text search engine that’s accessible through a restful interface.
ElasticSearch is schema less, and uses JSON instead of XML. It is open source and built in Java, which means you can run ElasticSearch on any platform, as Java is platform independent.
ElasticSearch is a document based store. It is an alternative to traditional document stores, so it can be used to replace other document stores like MongoDB or RavenDB.
Fast and Scalable:
ElasticSearch is incredibly fast when it comes to searching, so if your current document search is not giving you the read performance or is not scaling as well as you would want it to like, elasticsearch is highly scalable.
For all those coming from traditional MySQL database, here is a table comparing elasticsearch terminology with traditional relational database terminology:
A table comparing terminologies.
To get started, you need to download elasticsearch from this link, unzip the zipped file in a folder where you want to place elasticsearch.
To run this, open a command window, go to bin folder, and type elasticsearch to run it. Make sure, you have JAVA_HOME environment variable defined.
Interacting with ElasticSearch:
To check whether ElasticSearch has correctly installed and started locally, use the following URL in browser :
It should show you an output like:
Restful APIs are used to interact with ElasticSearch. The generic pattern used to make a RESTful call is as shown below:
REST API Format : http://host:port/[index]/[type]/[_action/id]
HTTP Methods used: GET, POST, PUT, DELETE
- To get a list of all available indices in your elasticsearch, use the following URL :
- To get the status of an index (say, company), use the following URL:
The first part (localhost) is denotes the host (server) where your ElasticSearch is hosted, and the default port of 9200.
The second part (company) is index , followed by the (employee) type name, followed by (_search) action.
ElasticSearch lets you use HTTP methods such as GETs, POSTs, DELETEs, and PUTs along with a payload that would be in a JSON structure.
Let's take a look at how to create an index, insert data into it and then retrieve data from elasticsearch.
Creating an Index:
Once you run the above command , this is the Response received:
The above command creates an index named, Company with type named employee having fields age, experienceInYears and name.
"age" : 45,
"experienceInYears" : 10
To read all records of a type within an index, use the following commands:
The above URLs, under our vehicles index, we have got document types such as company, car, bike and truck. Each one of these documents will contain specific data related to either car, bike or truck.
- One can perform lot of other operations using REST APIs on elasticsearch, such as:
- Checking the status of ElasticSearch Server,
- Performing CRUD (Create, Read, Update and Delete) and Search Operations against your indexes.
- Perform operations like paging, sorting, filtering, scripting, faceting, aggregations, etc.
ElasticSearch will help you resolve any search optimization problems you might have in your existing applications. ElasticSearch is useful for giving your users get a quality search experience and also let them find what they are really looking for.
My Name is Ashish @ashish_fagna. I am a software developer . If you enjoyed this article, please recommend and share it! Thanks for your time.
You can also contact me on ashish [dot] fagna [at] gmail.com