Elastic Search: aggregation random order?

后端 未结 3 1236
借酒劲吻你
借酒劲吻你 2021-01-27 15:21

is it possible to have aggregations with a random order? It seems that there is only asc or desc possible?

{
\"aggs\" : {
    \"genders\" : {
        \"terms\" :         


        
3条回答
  •  暖寄归人
    2021-01-27 15:39

    Yes it is possible. Did it this way:

    {
      "aggs": {
        "genders": {
          "terms": {
            "field": "gender",
            "size": 10,
            "order": {
              "rnd.max": "asc"
            }
          },
          "aggs": {
            "rnd": {
              "stats": {
                "script": "Math.random()"
              }
            }
          }
        }
      }
    }
    

    I'm using the stats-aggregation here as my random-number-generator, as it has a script-property, but any other metrics-aggregation with a script-property should do it also. You can play with the size-property of the terms-aggregation to control the number of buckets returned. The smaller the value is, the faster the aggregation runs.

提交回复
热议问题