How to filter only specific objects from nested array in elasticsearch?

{
  "sku_id": 12,
  "product_id": 1,
  "product_name": "coca-cola 1.5 L",
  "category_name": "beverages",
  "brand_name": "coca cola",
  "sellers": [
    {
      "seller": 1,
      "price": 50
    },
    {
      "seller": 2,
      "price": 40
    },
    {
      "seller": 3,
      "price": 30
    }
  ]
}

I have this type of document. I want to write a query to select product but only that seller in sellers array who have lowest value of price and ignore all other sellers in result.
My expected output is

{
  "sku_id": 12,
  "product_id": 1,
  "product_name": "coca-cola 1.5 L",
  "category_name": "beverages",
  "brand_name": "coca cola",
  "sellers": [
    {
      "seller": 3,
      "price": 30
    }
  ]
}

I can use aggregate but aggregation adds separate aggregation object at the end of result. Aggregation will not keep sellers array part of product object. I want exactly same response as i mentioned above.

1 Like

Elasticsearch can as far as I know not only return selected nested documents like in your example, so this is a transformation you will need to perform yourself on the result.

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