在elasticsearch中按日期和写入次数排序(二次排序)

xggvc2p6  于 2021-06-14  发布在  ElasticSearch
关注(0)|答案(1)|浏览(522)

我的索引有两个字段-
更新日期字段(例如“2020-01-04t05:00:06.870000z”)
numwrites是一个float
我需要一个问题-
按日期(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
  }
vjrehmav

vjrehmav1#

如果我已经很好地理解了您的问题,那么您可以使用子聚合,首先按日期排序,然后按这些天的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"
            }
        }
        }
    }
    }
}

我试着在同一天添加两个numwrite。以下是我的示例索引:

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

对于日期“2019-12-04t04:00:06.870z”,我有两个numwrite,得到的结果是:

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

相关问题