[英]Elasticsearch: sort by max value in array
Let's say I have 2 documents: 假设我有2个文档:
{
"id": "1234",
"things": [
{
"datetime": "2016-01-01T12:00:00+03:00"
},
{
"datetime": "2016-01-06T12:00:00+03:00"
},
{
"datetime": "2100-01-01T12:00:00+03:00"
}
]
}
and 和
{
"id": "5678",
"things": [
{
"datetime": "2016-01-03T12:00:00+03:00"
},
{
"datetime": "2100-01-06T12:00:00+03:00"
}
]
}
things.datetime is mapped as { "type": "date", "format": "date_time_no_millis" }. Things.datetime映射为{“ type”:“ date”,“ format”:“ date_time_no_millis”}。
I want to sort these documents based on the latest things.datetime value that is not in the future. 我想根据将来不会出现的最新Things.datetime值对这些文档进行排序。
Ie sorted by simply the max things.datetime would use the dates 2100-01-01T12:00:00+03:00 and 2100-01-06T12:00:00+03:00. 即简单地按最大的东西排序。datetime将使用日期2100-01-01T12:00:00 + 03:00和2100-01-06T12:00:00 + 03:00。 I want the sorting to be based on the values 2016-01-06T12:00:00+03:00 and 2016-01-03T12:00:00+03:00.
我希望排序基于值2016-01-06T12:00:00 + 03:00和2016-01-03T12:00:00 + 03:00。
How can I achieve this, using ElasticSearch 2.x? 如何使用ElasticSearch 2.x实现此目的?
I've tried: 我试过了:
"sort": {
"things.datetime": {
"order": "desc",
"mode": "max"
}
}
But that doesn't seem to sort even by the 2100 dates. 但这似乎并没有按2100年的日期排序。
I also tried to use nested_filter like so: 我也试图像这样使用nested_filter:
"sort": {
"things.datetime": {
"order": "desc",
"mode": "max",
"nested_filter": {
"range": {
"things.datetime": { "lte": "now" }
}
}
}
}
But it doesn't work as I'd expect. 但这并没有我期望的那样。
Also the "sort" value in the response is a negative number. 响应中的“ sort”值也是负数。 So for a document with dates:
因此,对于带有日期的文档:
"2015-10-24T05:50:00+03:00",
"2015-10-26T22:05:48+02:00",
"2015-10-24T08:05:43+03:00"
gets a negative sort value: 得到一个负的排序值:
"sort": [
-9223372036854775808
]
The correct way to achieve this seems to be: 实现此目的的正确方法似乎是:
"sort": {
"things.datetime": {
"order": "desc",
"mode": "max",
"nested_path": "things",
"nested_filter": {
"range": {
"things.datetime": { "lte": "now" }
}
}
}
}
When there are no more dates left after the nested_filter, the sort value becomes a negative number to ensure the correct order. 当nested_filter之后没有剩余的日期时,排序值将变为负数,以确保顺序正确。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.