# Composite, Nested, and Script Aggregation

**URL:** <https://discuss.elastic.co/t/composite-nested-and-script-aggregation/142950>\
**Category:** Elasticsearch\
**Created:** [August 3, 2018, 4:29pm UTC](https://discuss.elastic.co/t/composite-nested-and-script-aggregation/142950 "2018-08-03T16:29:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Hatch1](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@Michael\_Hatch1](https://discuss.elastic.co/u/Michael_Hatch1)\
**Post date:** [August 3, 2018, 4:29pm UTC](https://discuss.elastic.co/t/composite-nested-and-script-aggregation/142950/1 "2018-08-03T16:29:45Z")

</div>

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.

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 31, 2018, 4:29pm UTC](https://discuss.elastic.co/t/composite-nested-and-script-aggregation/142950/2 "2018-08-31T16:29:47Z")

</div>

Closed on August 31, 2018, 4:29pm UTC

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