弹性搜索查询,返回每个类别的前3个

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

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

我想在一次查询中完成此操作,而不是对Sidney Sheldon和John Gresham分别查询两次。

非常感谢您的帮助。

zhu033033 回答:弹性搜索查询,返回每个类别的前3个

首先对作者进行过滤,以仅定位所需的文档。 按作者汇总后,再获取前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
          }
        }
      }
    }
  }
}
本文链接:https://www.f2er.com/3167625.html

大家都在问