# Translate pseudo code in ES scripts

**URL:** <https://discuss.elastic.co/t/translate-pseudo-code-in-es-scripts/107442>\
**Category:** Elasticsearch\
**Created:** [November 13, 2017, 4:56pm UTC](https://discuss.elastic.co/t/translate-pseudo-code-in-es-scripts/107442 "2017-11-13T16:56:40Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![DedMorozzz.kh](https://avatars.discourse-cdn.com/v4/letter/d/e56c9b/32.png) [@DedMorozzz.kh](https://discuss.elastic.co/u/DedMorozzz.kh)\
**Post date:** [November 13, 2017, 4:56pm UTC](https://discuss.elastic.co/t/translate-pseudo-code-in-es-scripts/107442/1 "2017-11-13T16:56:40Z")

</div>

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?

---

<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:** [December 11, 2017, 4:56pm UTC](https://discuss.elastic.co/t/translate-pseudo-code-in-es-scripts/107442/2 "2017-12-11T16:56:42Z")

</div>

Closed on December 11, 2017, 4:56pm UTC

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