Introduction to Elasticsearch with a practical example

Elasticsearch is a full-text search and analytics engine that can be scaled easily for very large-scale usage. It is an open-source tool written in Java language which makes it compatible with most of the platforms. This search engine is used by many large organizations that handle high volume data traffic. We can visualize Elasticsearch as a database with great search capabilities built into it. In this post, we will see some basic concepts and data manipulations in the Elastic search.

Installation

  • Download Elasticsearch from www.elastic.co Based on your OS platform various download options are available.
  • The installation process is described for these platforms there.
  • If you use windows, go to the Elasticsearch home the directory and inside the bin folder. Run the elasticsearch.bat file.
  • This will start the Elastic search server. Browse to http://localhost:9200 will give the server status.

Data operations

The Elasticsearch provides Restful services for data manipulation. We can use Post Man or Fiddler tool for accessing these services. The data stored as JSON documents in the Elastic Search. The server creates indexes on these documents; basically, the index is a collection of different documents. The Indexing happens automatically when we insert data to Elastic Search. Fast searching for the documents is possible because of these indexes.

Insert Operation

The insert operation is pretty straightforward. We just need to POST any JSON document to an arbitrary URL. For example, POST below JSON to http://localhost:9200/company/employee/1

POST, will create a new employee document.

{ "name": "Adam", "number": "1" }

Here is the index created to group all employee type data.

Fetch Document

To GET a particular document sends a GET request to the document URL. A GET to http://localhost:9200/company/employee/1 , will produce below result.

{ "_index": "company", "_type": "employee", "_id": "1", "_version": 1, "found": true, "_source": { "name": "Adam", "number": "1" } }

Search Operation

If we want to search for documents append '_search' after the index. For example, to retrieve all employees below endpoint can be used.

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

{ "took": 73, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 2, "max_score": 1, "hits": [ { "_index": "company", "_type": "employee", "_id": "1", "_score": 1, "_source": { "name": "Adam", "number": "1" } }, { "_index": "company", "_type": "employee", "_id": "2", "_score": 1, "_source": { "name": "Bob", "number": "2" } } :// remaining JSON. : ] } }

Apart from this basic search, Elasticsearch provides a lot of search options against the document contents. We can search based on one field value in a document, do pattern matching or do range bound queries, etc. We need to POST the search requests to the search URL.

For example,

  • To retrieve all employees with name starts 'Bob'. Send below request to search URL.
    {
        "query": {      
            "regexp": {         
                "name": "Bob*"       
            }   
        }
    }
    
  • Similarly, below will fetch all employees with a number greater than 1.
    { 
        "query": {     
            "range": {        
                "number": {           
                    "gte": 1        
                }    
            } 
        }
    }
    
  • The below request can be used for exact text search.
    {  
        "query": {     
            "match": {        
                "name": "Bob"
             }  
        }
    } 
    

Conclusion:

Elastic search is a popular search engine that helps users to do fast text searches on documents. Elastic search provides RESTful web services as well as APIs for data access. It also can be integrated with other tools like Hadoop, Spark, etc. The server can be set up a stand-alone or can be used in a SaaS model. In this tutorial, senior java developer explained how standalone server can be set up and basic data operations on it. Elastic Search has become a good choice as search engine today, in Java enterprise application development.