Elasticsearch 嵌套聚合查询

时间:2021-04-29 06:20:29

标签: elasticsearch elasticsearch-aggregation

我正在尝试从下面的 Elasticsearch 模型中提取一些指标。

映射:

{
"properties":{
    "createdAt":{
        "type":"long"
    },
    "updatedAt":{
        "type":"long"
    },
    "tags": {
        "type": "nested",
        "properties": {
            "groupId": {
                "type": "keyword"
            },
            "accountId": {
                "type": "keyword"
            }
        }
    },
    "records":{
        "type":"nested",
        "properties":{
            "count":{
                "type":"integer"
            },
            "recordTags":{
                "type":"nested",
                "properties":{
                    "name":{
                        "type":"keyword"
                    },
                    "enabled":{
                        "type":"boolean"
                    }
                }
            }
        }
    }
}

}

示例记录:

记录-1:

{
"_id": "05b7a31833f821d80f0df98fbf39be3815d"
"createdAt":1619540790808,
"updatedAt":1619540798261,
"tags" : {
    "groupId" : "abe0326-41d2-9a82-79c68ef50761",
    "accountId" : "08747"
},
"records":[
    {
        "count": 2,
        "recordTags": {
            "name" : "RECO_1",
            "enabled" : true,            
        }
        
    },
    {
        "count": 3,
        "recordTags": {
            "name" : "RECO_2",
            "enabled" : true,            
        }
        
    }
]

}

记录 2:

{
"_id": "7a31833f821d80f0df98fbf39be3815d"
"createdAt":1619540790808,
"updatedAt":1619540798261,
"tags" : {
    "groupId" : "abe0326-41d2-9a82-79c68ef50761",
    "accountId" : "08747"
},
"records":[
    {
        "count": 2,
        "recordTags": {
            "name" : "RECO_1",
            "enabled" : true,            
        }
        
    },
    {
        "count": 3,
        "recordTags": {
            "name" : "RECO_2",
            "enabled" : true,            
        }
        
    }
]

}

我想通过recordTags.name 聚合记录并显示records.count 字段的总和。 前任: RECO_1(计数:4)

RECO_2(计数:6)

尝试了这个聚合查询,但它没有按预期工作。

/_search

{
"size":0,
"query": {
    "bool": {
        "must": [
            {
                "nested": {
                    "path": "tags",
                    "query": {
                        "term": {
                            "tags.accountId": "08747"
                        }
                    }
                }
            }
        ]
    }
},
"aggs":{
    "distinct_groups":{
        "nested":{
            "path":"tags"
        },
        "aggs":{
            "account_id":{
                "terms":{
                    "field":"tags.accountId"
                },
                "aggs":{
                    "groupId":{
                        "terms":{
                            "field":"tags.groupId"
                        },
                        "aggs":{
                            "distinct_records":{
                                "nested":{
                                    "path":"records.recordTags"
                                },
                                "aggs":{
                                    "interction":{
                                        "terms":{
                                            "field":"records.recordTags.name"
                                        }
                                    }
                                  }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

}

您能否帮助我了解如何获取指定聚合的记录数?

1 个答案:

答案 0 :(得分:0)

一旦您在 one nested 上下文中聚合并准备跳入 不同 嵌套上下文,您首先需要 脱离当前上下文。这是通过 reverse_nested aggregation 完成的,即:

{
  "size": 0,
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "tags",
            "query": {
              "term": {
                "tags.accountId": "08747"
              }
            }
          }
        }
      ]
    }
  },
  "aggs": {
    "distinct_groups": {
      "nested": {
        "path": "tags"
      },
      "aggs": {
        "account_id": {
          "terms": {
            "field": "tags.accountId"
          },
          "aggs": {
            "groupId": {
              "terms": {
                "field": "tags.groupId"
              },
              "aggs": {
                "back_to_top": {
                  "reverse_nested": {},      <--- empty options mean that you're jumping to the doc root
                  "aggs": {
                    "distinct_records": {
                      "nested": {
                        "path": "records.recordTags"
                      },
                      "aggs": {
                        "interction": {
                          "terms": {
                            "field": "records.recordTags.name"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}