Intoduction to ElasticSearch with basic queries

Posted By :Dimpal Bhatia |31st January 2019

Index

Documents are collectively called index. For example, you can have an index for College data, another index for Student data, and yet another index for Admission data. An index is always identified by a name and an index name should start with lowercase alphabets and an index name is used to refer to the index while performing indexing, search, update, and delete operations against the documents. We can declare as many indexes as we want.

Elastic Search achieves fast search responses because, it searches an index, instead of searching the text directly. In Elastic Search, Each document is the unit of search and index. An index consists of more than one  Documents, and a Document consists of one or more Fields. Each index has a document type and one index can not have more than one document type.

In the database, a Document corresponds to a table row and a Field corresponds to a table column.

 

Create Index in Kibana Console:-

 


 

POST /employedata/employee
{
    "name":"Dimpal",
    "age":21,
    "dept":"IT",
    "city":"Gurgaon"
    "is_active":true
}

 

Where "employee data" is an index name and employee is the document type
Here document type is similar as table name is a database and each inserted record is known as a document. here you can insert as many documents as you want.

 

 

Different Types of Search Queries in Elastic Search:-

 

1) Full-text Queries:-

Full-text  queries are used to search text fields. It  finds all the documents that match the query string and then the documents are ranked based on relevant score. Relevance score is calculated by how well document match. If you search text "employe" and if one document contains text employe more than other documents then that document is more relevent.

 

2) Term Level Queries:-

Term level queries are used to query enum ,numbers, dates and  status . These queries are similar to supported by a SQL database. For eg, whether a number  falls within a range.

 

 

Search from Kibana Console

 

1) Query to get all documents in index:-

GET /employedata/employe/_search
{
    "query":
    {
        "match_all":
        {
        }
    }
}

 

2) Query to search text from all documents :-

GET /employedata/employe/_search
{
    "query":
    {
        "query_string": {
          "query": "Dimpal"
        }
    }
}

 

This Query will return all documents that contain text "Dimpal"

 

3) Query to search value of particular field:

GET /employedata/employee/_search
{
  "query": {
    "match": {
      "city":"gurgaon" 
    }
  }
 
}

 

4) Query to search particular keyword:-

GET /employedata/employee/_search
{
  "query": {
    "term": {
      "is_active":false
    }
  }
 
}

 

5) Query to search phrases :-

GET /employedata/employee/_search
{
  "query": {
    "match_phrase": {
      "title":"Live Source" 
    }
  }

 

6) Query to Search multiple fields :-

GET /index_name/type/_search
{
  "query": {
    "match_phrase": {
      "query":"Computer",
      "fields":["title","description"]      
    }
  }

 

This Query will search text "Computer" in both title and description fields.

So, This is all about the basics of elastic search

Thanks

 


About Author

Dimpal Bhatia

She is a bright java developer. She has a good knowledge of JavaScript, jQuery, JSP, and ElasticSearch. Her hobbies are playing badminton and reading books

Request For Proposal

[contact-form-7 404 "Not Found"]

Ready to innovate ? Let's get in touch

Chat With Us