# Is there a solution to get a sum equal to value of multiple field from elasticsearch data

**URL:** <https://discuss.elastic.co/t/is-there-a-solution-to-get-a-sum-equal-to-value-of-multiple-field-from-elasticsearch-data/303490>\
**Category:** Elasticsearch\
**Created:** [April 28, 2022, 9:27am UTC](https://discuss.elastic.co/t/is-there-a-solution-to-get-a-sum-equal-to-value-of-multiple-field-from-elasticsearch-data/303490 "2022-04-28T09:27:56Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![abderrahmen\_fekih](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abderrahmen_fekih/32/104982_2.png) [@abderrahmen\_fekih](https://discuss.elastic.co/u/abderrahmen_fekih)\
**Post date:** [April 28, 2022, 9:27am UTC](https://discuss.elastic.co/t/is-there-a-solution-to-get-a-sum-equal-to-value-of-multiple-field-from-elasticsearch-data/303490/1 "2022-04-28T09:27:56Z")

</div>

I have an Elasticsearch data (ingredients list). I want to get **exactly 4 ingredients** where the sum of **protein** equal to **70**

```auto
  {
    "_index" : "sample_ingredients",
    "_id" : "C_cH-X8BaYOrJWCswgP-",
    "_score" : 1.0,
    "_source" : {
      "main category" : "meat_and_poultry",
      "code" : 24130220,
      "calcium" : 16,
      "vitamin A" : 12,
      "alpha-linolenic acid" : "NULL",
      "vitamin C" : 0.0,
      "vitamin B2 (riboflavin)" : 0.232,
      "linoleic acid" : "NULL",
      "water" : 64.25,
      "carbohydrate" : 0.0,
      "sodium" : 362,
      "protein" : 25.54,
      "name" : "Chicken leg",
      "calorie" : 185,
      "fat" : 9.15,
      "iron" : 1.0,
      "vitamin B1 (thiamin)" : 0.059,
      "category" : "Poultry Products",
      "dietary fiber" : 0.0
    }
  },
  ...
  {
    "_index" : "sample_ingredients",
    "_id" : "DPcH-X8BaYOrJWCswgP-",
    "_score" : 1.0,
    "_source" : {
      "main category" : "meat_and_poultry",
      "code" : 24120120,
      "calcium" : 13,
      "vitamin A" : 5,
      "alpha-linolenic acid" : "NULL",
      "vitamin C" : 0.0,
      "vitamin B2 (riboflavin)" : 0.131,
      "linoleic acid" : "NULL",
      "water" : 67.78,
      "carbohydrate" : 0.0,
      "sodium" : 328,
      "protein" : 28.04,
      "name" : "Chicken breast",
      "calorie" : 144,
      "fat" : 3.57,
      "iron" : 0.46,
      "vitamin B1 (thiamin)" : 0.081,
      "category" : "Poultry Products",
      "dietary fiber" : 0.0
    }
  },
  {
    "_index" : "sample_ingredients",
    "_id" : "DfcH-X8BaYOrJWCswgP-",
    "_score" : 1.0,
    "_source" : {
      "main category" : "meat_and_poultry",
      "code" : 24160110,
      "calcium" : 33,
      "vitamin A" : 26,
      "alpha-linolenic acid" : "NULL",
      "vitamin C" : 0.0,
      "vitamin B2 (riboflavin)" : 0.161,
      "linoleic acid" : "NULL",
      "water" : 57.23,
      "carbohydrate" : 0.6,
      "sodium" : 579,
      "protein" : 23.42,
      "name" : "Chicken wing",
      "calorie" : 257,
      "fat" : 18.04,
      "iron" : 0.93,
      "vitamin B1 (thiamin)" : 0.078,
      "category" : "Poultry Products",
      "dietary fiber" : 0.0
    }
  },
  ....

```

i.e. i will get exactly 4 ingredients this is an exemple to what i want:

```auto
{
  "main category" : "meat_and_poultry",
  ...
  "protein" : 23,
  "name" : "Chicken wing",
},
{
  "main category" : "diary",
  ...
  "protein" : 26,
  "name" : "Milk",
},
{
  "main category" : "meat_and_poultry",
  ...
  "protein" : 16,
  "name" : "beef",
},
{
  "main category" : "fruits",
  ...
  "protein" : 5,
  "name" : "banana",
}

```

**=\> sum = 70 protein**

there is a solution to get a result like that ? there is a solution to get a result like that ? there is a solution to get a result like that ? there is a solution to get a result like that ?

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [April 28, 2022, 11:32am UTC](https://discuss.elastic.co/t/is-there-a-solution-to-get-a-sum-equal-to-value-of-multiple-field-from-elasticsearch-data/303490/2 "2022-04-28T11:32:11Z")

</div>

If I got it right, you are searching for the possibility to say "show me arbitrary ingredients whose sum is 70`. With the exception of a few boundaries like min and max, this will be more of a combinatorial explosion problem I guess. Maybe a SAT solver is what you are after here instead?

---

<div class="post-metadata">

**Author:** ![abderrahmen\_fekih](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abderrahmen_fekih/32/104982_2.png) [@abderrahmen\_fekih](https://discuss.elastic.co/u/abderrahmen_fekih)\
**Post date:** [April 28, 2022, 12:10pm UTC](https://discuss.elastic.co/t/is-there-a-solution-to-get-a-sum-equal-to-value-of-multiple-field-from-elasticsearch-data/303490/3 "2022-04-28T12:10:56Z")

</div>

yes we can use a min and max value too to get more result. but how can i write a Elasticsearch query to get a random ingredients where sum of protein is 70 ?

---

<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:** [May 26, 2022, 12:11pm UTC](https://discuss.elastic.co/t/is-there-a-solution-to-get-a-sum-equal-to-value-of-multiple-field-from-elasticsearch-data/303490/4 "2022-05-26T12:11:25Z")

</div>

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