[英]Sort by date and number of writes by day in ElasticSearch (Secondary sort)
My index has two fields -我的索引有两个字段 -
I need a query that -我需要一个查询 -
Sample data :样本数据 :
"_source": {
"updated": "2020-01-04T05:00:06.870000Z",
"numWrites": 5.0
}
"_source": {
"updated": "2020-01-04T09:00:08.870000Z",
"numWrites": 3.0
}
"_source": {
"updated": "2019-12-04T01:00:06.870000Z",
"numWrites": 15.0
}
"_source": {
"updated": "2019-12-04T04:00:06.870000Z",
"numWrites": 12.0
}
}
The following query sorts by date.以下查询按日期排序。 But, within the same day, it doesn't sort by
numWrites
as expected, as the timestamps within the same day are different .但是,在同一天,它不会按预期按
numWrites
排序,因为同一天的时间戳不同。 How can I extract date in the from YYYY-mm-dd and then sort by numWrites
within a day ?如何从 YYYY-mm-dd 中提取日期,然后在一天内按
numWrites
排序?
Query :询问 :
{
"sort":[
{"updated": {"order" : "desc"}},
{"numWrites": {"order" : "desc"}}
]
}
Results :结果 :
"_source": {
"updated_time": "2020-01-04T09:00:08.870000Z",
"numWrites": 3.0
}
"_source": {
"updated": "2020-01-04T05:00:06.870000Z",
"numWrites": 5.0
}
"_source": {
"updated_time": "2019-12-04T04:00:06.870000Z",
"numWrites": 12.0
}
"_source": {
"updated_time": "2019-12-04T01:00:06.870000Z",
"numWrites": 15.0
}
If I have understood your question well, you can use a sub aggregation to sort by date first, then by numWrites for each of those days.如果我很好地理解了您的问题,您可以使用子聚合首先按日期排序,然后按 numWrites 对每一天进行排序。 Here is a suggested solution:
这是一个建议的解决方案:
"size": 0,
"aggs": {
"sort_by_date": {
"terms": {
"field": "updated",
"order": {
"_key": "desc"
}
},
"aggs": {
"sort_by_numWrites_per day": {
"terms": {
"field": "numWrites",
"order": {
"_key": "desc"
}
}
}
}
}
}
I tried adding two numWrites on the same date.我尝试在同一日期添加两个 numWrites。 Here is my sample index:
这是我的示例索引:
{
"_index" : "test-sort",
"_type" : "_doc",
"_id" : "2kRNZ3QByAa8PXf3rJBC",
"_score" : 1.0,
"_source" : {
"updated" : "2020-01-04T05:00:06.870000Z",
"numWrites" : 5.0
}
},
{
"_index" : "test-sort",
"_type" : "_doc",
"_id" : "20RNZ3QByAa8PXf3rJBC",
"_score" : 1.0,
"_source" : {
"updated" : "2020-01-04T09:00:08.870000Z",
"numWrites" : 3.0
}
},
{
"_index" : "test-sort",
"_type" : "_doc",
"_id" : "3ERNZ3QByAa8PXf3rJBC",
"_score" : 1.0,
"_source" : {
"updated" : "2019-12-04T01:00:06.870000Z",
"numWrites" : 15.0
}
},
{
"_index" : "test-sort",
"_type" : "_doc",
"_id" : "3URNZ3QByAa8PXf3rJBC",
"_score" : 1.0,
"_source" : {
"updated" : "2019-12-04T04:00:06.870000Z",
"numWrites" : 12.0
}
I have two numWrites for the date "2019-12-04T04:00:06.870Z" and the result I obtained is:我有两个日期“2019-12-04T04:00:06.870Z”的numWrites,我得到的结果是:
{
"key" : 1578128408870,
"key_as_string" : "2020-01-04T09:00:08.870Z",
"doc_count" : 1,
"sort_by_numWrites_per day" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 3.0,
"doc_count" : 1
}
]
}
},
{
"key" : 1578114006870,
"key_as_string" : "2020-01-04T05:00:06.870Z",
"doc_count" : 1,
"sort_by_numWrites_per day" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 5.0,
"doc_count" : 1
}
]
}
},
{
"key" : 1575432006870,
"key_as_string" : "2019-12-04T04:00:06.870Z",
"doc_count" : 2,
"sort_by_numWrites_per day" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 12.0,
"doc_count" : 1
},
{
"key" : 10.0,
"doc_count" : 1
}
]
}
},
{
"key" : 1575421206870,
"key_as_string" : "2019-12-04T01:00:06.870Z",
"doc_count" : 1,
"sort_by_numWrites_per day" : {
"doc_count_error_upper_bound" : 0,
"sum_other_doc_count" : 0,
"buckets" : [
{
"key" : 15.0,
"doc_count" : 1
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.