elasticsearch中子文档的子聚合?

时间:2014-06-06 09:45:56

标签: elasticsearch

说我有父文件

curl -XPUT es-host:9200/index/parent_type/1 -d '{

  "foo": "bar"
}'

我有一份子文件

curl -XPUT es-host:9200/index/child_type/?parent=1 -d '{

  "child-foo-1": "child-bar-1",
  "child-foo-2": "child-bar-2"
}'

有没有办法在父文档聚合下为我的子文档数据获取子聚合,例如,

{
  "aggregations": {

    "foo-aggregations": {

      "buckets": [
        {
          "key": "bar",
          "doc_count": 1,
          "child-foo-1-aggregations": {

            "key": "child-bar-1",
            "doc_count": 1
          },
          "child-foo-2-aggregations": {

            "key": "child-bar-2",
            "doc_count": 1
          }
        }
      ]
    }
  }
}

1 个答案:

答案 0 :(得分:0)

curl -XPOST 'localhost:9200/index/parent/_search?pretty' -d '
{
  "size": "0",
    "aggs" : {
        "name" : {
            "terms" : {
                "field": "name.keyword"
            },
              "aggs" : {
                    "child_category": {
                        "children" : {
                            "type": "child_type_name"
                        },
                        "aggs" : {
                            "data" : {
                                "terms" : {
                                    "field": "child_field_text.keyword"
                                }

                            }
                        }
                    }
              }
        }
    }
}'