I'm trying to make a select query with a Painless script, but I keep getting an error in the response that the script is not valid.
What I'm trying to do in this simplified script is to check if my given param ages are both adult following the age in 'unit.info'.
Error:
"if(containsAge(doc['unitHolder.units'], params.ages)){",
" ^---- HERE",...
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [unitHolder.units] in mapping with types [pack]"
}
Request query:
{
"query": {
"bool": {
"must": [
{
"script": {
"script": {
"source": "boolean containsAge(def unit, def ages)
{
if(unit.info.children.minAge != null)
{
int nrAdults = 0;
int nrChildren = 0;
int nrInfants = 0;
for (age in ages)
{
if (age < unit.info.children.maxAge.value)
{
nrAdults++;
}else if(age > unit.info.children.minAge.value)
{
nrInfants++;
}else{
nrChildren++;
}
}
if (nrAdults > 2)
{
return true;
}
}
return false;
}
if(containsAge(doc['unitHolder.units'], params.ages))
{
return true;
}
return false;",
"lang": "painless",
"params": {
"ages": [
50,
35
]
}
}
}
}
]
}
},
"size": 10
}
Mapping:
"mappings": {
"pack": {
"properties": {
"unitHolder": {
"properties": {
"createDate": {
"type": "date"
},
"units": {
"properties": {
"info": {
"properties": {
"children": {
"properties": {
"maxAge": {
"type": "long"
},
"minAge": {
"type": "long"
}
}
}
}
}
}
}
}
}
}
}
}