Getting Started with ElasticSearch : Creating Indices, Inserting Values and Retrieving Data

Introduction to ElasticSearch

Introduction:

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.

Terminology:

For all those coming from traditional MySQL database, here is a table comparing elasticsearch terminology with traditional relational database terminology:

A table comparing terminologies.

A table comparing terminology of MySQL with ElasticSearch

Setup ElasticSearch:

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 :

http://localhost:9200/

It should show you an output like:

Once elasticsearch has started, you can use any Rest API client such as postman or fiddler.

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 :
http://localhost:9200/_cat/indices
  • To get the status of an index (say, company), use the following URL:
http://localhost:9200/company?pretty

The first part (localhost) is denotes the host (server) where your ElasticSearch is hosted, and the default port of 9200.

http://localhost:9200/company/employee/_search

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.

I assume you are using the REST API client postman or fiddler to run the below mentioned RESTful calls.

Let's take a look at how to create an index, insert data into it and then retrieve data from elasticsearch.

Creating an Index:

http://localhost:9200/company

PUT
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"analysis": {
"analyzer": {
"analyzer-name": {
"type": "custom",
"tokenizer": "keyword",
"filter": "lowercase"
}
}
},
"mappings": {
"employee": {
"properties": {
"age": {
"type": "long"
},
"experienceInYears": {
"type": "long"
},
"name": {
"type": "string",
"analyzer": "analyzer-name"
}
}
}
}
}
}

Once you run the above command , this is the Response received:

{
"acknowledged": true
}

The above command creates an index named, Company with type named employee having fields age, experienceInYears and name.

Inserting Data:

http://localhost:9200/company/employee/?_create
POST
{
"name": "Andrew",
"age" : 45,
"experienceInYears" : 10
}

Response :

{
"_index": "company",
"_type": "employee",
"_id": "AVM8D42POa82oxyTa_Pu",
"_version": 1,
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}

Retrieving Data:

To read all records of a type within an index, use the following commands:

http://localhost:9200/company/employee/_search

http://localhost:9200/vehicles/car/_search

http://localhost:9200/vehicles/bike/_search

http://localhost:9200/vehicles/truck/_search

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.

Summary:

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