Sample Query with Elasticsearch [6.4]
Elasticsearch is a search engine. Elasticsearch use a full Query DSL (Domain Specific Language) . It’s a base on JSON fomat to define queries.
Query DSL ‘s consisting of two types of clauses:
- clauses
Leaf query clauses is looking for a value in a field, such as the match, term or range queries. - Compound query clauses
Compound query clauses is combining multiple queries,such as the bool or dis_max query
Firstly, we will start with a basic examples (Leaf query) : Suppose we have a hotel node and we will to find all hotels. So,we can use the query sentence as below:
match
Match keyword is used for matching a value in the particular field. This query will return all of data in hotel node.
GET /hotel/_search
{
“query”: { “match_all”: {} }
}
term
Term keyword is used for matching documents that contain the exact term specified.
GET /hotel/_search
{
“query”: { “term”: {“hotel_name” : “Pull man G”} }
}
range
Rang keyword is used with the aggregate data that enables the user to define a set of ranges
GET /hotel/_search
{
“query”: {
“range” : {
“price” : {
“gte” : 1000,
“lte” : 4000,
“boost” : 2.0
}
}
}
}
Secondly, we will gives the basic examples of combining queries. ( Compound query) The sample query sentence is displayed below:
bool
Bool keyword is used for matching documents and combined with other queries
POST /hotel/_search
{
“query”: {
“bool” : {
“must” : {
“term” : {“hotel_name” : “Pullman” }
},
“filter”: {
“term” : { “tag” : “hotel” }
},
“must_not” : {
“range” : {
“price” : { “gte” : 1000, “lte” : 4000 }
}
},
“should” : [
{ “term” : { “tag” : “breakfast” } }
],
“boost” : 1.0
}
}
}
dis_max
Dis_max keyword is used with a union documents query (subqueries),
and calculate scores from each document (maximum score)and then plus a tie breaking increment .
GET /hotel/_search
{
“query”: {
“dis_max” : {
“tie_breaker” : 0.8,
“boost” : 2,
“queries” : [
{
“term” : { “hotel_rang” : 1}
},
{
“term” : { “hotel_rang” : 2}
}
]
}
}
}