Elastic Search: aggregation random order?

后端 未结 3 1232
借酒劲吻你
借酒劲吻你 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:56

    Yes you can show random results refer to the answer provided here:
    Random order & pagination Elasticsearch

    You can try something like this. I wanted to randomize the results inside buckets so I have used it.

    {
      "aggs": {
        "genders": {
          "terms": {
            "field": "gender"
          },
          "aggs": {
            "search_second": {
              "top_hits": {
                "sort": {
                  "_script": {
                    "script": "Math.random()",
                    "type": "number",
                    "params": {},
                    "order": "asc"
                  }
                }
              }
            }
          }
        }
      }
    } 
    

提交回复
热议问题