Translate pseudo code in ES scripts

Hi all, i can`t create correct code for next task:
I have some audience items with different information. For example
"Audience": [
{
"AgeGroup1": "13-17",
"AgePercentage1": 5.52,
"AgeGroup2": "18-24",
"AgePercentage2": 29.47,
"Country1": "US",
"CountryPercentage1": 42,
"Country1": "CA",
"CountryPercentage1": 27,
"LastUpdated": "2017-03-01T11:01:11"
}
]

Need select items WHERE sum of selected items more or equal choosed value. For example 60
And important moments. Main search on one type, and additional moments on child type
I try create this script, wihtout any additional filters:

{
  "query": {
    "function_score": {
      "query": {
        "bool": {
          "minimum_should_match": 1,
          "must": [
            {
              "has_child": {
                "type": "publisher_audience",
                "query": {
                  "bool": {
                    "minimum_should_match": 1,
                    "must": [
                      {
                        "script": {
                          "script": {
                          	"lang": "painless",
          "inline": "if (doc['Audience'] != null && doc['Audience'].size() > 0) { double result;for (int i = 0; i < doc['Audience'].length; ++i) {result = 0.0; for (int j = 0; j < params.countries.length; ++j) {if (doc['Audience'][i].Country1 == params.countries[j] && doc['Audience'][i].CountryPercentage1) {result += doc['Audience'][i].CountryPercentage1.toDouble();} else if (doc['Audience'][i].Country2 == params.countries[j] && doc['Audience'][i].CountryPercentage2) {result += doc['Audience'][i].CountryPercentage2.toDouble();} else if (doc['Audience'][i].Country3 == params.countries[j] && doc['Audience'][i].CountryPercentage3) {result += doc['Audience'][i].CountryPercentage3.toDouble();} else if (doc['Audience'][i].Country4 == params.countries[j] && doc['Audience'][i].CountryPercentage4) {result += doc['Audience'][i].CountryPercentage4.toDouble();}} if (result >= params.threshold) {return true;}}} return false",
                            "params": {
                              "threshold": 60,
                              "countries": [
                                "US",
                                "CA"
                              ]
                            }
                          }
                        }
                      }
                    ]
                  }
                }
              }
            }
          ]
        }
      }
    }
  },
  "sort": [
    {
      "TotalSubscribers": {
        "order": "desc"
      }
    }
  ],
  "from": 0,
  "size": 50
}

But it`s not works. Only 0 results and 0 errors

Main idea is next:

float total = 0;
if( audience.countryName = 'US') { total+=audience.countryValue} else if ( audience.countryName = 'CA') { total+=audience.countryValue}
if (total > 60) { /*need select this item */} else { /* skip this item */}

How i can do it?

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