This is my query, I have try few of them ( below ) trying to iterate the nested fields for compare the profit between 2017 and 2018 year. How can I iterate the nested fields ? Please help in advance...
1.for (int i=0; i<params["_source"].profit_nested; ++i)
-null pointer
2. for(item in params._source.profit_nested)
-null pointer
3. for (int i =0; i < doc['profit_nested'].value.length; i++)
-No field found for [profit_nested] in mapping with types
{
"size": 1,
"query": {
"bool": {
"must": {
"range" : {
"date" : {
"gte":"2017-12","lte":"2018-01","format":"yyyy-MM"
}
}
},
"filter": {
"nested": {
"path": "profit_nested",
"query": {
"script": {
"script": """
int year2018;
int year2017;
int year2016;
for (int i=1; i< params["_source"].profit_nested.year; i++){
year2018 = i;
}
if(year2017 == year2018)
{ return(true) } else { return(false) }
"""
}
}
}
}
}
}
}
This is my data result
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [
{
"_index" : "ftesting_nested",
"_type" : "testing",
"_id" : "dPWn92cBPjdQI7ZWakad",
"_score" : 1.0,
"_source" : {
"item" : "shirt",
"color": "red",
"date" : "2018-12",
"profit_nested" : [
{
"year" : "2014",
"profit" : 1000
},
{
"year" : "2015",
"profit" : -500
},
{
"year" : "2016",
"profit" : 1000
},
{
"year" : "2017",
"profit" : 2000
},
{
"year" : "2018",
"profit" : 3000
}
]
}
}