ElasticSearch With a REST API


ElasticSearch is an open source search engine with a REST API for querying written words. Elasticsearch is a standalone database server, written in Java. It is built on top of Apache Lucene. It uses JSON over HTTP and is suitable for programming languages other than Java as well. The first version of ElasticSearch was released in February 2010 by Shay Banon. ElasticSearch exposes a REST API to interact with data using HTTP verbs. To retrieve information, we use a GET command. To create or update records, you can use a PUT or POST command.

For the complete Feature List please visit https://www.elastic.co/products/elasticsearch.

We can download the ElasticSearch from http://www.elasticsearch.org/download/ . Please note that the Java runtime should be installed and the JAVA_HOME environment variable must first be set as a pre-requisite. To run the server we need to run elasticsearch.bat located in the bin folder of the ElasticSearch folder.

If we don't have a Java runtime installed or not correctly configured then we'll not see output like the one above but instead a message saying "JAVA_HOME environment variable must be set!”. Then we need to download and install Java. Now open the elasticsearch.in.bat file in Notepad and remove the line highlighted in the following image.

Now change the “%JAVA_HOME%” to your Java JDK path (which is C:\Program Files\Java\jdk1.7.0_67\jre\bin\java in my case) in elasticsearch.bat file. Once ElasticSearch is up and running, open a browser and type “http://localhost:9200/” in the address bar.

Now we can play with ElasticSearch using RESTClient. To do this we need to first populate some data in an index. An index is a collection of documents of similar data. For example, we can have an index for Employees data. An index is identified by a name that is used when performing index, search, update and delete operations against the documents in it.

Now we would see some CRUD operations. We will populate an index called “employees”. Please note that ES supports index name in small case only. We can use a PUT or POST method to create an index. If the index does not exist then it would be created. Similarly if a document does not exist in the index then it would be inserted and if it exists then it would e updated. We need to use http://localhost:9200/<_index>/<_type>/[<id>] to create and update an index and document.[_index] and [_ type] are required whereas the id part is optional.

Here we have created an index “employees” and passed JSON data for one employee. If you note the response then we see it shows the version of document and “created: true”. If we change any data within the JSON and post the data with the same URL then the version would change and “created” would be false.

Similarly I have inserted the following JSON data as well.

  1.  {  
  2.   "EmployeeId""2",  
  3.   "FirstName""Karunesh",  
  4.   "LastName""Singh",  
  5.   "Department""PMO",  
  6.   "Salary""50000"  
  7. }  
  9. {  
  10.   "EmployeeId""3",  
  11.   "FirstName""Swati",  
  12.   "LastName""Kumari",  
  13.   "Department""HR",  
  14.   "Salary""15000"  
  15. }  
  17. {  
  18.   "EmployeeId""4",  
  19.   "FirstName""Rahul",  
  20.   "LastName""Dalal",  
  21.   "Department""MST",  
  22.   "Salary""30000"  
  23. }  
  25. {  
  26.   "EmployeeId""5",  
  27.   "FirstName""Rajib",  
  28.   "LastName""Ganguly",  
  29.   "Department""MST",  
  30.   "Salary""20000"  
  31. }  
  33. {  
  34.   "EmployeeId""6",  
  35.   "FirstName""Amit",  
  36.   "LastName""Mohod",  
  37.   "Department""CTS",  
  38.   "Salary""22000"  
  39. }  
We can read a document from an index with a simple “GET” if we have “ID” for that as in the following.

If we need to remove a single document from the index by ID we must use the same URL with the HTTP method DELETE.

Now we would see how to search the document with ES. For this ES has an endpoint as “_search”. If we need to get all the documents of a specific type within an index then we must call: http://localhost:9200//<_index>/<_type>/_search with the HTTP method GET
In our case it is:
http://localhost:9200/employees/employee/_search. It will give us all the documents.

In order to apply a filter to make a more useful search request we need to supply a request body with a query with the HTTP method POST. The request body should be a JSON object. Suppose we need to search the document with word “Amit”, then our JSON object should be:
  1. {  
  2.     "query"
  3.     {  
  4.         "query_string": { "query""Amit" }  
  5.     }  
  6. }  
These JSON uses ElasticSearch's query DSL.

If we want to search “Amit” in the first name then we need to use.
  1. {  
  2.     "query":   
  3.     {  
  4.         "query_string": { "query""FirstName:Amit" }  
  5.     }  
  6. }  
We can combine multiple filters or query strings to make some useful search. I will try to explain these options in the next article.

Up Next
    Ebook Download
    View all
    View all