Aggregation nested field min value

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.

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

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

{
  "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.

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

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