Reverse_nested aggregation below a filter aggregation seems to ignore the current execution context


#1

Hello,

I have the following "product" mapping:

  "product": {
	"properties": {
	  "Product_Locations": {
		"type": "nested",
		"properties": {
		  "ProductLocation_CategoryAttributes": {
			"type": "nested",
			"properties": {
			  "Attribute_ID": {
				"type": "string",
				"index": "not_analyzed"
			  }
			}
		  },
		  "ProductLocation_CategoryPath": {
			"type": "nested",
			"properties": {
			  "Category_ID": {
				"type": "string",
				"index": "not_analyzed"
			  }
			}
		  }
		}
	  }
	}
  }

Notes:

  • a "product" has several "Product_Location"
  • each "Product_Location" has one "ProductLocation_CategoryPath", a "ProductLocation_CategoryPath" contains several "Category"' (this list of category is a branch of a classification where the product is classified in the leaf/last category)
  • "ProductLocation_CategoryAttributes" is a list of "Attribute" attached to the "ProductLocation_CategoryPath" so they are both related

Now my issue: I want to search product and, using aggregations, I want to get categories where the product are classified and for each "branch" where one product is classified, I want to get the related attributes too.

So I tried this query:

POST /products/product/_search
{
  "size": 100,
  "query": {
    "bool": {
      "filter": {
        "nested": {
          "path": "Product_Locations.ProductLocation_CategoryPath",
          "query": {
            "bool": {
              "must": [
                {
                  "term": {
                    "Product_Locations.ProductLocation_CategoryPath.Category_ID": {
                      "value": "category_id_1"
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
  },
  "aggs": {
    "aggs_1": {
      "nested": {
        "path": "Product_Locations"
      },
      "aggs": {
        "aggs_2": {
          "nested": {
            "path": "Product_Locations.ProductLocation_CategoryPath"
          },
          "aggs": {
            "aggs_3": {
              "filter": {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "Product_Locations.ProductLocation_CategoryPath.Category_ID": {
                          "value": "category_id_1"
                        }
                      }
                    }
                  ]
                }
              },
              "aggs": {
                "aggs_4": {
                  "terms": {
                    "field": "Product_Locations.ProductLocation_CategoryPath.Category_ID",
                    "size": 0
                  }
                },
                "aggs_5": {
                  "reverse_nested": {
                    "path": "Product_Locations.ProductLocation_CategoryAttributes"
                  },
                  "aggs": {
                    "aggs_6": {
                      "terms": {
                        "field": "Product_Locations.ProductLocation_CategoryAttributes.Attribute_ID",
                        "size": 0
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

In the result, the aggregation for the categories works fine but the one for the attribute does not.

It seems the reverse_nested aggregation ignores the current execution context (the agg filter about category) and so it returns some "random" attributes (i.e. some of the returned attributes are good but others seem to come from some completely unrelated product locations and products...)

Any idea how to fix this?

Thanks in advance.

Olivier.


(system) #2