嵌套构面到elasticsearch中的聚合转换

时间:2017-08-23 16:51:53

标签: elasticsearch facet

我有以下es查询,我需要从1.5迁移到5.5 我知道我需要将facet转换为聚合。但是,在下面的示例中,有嵌套查询& facet_filter都涉及.. 基本上我想弄清楚所有搜索文档中每个field_val的计数,其中嵌套文档的相应field_name是" rg"

"facets": {
    "rg_facet": {
        "terms": {
            "field": "profile.field_val",
            "size": 100
        },
        "nested": "profile",
        "facet_filter": {
            "bool": {
                "must": [
                    {
                        "term": {
                            "profile.field_name": "rg"
                        }
                    }
                ]
            }
        }
    }
}

个人资料是嵌套字段。典型文档如下所示

{
  "field1" : "val1",
  ....
  "profile" : [
        {
          "field_val": "1705209",
          "field_name": "a_0"
        },
        {
          "field_val": "Batch 14",
          "field_name": "a_2"
        },
        {
          "field_val": "Delhi",
          "field_name": "rg"
        }
  ]
}

1 个答案:

答案 0 :(得分:0)

你可以这样做:

{
  "aggs": {
    "profile_agg": {
      "nested": {
        "path": "profile"
      },
      "aggs": {
        "rg_filter": {
          "filter": {
            "term": {
              "profile.field_name": "rg"
            }
          },
          "aggs": {
            "field_vals": {
              "terms": {
                "field": "profile.field_val", 
                "size": 100
              }
            }
          }
        }
      }
    }
  }
} 

请参阅nested aggregationfilter aggregationterms aggregation