# Aggregation nested field min value

**URL:** <https://discuss.elastic.co/t/aggregation-nested-field-min-value/185512>\
**Category:** Elasticsearch\
**Created:** [June 12, 2019, 8:54pm UTC](https://discuss.elastic.co/t/aggregation-nested-field-min-value/185512 "2019-06-12T20:54:54Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![PForsgard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pforsgard/32/48014_2.png) [@PForsgard](https://discuss.elastic.co/u/PForsgard)\
**Post date:** [June 12, 2019, 8:54pm UTC](https://discuss.elastic.co/t/aggregation-nested-field-min-value/185512/1 "2019-06-12T20:54:54Z")

</div>

I trying to get a facet filter out from prices that user has access to.

I have tried with the following data model and data but I don't get what I want out.

```auto
PUT /test
{
  "mappings": {
    "properties": {
      "prices": {
        "type": "nested",
        "properties": {
          "price": {
            "type": "double"
          },
          "systemId": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

POST /test/_doc
{
  "prices": [
    {
      "price": "100",
      "systemId": "1"
    },
    {
      "price": "200",
      "systemId": "1"
    },
    {
      "price": "300",
      "systemId": "2"
    }
  ]
}

POST /test/_doc
{
  "prices": [
    {
      "price": "200",
      "systemId": "1"
    },
    {
      "price": "300",
      "systemId": "2"
    }
  ]
}

```

As you can see the first document that was added have two prices with the same systemId and it is with this my knowledge about how to create the aggregation to only get the min price out for each product and use that in the aggregation.

The aggregation that I have created for the moment

```auto
POST /test/_search
{
  "query": {
    "match_all": {}
  },
  "aggs": {
    "prices-agg": {
      "nested": {
        "path": "prices"
      },
      "aggs": {
        "filtered-agg": {
          "filter": {
            "bool": {
              "must": [
                {
                  "term": {
                    "prices.systemId": {
                      "value": "1"
                    }
                  }
                }
              ]
            }
          },
          "aggs": {
            "price-term": {
              "terms": {
                "field": "prices.price",
                "order": {
                  "_key": "asc"
                }
              }
            }
          }
        }
      }
    }
  }
}

```

that will result in

```auto
{
  "took" : 0,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 2,
      "relation" : "eq"
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "zLFJTWsBUzYmxDgUL8T1",
        "_score" : 1.0,
        "_source" : {
          "prices" : [
            {
              "price" : "100",
              "systemId" : "1"
            },
            {
              "price" : "200",
              "systemId" : "1"
            },
            {
              "price" : "300",
              "systemId" : "2"
            }
          ]
        }
      },
      {
        "_index" : "test",
        "_type" : "_doc",
        "_id" : "zbFJTWsBUzYmxDgUQcS-",
        "_score" : 1.0,
        "_source" : {
          "prices" : [
            {
              "price" : "200",
              "systemId" : "1"
            },
            {
              "price" : "300",
              "systemId" : "2"
            }
          ]
        }
      }
    ]
  },
  "aggregations" : {
    "prices-agg" : {
      "doc_count" : 5,
      "filtered-agg" : {
        "doc_count" : 3,
        "price-term" : {
          "doc_count_error_upper_bound" : 0,
          "sum_other_doc_count" : 0,
          "buckets" : [
            {
              "key" : 100.0,
              "doc_count" : 1
            },
            {
              "key" : 200.0,
              "doc_count" : 2
            }
          ]
        }
      }
    }
  }
}

```

and here in the result I want the result as 100 = 1, 200 = 1 calculated of min(100 or 200) for the first hit and then 200 for the second hit. Is this possible to achieve?

In the end I want all products divided into 7 buckets to see how many products that exists in each price span.

Any help to pointing me into right direction is welcome.

---

<div class="post-metadata">

**Author:** ![PForsgard](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/pforsgard/32/48014_2.png) [@PForsgard](https://discuss.elastic.co/u/PForsgard)\
**Post date:** [June 28, 2019, 3:07pm UTC](https://discuss.elastic.co/t/aggregation-nested-field-min-value/185512/2 "2019-06-28T15:07:23Z")

</div>

Does not anyone have made a facet like this and can share some knowledge?

---

<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:** [July 26, 2019, 3:07pm UTC](https://discuss.elastic.co/t/aggregation-nested-field-min-value/185512/3 "2019-07-26T15:07:36Z")

</div>

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