ES Version: 6.2.2
I'm trying to make a dashboard filtered by office, and get for a specific office (the selected one i get in the my http request) the price associate to this office.
Here is my ES mapping
"budgets": {
"properties": {
"office": {
"properties": {
"_id": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
},
"priceHT": {
"type": "long"
},
"priceTTC": {
"type": "long"
}
}
}
Here is my painless script multiline: I replace the params.officeId by the selected one i get from the client
double total = 0.0;
if(params.officeId !== 0){
if(doc.containsKey('budgets')){
for (p in params['_source'].budgets) {
if(p.office._id == params.officeId) {
total += p.priceTTC
}
if(p.office._id !== params.officeId) {
total += p.priceTTC
}
}
}
if(!doc.containsKey('budgets')){
total += 9999999
}
}
if(params.officeId == 0) {
if(doc['priceTTC'] !== null) {
total += doc['priceTTC'].value
}
if(doc['priceTTC'] == null) {
total += 0
}
}
return total"
Here is the full query which is working fine expect when i have a params.officeId
{
"size": 0,
"query": {
"bool": {
"must": [],
"filter": {
"bool": {
"must": [],
"should": []
}
}
}
},
'aggs': {
"supplier": {
"filter": {
"bool": {
"must": [],
"should": []
}
},
"aggregations": {
"filtered_supplier": {
"terms": {
"field": "supplier._id.keyword",
"size": 1000,
},
"aggs": {
"salesTTC": {
"sum": {
"script": {
"lang": "painless",
"source": "double total = 0.0;if(params.officeId !== 0){ if(doc.containsKey('budgets')){ for (p in params['_source'].budgets) { if(p.office._id == params.officeId) { total += p.priceTTC } if(p.office._id !== params.officeId) { total += p.priceTTC } } if(!doc.containsKey('budgets')){total += 1}}} if(params.officeId == 0) { if(doc['priceTTC'] !== null) { total += doc['priceTTC'].value } if(doc['priceTTC'] == null) {total += 0} } return total;",
"params": {
"officeId": 0
}
}
}
},
"salesHT": {
"sum": {
"script": {
"lang": "painless",
"source": "double total = 0.0;if(params.officeId !== 0){ if(doc.containsKey('budgets')){ for (p in params['_source'].budgets) { if(p.office._id == params.officeId) { total += p.priceHT } if(p.office._id !== params.officeId) { total += p.priceHT } } if(!doc.containsKey('budgets')){total += 1}}} if(params.officeId == 0) { if(doc['priceHT'] !== null) { total += doc['priceHT'].value } if(doc['priceHT'] == null) {total += 0} } return total;",
"params": {
"officeId": 0
}
}
}
}
}
}
}
}
}
};
I'm struggling since 2 days and tried many thinks, help me please
EDIT: the execution is not going in the if(doc.containsKey('budgets')){...} and always get 9999999 the test value i put in the line += 9999999
Thanks