0

我有一个像这样的索引:

[
    {
        "Name": "Alex",
        "LastName": "Ich",
        "Department": 2
    },
    {
        "Name": "Charlie",
        "LastName": "Sheen",
        "Department": 3
    },
    {
        "Name": "Peter",
        "LastName": "Petrelli",
        "Department": 5
    },
    {
        "Name": "Alan",
        "LastName": "Harper",
        "Department": 6
    },
    {
        "Name": "Ann",
        "LastName": "Bottle",
        "Department": 3
    },
]

而且我想得到不同的结果Department,我不在乎顺序,每个结果只有 1 个Department。我尝试使用聚合,但我只能设法通过关联Deppartmentsdoc_count获得不同。他们查询我尝试过的内容类似于:

{
  "aggs": {
    "deppartments": {
      "terms": {
        "field": "Department"
      }
    }
  },"size": 0
}

它返回:

"buckets": [
    {
        "key": 2,
        "doc_count": 1
    },
    {
        "key": 3,
        "doc_count": 2
    },
    {
        "key": 5,
        "doc_count": 1
    },
    {
        "key": 6,
        "doc_count": 1
    },
]

当我想要类似的东西时:

[
    {
        "Name": "Alex",
        "LastName": "Ich",
        "Department": 2
    },
    {
        "Name": "Charlie",
        "LastName": "Sheen",
        "Department": 3
    },
    {
        "Name": "Peter",
        "LastName": "Petrelli",
        "Department": 5
    },
    {
        "Name": "Alan",
        "LastName": "Harper",
        "Department": 6
    }
]
4

1 回答 1

1

您可以为此使用热门点击聚合

{
  "aggs": {
    "departments": {
      "terms": {
        "field": "Department",
        "size": 10
      },
      "aggs": {
        "search_results": {
          "top_hits": {
            "size": 10   <--- you can change the size to 1 if you want
          }
        }
      }
    }
  },
  "size": 0
}

这有帮助吗?

于 2015-12-10T15:41:58.280 回答