Issue regarding reverse nested

GET index_name/_search
{
 
  "aggs": {
    "firs_i_want_to_group_by_item": {
      "nested": {
        "path": "associations"
      },
      "aggs": {
        "filter1": {
          "filter": {
            "term": {
              "associations.id": "1376430053646835712"
            }
          },
          "aggs": {
            "nested2": {
              "nested": {
                "path": "associations.associations"
              },
              "aggs": {
                "filter2": {
                  "filter": {
                    "term": {
                      "associations.associations.id": "1376428714334326784"
                    }
                  },
                  "aggs": {
                    "first_group_by_item": {
                      "terms": {
                        "field": "associations.associations.item_name.keyword"
                      },
                      "aggs": {
                        "go_to_level_1_using_nested": {
                          "reverse_nested": {
                            "path": "associations"
                          },
                          "aggs": {
                            "item_level_sum": {
                              "sum": {
                                "field": "associations.item_amount"
                              }
                            }
                          }
                        },
                        "so_now_lets_go_to_level_one_and_group_by": {
                          "reverse_nested": {
                            "path": "associations"
                          },
                          "aggs": {
                            "again_reverse": {
                              "reverse_nested": {
                              
                              },
                              "aggs": {
                                "group_by_state": {
                                  "terms": {
                                    "field": "state.keyword",
                                    "size": 10
                                  },
                                  "aggs": {
                                    "actual_sum": {
                                      "nested": {
                                        "path": "associations"
                                      },
                                      "aggs": {
                                        "summ": {
                                          "sum": {
                                            "field": "associations.item_name"
                                          }
                                        }
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                         
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

  "aggregations": {
    "firs_i_want_to_group_by_item": {
      "doc_count": 4,
      "filter1": {
        "doc_count": 4,
        "nested2": {
          "doc_count": 4,
          "filter2": {
            "doc_count": 4,
            "first_group_by_item": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [
                {
                  "key": "mobile",
                  "doc_count": 3,
                  "so_now_lets_go_to_level_one_and_group_by": {
                    "doc_count": 3,
                    "again_reverse": {
                      "doc_count": 3,
                      "group_by_state": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                          {
                            "key": "telangana",
                            "doc_count": 2,
                            "actual_sum": {
                              "doc_count": 3,
                              "summ": {
                                "value": 83000
                              }
                            }
                          },
                          {
                            "key": "assam",
                            "doc_count": 1,
                            "actual_sum": {
                              "doc_count": 1,
                              "summ": {
                                "value": 25000
                              }
                            }
                          }
                        ]
                      }
                    }
                  },
                  "go_to_level_1_using_nested": {
                    "doc_count": 3,
                    "item_level_sum": {
                      "value": 75000
                    }
                  }
                },
                {
                  "key": "television",
                  "doc_count": 1,
                  "so_now_lets_go_to_level_one_and_group_by": {
                    "doc_count": 1,
                    "again_reverse": {
                      "doc_count": 1,
                      "group_by_state": {
                        "doc_count_error_upper_bound": 0,
                        "sum_other_doc_count": 0,
                        "buckets": [
                          {
                            "key": "telangana",
                            "doc_count": 1,
                            "actual_sum": {
                              "doc_count": 2,
                              "summ": {
                                "value": 58000
                              }
                            }
                          }
                        ]
                      }
                    }
                  },
                  "go_to_level_1_using_nested": {
                    "doc_count": 1,
                    "item_level_sum": {
                      "value": 33000
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  }

in the actual sum its consider another nested objects under state which does not fall under the parent group.

@Itamar_Syn-Hershko @Mark_Harwood1

Please don’t @ me

Please note that this forum is largely run by volunteers and that it is considered quite rude to ping people that are not already involved in the thread. You therefore also need to be patient. If you have not received any response within a 2 or 3 business days it is generally fine to bump the thread.

I would also recommend always specifying which version of Elasticsearch you are using and explain in detail what the problem is. The more effort you put in explaining the issue, the better the chance someone will be able to help. If you, who have the problem, can not be bothered to explain the issue in detail so the readers can quickly get a grasp of it, why should they choose to spend their precious time trying to understand your problem?

1 Like

did you know the answer please?

If someone says they do not want to be pinged I think you should respect that and not ping them again. When I see someone behaving this way I tend to mute the thread irrespective of whether I know the answer or not, and would not be surprised if Mark has done the same.

I would recommend you spend some more time improving the description of your problem so it is more accessible to other users that likely do not have the same background knowledge of the problem as you do.

1 Like