Elasticsearch中多个字段的术语聚合

时间:2018-05-08 12:59:52

标签: elasticsearch

我试图从Elasticsearch获得一些数据。我有一个问题:

GET index/_search
{
  "aggs": {
    "first-metadata": {
      "terms": {
        "field": "filters.metadata.first-metadata"
      }
    }
  }
}

作为回应,我得到了类似的东西:

"aggregations": {
  "first-metadata": {
    "buckets": [
      {
        "key": 87000,
        "doc_count": 2
      },
      {
        "key": 100000,
        "doc_count": 1
      }
    ]
  }
}

一切都像我预期的那样。但问题是我有多种元数据类型:first-metadatasecond-metadatathird-metadata我希望有类似的东西:

"aggregations": {
  "first-metadata": {
    "buckets": [
      {
        "key": 87000,
        "doc_count": 2
      },
      {
        "key": 100000,
        "doc_count": 1
      }
    ]
  },
  "second-metadata": {
    "buckets": [
      {
        "key": 5555,
        "doc_count": 24
      },
      {
        "key": 2222,
        "doc_count": 14
      },
      {
        "key": 3333,
        "doc_count": 7
      }
    ]
  },
  "third-metadata": {
    "buckets": [
      {
        "key": 7777,
        "doc_count": 23
      },
      {
        "key": 3333,
        "doc_count": 11
      }
    ]
  }
}

有没有办法在一个聚合查询中实现这样的结果?

1 个答案:

答案 0 :(得分:1)

非常简单

POST index/_search
{
  "aggs": {
    "first-metadata": {
      "terms": {
        "field": "filters.metadata.first-metadata"
      }
    },
    "second-metadata": {
      "terms": {
        "field": "filters.metadata.second-metadata"
      }
    }

  }
}