我正在使用ElasticSearch开展搜索服务。我尝试按字段值限制结果数。例如,我对“user”字段有“评论”(本例中没有父/子),我想用户将“评论”限制搜索为2。
我尝试了一些聚合和toHits,这给了我很好的结果。
"aggs": {
"agg": {
"terms": {"field": "user","size":10},
"aggs": {
"topHits": { "top_hits": {"size": 2}}
}
}
我得到的结果是:
"aggregations": {
"agg": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 5,
"buckets": [
{
"key": "user1",
"doc_count": 5,
"topHits": {
"hits": {
"total": 5,
"hits": [
{
"_index": "idx",
"_type": "comments",
"_id": "comment1",
"_source": {
"user": "user1",
"headline": "Bla bla 1"
}
},{
"_index": "idx",
"_type": "comments",
"_id": "comment2",
"_source": {
"user": "user1",
"headline": "Bla bla 2"
}
}
]
}
},{
"key": "user2",
"doc_count": 1,
"topHits": {
"hits": {
"total": 1,
"hits": [
{
"_index": "idx",
"_type": "comments",
"_id": "comment3",
"_source": {
"user": "user2",
"headline": "Bla bla 3"
}
}
]
}
}
},
但是,如果我这样做,我无法管理目标“字段”,总计数是用户数(而不是评论数)。
您是否知道一个解决方案(可能是post_filter?脚本?)直接在查询中构建过滤器?