is it possible to have aggregations with a random order? It seems that there is only asc or desc possible?
{
\"aggs\" : {
\"genders\" : {
\"terms\" :
Nope, according to the official documentation, terms aggregations are always sorted one way or another. If not specified, aggregated terms are sorted by count descending.
If at all needed, you can always shuffle the results on the client-side in some random order, though.
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.
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"
}
}
}
}
}
}
}
}