Given a mapping describing an event which contains the following nested field:
...
"timings": {
"type": "nested",
"properties": {
"begin": {
"type": "date"
},
"end": {
"type": "date"
}
}
},
...
The following sort makes it possible to show the nearest upcoming events first, followed by the nearest past events:
"sort" : [{
"timings.end" : {
"mode": "min",
"order": "asc",
"nested": {
"path": "timings",
"filter": {
"range": {
"timings.end" : { "gte": "now" }
}
}
}
}
}, {
"_search_last_timing": { "order": "desc" }
}, {
"uid": { "order": "asc" }
}]
The first sort is done according to the smallest upcoming timings. As long as the indexed documents contain upcoming timings, the provided sort values can be given to the search_after to fetch next results just fine.
The problem arises as soon as the first past event shows up (one containing only past timings). When this occurs, the first provided sort key shows a value that looks like the maximum possible value for the date type (epoch_millis): An example: [9223372036854776000,1608327000000,62780895]
Providing the sort values with that maximum to a search_after
will cause the index to throw the following error message:
{
"error": {
"root_cause": [
{
"type": "parse_exception",
"reason": "failed to parse date field [9223372036854776000] with format [strict_date_optional_time||epoch_millis]: [failed to parse date field [9223372036854776000] with format [strict_date_optional_time||epoch_millis]]"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "test",
"node": "fylQJH2MRluyF0Yvxx2row",
"reason": {
"type": "parse_exception",
"reason": "failed to parse date field [9223372036854776000] with format [strict_date_optional_time||epoch_millis]: [failed to parse date field [9223372036854776000] with format [strict_date_optional_time||epoch_millis]]",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "failed to parse date field [9223372036854776000] with format [strict_date_optional_time||epoch_millis]",
"caused_by": {
"type": "date_time_parse_exception",
"reason": "Failed to parse with all enclosed parsers"
}
}
}
}
]
},
"status": 400
}