Skip to content

Incorrect reverse nested agg counts when using multi level nested filters #9263

@spotta

Description

@spotta

There are couple of issues, one is regression of previously fixed issue #6994. This is working fine in 1.3.4 and 1.4.0, failing from 1.4.1.

Also there is another issue, reverse nested agg counts are incorrect when using more than one level of nested filter when fetching the aggs, this is happening even in 1.3.4.

Below details to repro the issue. Not sure if these should be separate issues, entering as one as they seem to be related.

DELETE /_all


POST /test
{
  "mappings": {
    "foo": {
      "dynamic": "strict",
      "properties": {
        "id": {
          "type": "long"
        },
        "baz": {
          "type": "nested",
          "properties": {
            "baz_cde": {
              "type": "string"
            }
          }
        },
        "bar": {
          "type": "nested",
          "properties": {
            "bar_typ": {
              "type": "string"
            },
            "color": {
              "type": "nested",
              "properties": {
                "name": {
                  "type": "string"
                }
              }
            }
          }
        }
      }
    }
  }
}


PUT /test/foo/1
{
    "id": 1,
    "bar": [
        {
            "bar_typ": "bar1",
            "color": [
                {
                    "name": "red"
                },
                {
                    "name": "green"
                },
                {
                    "name": "yellow"
                }
            ]
        },
        {
            "bar_typ": "bar1",
            "color": [
                {
                    "name": "red"
                },
                {
                    "name": "blue"
                },
                {

                    "name": "white"
                }
            ]
        },
        {
            "bar_typ": "bar1",
            "color": [
                {
                    "name": "black"
                },
                {
                    "name": "blue"
                }
            ]
        },
        {
            "bar_typ": "bar2",
            "color": [
                {
                    "name": "orange"
                }
            ]
        },
        {
            "bar_typ": "bar2",
            "color": [
                {
                    "name": "pink"
                }
            ]
        }
    ],
    "baz": [
        {
            "baz_cde": "abc"
        },
        {
            "baz_cde": "klm"
        },
        {
            "baz_cde": "xyz"
        }
    ]
}

Query to find bar counts, grouping by baz_cde, and applying a bar filter

POST /test/_search
{
  "size": 0,
  "aggs": {
    "nested_0": {
      "nested": {
        "path": "baz"
      },
      "aggs": {
        "group_by_baz": {
          "terms": {
            "field": "baz.baz_cde"
          },
          "aggs": {
            "to_root": {
              "reverse_nested": {},
              "aggs": {
                "nested_1": {
                  "nested": {
                    "path": "bar"
                  },
                  "aggs": {
                    "filter_by_bar": {
                      "filter": {
                        "term": {
                          "bar.bar_typ": "bar1"
                        }
                      },
                      "aggs": {
                        "bar_count": {
                          "value_count": {
                            "field": "bar_typ"
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

This query should return bar_count as 3 for all baz_cde fields, and is working as expected in 1.3.4 and 1.4.0, but failing from 1.4.1 onwards. Tested in 1.4.2 also.

Query to find bar counts, grouping by baz_cde, and applying a bar filter as well as bar.color filter

POST /test/_search
{
  "size": 0,
  "aggs": {
    "nested_0": {
      "nested": {
        "path": "baz"
      },
      "aggs": {
        "group_by_baz": {
          "terms": {
            "field": "baz.baz_cde"
          },
          "aggs": {
            "to_root": {
              "reverse_nested": {},
              "aggs": {
                "nested_1": {
                  "nested": {
                    "path": "bar"
                  },
                  "aggs": {
                    "filter_by_bar": {
                      "filter": {
                        "term": {
                          "bar.bar_typ": "bar1"
                        }
                      },
                      "aggs": {
                        "nested_2": {
                          "nested": {
                            "path": "bar.color"
                          },
                          "aggs": {
                            "filter_bar_color": {
                              "filter": {
                                "term": {
                                  "bar.color.name": "red"
                                }
                              },
                              "aggs": {
                                "reverse_to_bar": {
                                  "reverse_nested": {
                                    "path": "bar"
                                  },
                                  "aggs": {
                                    "bar_count": {
                                      "value_count": {
                                        "field": "bar_typ"
                                      }
                                    }
                                  }
                                }
                              }
                            }
                          }
                        }
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

This query should return bar_count as 2 for all baz_cde fields, but only one of them "xyz" is getting the correct count 2 but for the others value 1 is returned. This is happening in older versions too.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions