在ElasticSearch中按日期排序,按天写入次数排序(辅助排序)



我的索引有两个字段-

更新了作为日期字段的
  • (例如"2020-01-04T05:00:06.870000Z"(
  • numWrites是浮点

我需要一个查询-

  • 按日期(YYYY-mm-dd(降序排序
  • 对于每天,按numWrites降序排序

样本数据:

"_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
}
}

以下查询按日期排序但是,在同一天内,它没有按预期的numWrites排序,因为同一天的时间戳不同。如何从YYYY-mm-dd中提取日期,然后在一天内按numWrites排序?

查询:

{
"sort":[
{"updated": {"order" : "desc"}},
{"numWrites": {"order" : "desc"}}
]
} 

结果:

"_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
}

如果我已经很好地理解了你的问题,你可以使用子聚合先按日期排序,然后按每一天的numWrites排序。这里有一个建议的解决方案:

"size": 0,
"aggs": {
"sort_by_date": {
"terms": {
"field": "updated",
"order": {
"_key": "desc"
}
},
"aggs": {
"sort_by_numWrites_per day": {
"terms": {
"field": "numWrites",
"order": {
"_key": "desc"
}
}
}
}
}
}

我尝试在同一日期添加两个numWrites。这是我的样本索引:

{
"_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
}

我有两个numWrites用于日期"2019-12-04T04:00:06.870Z";我得到的结果是:

{
"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
}

相关内容

  • 没有找到相关文章

最新更新