gpt4 book ai didi

elasticsearch - Elasticsearch 查询,返回每个类别的前3个

转载 作者:行者123 更新时间:2023-12-02 23:12:35 24 4
gpt4 key购买 nike

我想编写一个 flex 查询,在该查询中应返回Sidney Sheldon和John Gresham等2位作者的前三本书。

该文档包含字段ID,标题,作者,no_copies_sold,release_Date ...

我想在单个查询中完成此操作,而不是为Sidney Sheldon和John Gresham分别查询2次。

任何帮助深表感谢。

最佳答案

首先过滤作者,使其仅定位到您想要的文档。
按作者汇总后,再获取前3个no_copies_sold(= top_book)并询问标题(如果需要,在查询中添加其他字段)。

{
"size": 0,
"query": {
"terms": {
"author": [
"Sidney Sheldon",
"John Gresham"
]
}
},
"aggs": {
"author_agg": {
"terms": {
"field": "author"
},
"aggs": {
"hits": {
"top_hits": {
"sort": [
{
"no_copies_sold": {
"order": "desc"
}
}
],
"_source": [
"title"
],
"size": 3
}
}
}
}
}
}

关于elasticsearch - Elasticsearch 查询,返回每个类别的前3个,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58691245/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com