Composite, Nested, and Script Aggregation

How do I formulate a query to fetch the top discount (100 - price/value *100) of each brand, returning the brand name and its max discount available? I'm using a script to calc the discount, but I'm having trouble compiling the nested brand name to the un-nested price/value fields in a single aggregation result.

"mappings": {
    "_doc": {
      "properties": {
        "price": {
          "type": "integer"
        },
        "value": {
          "type": "integer"
        },
        "brand": {
          "type": "nested",
          "properties": {
            "name": {
              "type": "keyword"
            }
          }
        }
      }
    }
}```

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.