像SQL中的“ distint count”一样的elasticsearch搜索?

时间:2019-04-19 07:45:35

标签: elasticsearch

我将以下数据放入了弹性搜索。

POST movies/movie
{
    "title": "Apocalypse Now",
    "director": "Francis Ford Coppola",
    "year": 1979,
    "genres": ["Drama", "War", "Foo"]
}

POST movies/movie
{
    "title": "Apocalypse Now",
    "director": "Francis Ford Coppola",
    "year": 1979,
    "genres": ["Drama", "War", "Foo", "Bar"]
}

POST movies/movie
{
    "title": "Apocalypse Now",
    "director": "Francis Ford Coppola",
    "year": 1979,
    "genres": ["Drama", "Comic", "Bar"]
}

我想得到以下结果。

"Drama" : 3
"War" : 2
"Foo" : 2
"Bar" : 2
"Comic" : 1

如何获得这些结果? 感谢您为解决此问题所提供的帮助。 预先感谢。

1 个答案:

答案 0 :(得分:1)

您可以使用terms aggregation,如下所示:

POST movies/_search
{
  "size": 0,
  "aggs": {
    "counts": {
      "terms": {
        "field": "genres.keyword",
        "size": 20
      }
    }
  }
}