Created
February 26, 2020 17:57
-
-
Save VarunVats9/e3a30a5a34ef4c5660667e6fccda1524 to your computer and use it in GitHub Desktop.
Elasticsearch - must, and, should, must_not, filter, query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MATCH == SHOULD | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"match": { | |
"name": "Spring Title" | |
} | |
} | |
} | |
{ | |
"took" : 2, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 1, | |
"relation" : "eq" | |
}, | |
"max_score" : 0.6931471, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 0.6931471, | |
"_source" : { | |
"name" : "Spring Framework 2: Beginner", | |
"price" : 109.0, | |
"description" : "Learn Spring Framework", | |
"status" : "active", | |
"quantity" : 1, | |
"categories" : [ | |
{ | |
"name" : "Software" | |
} | |
], | |
"tags" : [ | |
"java", | |
"Spring framework" | |
], | |
"date" : "2020-01-19" | |
} | |
} | |
] | |
} | |
} | |
GET /product/_search | |
{ | |
"query": { | |
"bool": { | |
"should": [ | |
{ | |
"term": { | |
"name": "spring" | |
} | |
}, | |
{ | |
"term": { | |
"name": "Title" | |
} | |
} | |
] | |
} | |
} | |
} | |
{ | |
"took" : 2, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 1, | |
"relation" : "eq" | |
}, | |
"max_score" : 0.6931471, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 0.6931471, | |
"_source" : { | |
"name" : "Spring Framework 2: Beginner", | |
"price" : 109.0, | |
"description" : "Learn Spring Framework", | |
"status" : "active", | |
"quantity" : 1, | |
"categories" : [ | |
{ | |
"name" : "Software" | |
} | |
], | |
"tags" : [ | |
"java", | |
"Spring framework" | |
], | |
"date" : "2020-01-19" | |
} | |
} | |
] | |
} | |
} | |
// AND == MUST | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"match": { | |
"name": { | |
"query": "spring title", | |
"operator": "and" | |
} | |
} | |
} | |
} | |
{ | |
"took" : 0, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 0, | |
"relation" : "eq" | |
}, | |
"max_score" : null, | |
"hits" : [ ] | |
} | |
} | |
GET /product/_search | |
{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"term": { | |
"name": "spring" | |
} | |
}, | |
{ | |
"term": { | |
"name": "title" | |
} | |
} | |
] | |
} | |
} | |
} | |
{ | |
"took" : 1, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 0, | |
"relation" : "eq" | |
}, | |
"max_score" : null, | |
"hits" : [ ] | |
} | |
} | |
// Query clause with MUST key | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"match": { | |
"name": "framework" | |
} | |
}, | |
{ | |
"range": { | |
"price": { | |
"lte": 110.00 | |
} | |
} | |
} | |
] | |
} | |
} | |
} | |
{ | |
"took" : 0, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 2, | |
"relation" : "eq" | |
}, | |
"max_score" : 1.1823215, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 1.1823215, | |
"_source" : { | |
"name" : "Spring Framework 2: Beginner", | |
"price" : 109.0, | |
"description" : "Learn Spring Framework", | |
"status" : "active", | |
"quantity" : 1, | |
"categories" : [ | |
{ | |
"name" : "Software" | |
} | |
], | |
"tags" : [ | |
"java", | |
"Spring framework" | |
], | |
"date" : "2020-01-19" | |
} | |
}, | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "3", | |
"_score" : 1.1823215, | |
"_source" : { | |
"name" : "Django Framework 2: Beginner", | |
"price" : 59.0, | |
"description" : "Learn Django Framework", | |
"status" : "active", | |
"quantity" : 1, | |
"categories" : [ | |
{ | |
"name" : "Software" | |
} | |
], | |
"tags" : [ | |
"python", | |
"Django framework" | |
] | |
} | |
} | |
] | |
} | |
} | |
// Range Query [Above] Vs Range Filter [below] | |
// In a query the documents are ranked relative to reach other, | |
// whereas in filter that is not the case, hence it is faster. | |
// [ must and should → query context ] | |
// [ filter and must_not → filter context ] | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"bool": { | |
"must_not": [ | |
{ | |
"match": { | |
"name": "advance" | |
} | |
} | |
], | |
"filter": [ | |
{ | |
"range": { | |
"price": { | |
"lte": 150.00 | |
} | |
} | |
} | |
] | |
} | |
} | |
} | |
{ | |
"took" : 1, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 2, | |
"relation" : "eq" | |
}, | |
"max_score" : 0.0, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 0.0, | |
"_source" : { | |
"name" : "Spring Framework 2: Beginner", | |
"price" : 109.0, | |
"description" : "Learn Spring Framework", | |
"status" : "active", | |
"quantity" : 1, | |
"categories" : [ | |
{ | |
"name" : "Software" | |
} | |
], | |
"tags" : [ | |
"java", | |
"Spring framework" | |
], | |
"date" : "2020-01-19" | |
} | |
}, | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "3", | |
"_score" : 0.0, | |
"_source" : { | |
"name" : "Django Framework 2: Beginner", | |
"price" : 59.0, | |
"description" : "Learn Django Framework", | |
"status" : "active", | |
"quantity" : 1, | |
"categories" : [ | |
{ | |
"name" : "Software" | |
} | |
], | |
"tags" : [ | |
"python", | |
"Django framework" | |
] | |
} | |
} | |
] | |
} | |
} | |
// Should when alone acts as AND, | |
// But with (must | must_not) act as OR | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"bool": { | |
"should": [ | |
{ | |
"match": { | |
"description": "python" | |
} | |
} | |
] | |
} | |
} | |
} | |
{ | |
"took" : 0, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 0, | |
"relation" : "eq" | |
}, | |
"max_score" : null, | |
"hits" : [ ] | |
} | |
} | |
GET /product/_search | |
{ | |
"query": { | |
"bool": { | |
"must": [ | |
{ | |
"match": { | |
"name": "advance" | |
} | |
} | |
], | |
"must_not": [ | |
{ | |
"match": { | |
"status": "active" | |
} | |
} | |
], | |
"should": [ | |
{ | |
"match": { | |
"description": "python" | |
} | |
} | |
], | |
"filter": [ | |
{ | |
"range": { | |
"price": { | |
"gte": 10.00 | |
} | |
} | |
} | |
] | |
} | |
} | |
} | |
{ | |
"took" : 2, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 1, | |
"relation" : "eq" | |
}, | |
"max_score" : 0.9808291, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "1", | |
"_score" : 0.9808291, | |
"_source" : { | |
"name" : "Spring Framework 2: Advance", | |
"price" : 30.0, | |
"description" : "Learn Spring Framework", | |
"status" : "deactive", | |
"quantity" : 1, | |
"categories" : [ | |
{ | |
"name" : "Software" | |
} | |
], | |
"tags" : [ | |
"spring", | |
"spring framework" | |
] | |
} | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment