CoreJavaTutorials
Contribute Us | Ask Question |

Most Useful Queries Of Elastic Search

Elastic Search is an open source ,powerful and very fast search engine used by many top brands in the world like wikipedia,ebay,Linkedin for their search engine like ebay used for product search etc.

Elastic allow to put data as index into various nodes and allow us to fetch data from documents in seconds.With the help of elastic we take full use of cluster system to access data in parallel.
To know more about elastic you should go to elastic tutorials.

Here we provide some useful queries to manipulate and fetch data or document from index(database in term of RDBMS).
The queries are follows:

To get detail of all index

It is used get list of all indices that are put in elastic with detail like storage,health of an index and more .
GET _cat/indices?v

To show the index structure like table desc

The below command us get structure of employee index.
GET employee/_mapping

To fetch all the record of a index

To fetch all the documents of a particular index we use the following command:
GET employee/_search

Fetch all documents from a index on single field(column) particular condition

To fetch all the documents from a particular index on particular condition like where clause in RDBMS.
In below command we get all documents where gender is female from employee index.We use the following command:
GET employee/_search { "query":{ "match" : { "gender": "female" } } }

Fetch all the documents from an index on multiple fields(columns) using OR condition

In below command we get all documents where name is Aman age is 24 and gender is male from employee index.
Here we use OR condition means if any condition match from three fields to an index ,that should be fetch from an Index . We use the following command: GET employee/_search {
"query": {
"filtered": {
"filter": {
"bool": {
"should": [
{"term": { "name": "aman"}},
{"term": {"gender": "male"}},
{"term": {"age": "24"}}
]
}
}
}
}
}
Here Bool is used to give condition and should use for OR Condition.

Fetch all the documents from an index on multiple fields(columns) using AND condition

In below command we get all documents where name is Aman age is 24 and gender is male from employee index.
Here we use AND condition means all the condition from the three fields must be match to an index . We use the following command: GET employee/_search {
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{"term": { "name": "aman"}},
{"term": {"gender": "male"}},
{"term": {"age": "24"}}
]
}
}
}
}
}
Here Bool is used to give condition and Must use for AND Condition.

To find particular string in mutiple fields or single field

Here we get all document from the index where a string is match on given field or fields.
In below command we check ram in fathername and name field(column in RDBMS).
GET employee/_search { "query": { "multi_match" : { "query": "ram", "fields": [ "fathername", "name" ] } } }

Count Query

Here we get number of documents in an index.In below command we count the number of document from employee index where gender is female
GET /employee/_count { "query" : { "term" : { "gender": "female" } } }

Order By

Here we sort the data in Ascending Order.
GET employee/_search { "sort" : [ { "name" : "asc" }, { "dob" : "asc" }, "_score" ], "query" : { "term" : { "name": "raju" } } }

Here we sort the data in Descending Order
GET employee/_search { "sort" : [ { "name" : "desc" }, { "dob" : "desc" }, "_score" ], "query" : { "term" : { "name": "raju" } } }

Limit

Here we limit the number of document to be fetch from an Index.
In from we give the start range which is 0 and in size we give the end range which is 100 in below command
GET employee/_search { "from" : 0, "size" : 100, "query" : { "term" : { "gender": "female" } } }