HowTo Filter documents contain duplicates in array with PainLess

I have documents like

{"tokens" [
    {"city": "RU-Moscow", ...}, 
    {"city": "RU-Piter", ...}, 
    {"city": "RU-Moscow", ...}, 
    {"city": "GE-Berlin", ...},
    {"city": "NL-Amsterdam", ...}
    {"city": "NL-Amsterdam", ...}
    {"city": "NL-Amsterdam", ...}
], ...}

I want to find documents which have the same Russian city in tokens at least twice.

So I wrote such painless script:

String[] cities = new String[] {};
for (def token in doc['tokens']){
    String city = token['city'].toUpperCase();
    if (city.indexOf('RU') == 0) {
        if (cities.contains(city)) {
             return true;
        } else {
            cities.add(city);
        }
    }
}
return false;

but it fails with a runtime error
I even feel that I have no access to array tokens at all fron the filter context!!!

I also found similar problem here:


In this link he said that I have no access to params._source from filter context.

Is it really true?
How can I solve my problem?

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