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
    },
]

そして、 distinct で結果を取得したいのですが、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 に答える