我得到了以下的ElasticSearch-query,从“cat.id”上分组的每个“类别”中获取10个文档:
"aggs": {
"test": {
"terms": {
"size": 10,
"field": "cat.id"
},
"aggs": {
"top_test_hits": {
"top_hits": {
"_source": {
"includes": [
"id"
]
},
"size": 10
}
}
}
}
}这工作得很好。然而,我似乎找不到一种方法,从每个存储桶中随机抽取10个结果。结果总是一样的。我想从每个存储桶中随机抽取10个项目。我尝试了所有打算用于文档的东西,但似乎都不起作用。
发布于 2018-06-21 05:27:12
正如在this answer中已经建议的,您可以尝试在top_hits聚合中使用随机排序,使用如下_script:
{
"aggs": {
"test": {
"terms": {
"size": 10,
"field": "cat.id"
},
"aggs": {
"top_test_hits": {
"top_hits": {
"_source": {
"includes": [
"id"
]
},
"size": 10,
"sort": {
"_script": {
"type": "number",
"script": {
"lang": "painless",
"source": "(System.currentTimeMillis() + doc['_id'].value).hashCode()"
},
"order": "asc"
}
}
}
}
}
}
}
}随机排序在本question中有广泛的介绍。
希望这能有所帮助!
https://stackoverflow.com/questions/50933851
复制相似问题