elasticsearch Elastic Search>日期直方图聚合并按相同日期排序

lnvxswe2  于 2023-04-20  发布在  ElasticSearch
关注(0)|答案(1)|浏览(178)

下面是我的示例文档

{
          "totalPrice": 122.24173769217346,
          "createdAt": "2023-07-24T06:25:44.961",
          "domain": "mystore.wix.com"
        }

我的要求是检索记录,其中域必须匹配mystore.wix.com,并且应该按日期createdAt分组,totalPrice总和聚合,并按相同的createdAt降序排序。我希望聚合的大小为10,即只需要返回10个桶,不需要更多。
我试过查询,

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "domain": "mystore.wix.com"
          }
        }
      ]
    }
  },
  "aggs": {
    "groupByDate": {
      "date_histogram": {
        "field": "createdAt",
        "calendar_interval": "1d",
        "min_doc_count": 1,
        "order": {
          "_key": "desc"
        }
      },
        "aggs": {
          "totalPrice": {
          "sum": {
            "field": "totalPrice",
            "script": {"source": "Math.round(doc['totalPrice'].value * 1000.0)/1000.0"}
        }
    }
      }
    }
  }
}

但是我不知道如何将聚合大小限制为10。因此我得到了一个大的结果。我在这里错过了什么?

nlejzf6q

nlejzf6q1#

我不认为将弹性限制在使用的桶的数量上是可能的。
一种选择是使用分页参数并只获取第一页。
默认情况下,搜索将返回前10个匹配的结果。要在更大的结果集中分页,可以使用搜索API的from和size参数。from参数定义要跳过的结果数,默认值为0。size参数是要返回的最大结果数。这两个参数一起定义了一页结果。
https://www.elastic.co/guide/en/elasticsearch/reference/current/paginate-search-results.html#paginate-search-results

相关问题