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

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

  {
    "_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:

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

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?

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 ?

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