Created
February 26, 2020 14:45
-
-
Save VarunVats9/62aba5aa446711101501a362f92453a2 to your computer and use it in GitHub Desktop.
Elasticsearch - date, operator, match_phrase, multiple fields
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
// Add another field | |
PUT /product/_mapping >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"properties": { | |
"date": { | |
"type": "date", | |
"format": "yyyy-MM-dd" | |
} | |
} | |
} | |
{ | |
"acknowledged" : true | |
} | |
POST /product/_update/2 | |
{ | |
"doc": { | |
"date": "2020-01-19" | |
} | |
} | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_version" : 3, | |
"result" : "updated", | |
"_shards" : { | |
"total" : 2, | |
"successful" : 2, | |
"failed" : 0 | |
}, | |
"_seq_no" : 10, | |
"_primary_term" : 1 | |
} | |
// Subtract one day one year | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"range": { | |
"date": { | |
"gte": "2021-01-20||-1y-1d" | |
} | |
} | |
} | |
} | |
{ | |
"took" : 0, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 1, | |
"relation" : "eq" | |
}, | |
"max_score" : 1.0, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 1.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" | |
} | |
} | |
] | |
} | |
} | |
// Subtract by one year and rounding by month | |
// gt Rounds up | |
// gte Rounds down | |
// lt Rounds down | |
// lte Rounds up | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"range": { | |
"date": { | |
"gte": "2021-01-19||-1y/M" | |
} | |
} | |
} | |
} | |
{ | |
"took" : 0, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 1, | |
"relation" : "eq" | |
}, | |
"max_score" : 1.0, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 1.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" | |
} | |
} | |
] | |
} | |
} | |
// Rounds up to the next month (2020-01-31T23:59:59.999) | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"range": { | |
"date": { | |
"gt": "2021-01-19||/M-1y" | |
} | |
} | |
} | |
} | |
{ | |
"took" : 0, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 0, | |
"relation" : "eq" | |
}, | |
"max_score" : null, | |
"hits" : [ ] | |
} | |
} | |
// Round down to the start of the month and subtract one year from now | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"range": { | |
"date": { | |
"gte": "now/M-1y" | |
} | |
} | |
} | |
} | |
{ | |
"took" : 2, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 1, | |
"relation" : "eq" | |
}, | |
"max_score" : 1.0, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 1.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" | |
} | |
} | |
] | |
} | |
} | |
// Match with boolean | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"match": { | |
"name" : { | |
"query": "framework of the world", | |
"operator": "or" | |
} | |
} | |
} | |
} | |
{ | |
"took" : 0, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 2, | |
"relation" : "eq" | |
}, | |
"max_score" : 0.18232156, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 0.18232156, | |
"_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.18232156, | |
"_source" : { | |
"name" : "Django Framework 2: Beginner", | |
"price" : 59.0, | |
"description" : "Learn Django Framework", | |
"status" : "active", | |
"quantity" : 1, | |
"categories" : [ | |
{ | |
"name" : "Software" | |
} | |
], | |
"tags" : [ | |
"python", | |
"Django framework" | |
] | |
} | |
} | |
] | |
} | |
} | |
// Operator search | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"match": { | |
"name" : { | |
"query": "framework of the world", | |
"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" : [ ] | |
} | |
} | |
// Order of terms matters | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"match_phrase": { | |
"name": "spring framework" | |
} | |
} | |
} | |
{ | |
"took" : 9, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 1, | |
"relation" : "eq" | |
}, | |
"max_score" : 0.87546873, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 0.87546873, | |
"_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" | |
} | |
} | |
] | |
} | |
} | |
// Match phrase | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"match_phrase": { | |
"name": "framework spring" | |
} | |
} | |
} | |
{ | |
"took" : 1, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 0, | |
"relation" : "eq" | |
}, | |
"max_score" : null, | |
"hits" : [ ] | |
} | |
} | |
// Search multiple fields | |
GET /product/_search >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>> | |
{ | |
"query": { | |
"multi_match": { | |
"query": "framework", | |
"fields": [ "name", "description", "status" ] | |
} | |
} | |
} | |
{ | |
"took" : 0, | |
"timed_out" : false, | |
"_shards" : { | |
"total" : 1, | |
"successful" : 1, | |
"skipped" : 0, | |
"failed" : 0 | |
}, | |
"hits" : { | |
"total" : { | |
"value" : 2, | |
"relation" : "eq" | |
}, | |
"max_score" : 0.18232156, | |
"hits" : [ | |
{ | |
"_index" : "product", | |
"_type" : "_doc", | |
"_id" : "2", | |
"_score" : 0.18232156, | |
"_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.18232156, | |
"_source" : { | |
"name" : "Django Framework 2: Beginner", | |
"price" : 59.0, | |
"description" : "Learn Django Framework", | |
"status" : "active", | |
"quantity" : 1, | |
"categories" : [ | |
{ | |
"name" : "Software" | |
} | |
], | |
"tags" : [ | |
"python", | |
"Django framework" | |
] | |
} | |
} | |
] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment