Two things regarding the mapping:
- There’s an
address
field there that’s not found in your actual documents. Remove it. - Your dates should be mapped correctly using the
date
datatype.
A correct mapping would look like this:
{
"properties":{
"end_date":{
"type":"date",
"format":"yyyy-MM-dd"
},
"start_date":{
"type":"date",
"format":"yyyy-MM-dd"
},
//...other properties
}
}
Once you get the mapping right, this query looks for all non-expired ads w/ a true status and sorts by the longest running:
{
"query": {
"bool": {
"must": [
{
"range": {
"end_date": {
"gt": "now"
}
}
},
{
"term": {
"status": {
"value": true
}
}
}
]
}
},
"sort": [
{
"end_date": {
"order": "desc"
}
}
]
}
Alternatively, if you’re looking for the expired ones, change gt
to lt
which stands for less-than.
0
solved Elasticsearch bool query sort by date if status is true