How to access the fields of arrays of Object in Pianless Script

I am using this script

double new_offer_price = doc['offer_price'].value;
boolean deal_type_check = true;
if (doc.containsKey('flash_deal_es')) {
for (int i = 0; i < doc['flash_deal_es'].length; i++) {
if (params.platform_type_id == 0) {
deal_type_check = (doc['flash_deal_es'][i].fd_deal_type.value == params.platform_type_id) ? true : false;
}
if (deal_type_check) {
if (((params.cur_date - doc['flash_deal_es'][i].fd_start_dt.date.getMillis()) > 0) && ((doc['flash_deal_es'][i].fd_end_dt.date.getMillis() - params.cur_date) > 0)) {
new_offer_price = doc['flash_deal_es'][i].fd_offer_price.value;
}
}
}
}
return new_offer_price;

its not working properly,,
is there any syntactical mistake???

You'll need to be more specific than "it's not working properly". Do you get an exception from elasticsearch when using that script? Why is the json requestion you are making?

Also, please blockquote code/json using triple quotes (it makes it much easier to read), eg:

double new_offer_price = doc['offer_price'].value;
boolean deal_type_check = true;
...

No, I am not getting Exception,

"_script": {
"script": {
"source": "double new_offer_price = doc['offer_price'].value;boolean deal_type_check = true;if(doc.containsKey('flash_deal_es')){for(int i=0; i< doc['flash_deal_es'].length; i++) {if(params.platform_type_id == 0){deal_type_check = (doc['flash_deal_es'][i].fd_deal_type.value == params.platform_type_id) ? true : false;}if(deal_type_check){if(((params.cur_date - doc['flash_deal_es'][i].fd_start_dt.date.getMillis()) > 0) && ((doc['flash_deal_es'][i].fd_end_dt.date.getMillis() - params.cur_date) > 0)){new_offer_price = doc['flash_deal_es'][i].fd_offer_price.value;}}}}return new_offer_price;",
"lang": "painless",
"params": {
"cur_date": 1519121635067,
"platform_type_id": 0
}
},
"type": "number",
"order": "asc"
}

Can we print the value of variable of script in elasticsearch log???
Elasticsearch:6.1

There is no logging functionality in painless as that would be a big performance hit if done on a search request matching many documents. Instead, there is Debug.explain().

Since there is no exception, can you be more specific about what you are expecting from this request and what behavior you actually see?

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