Return value of null

Hello, i used script fileds to return value if sessionDate : null return 'never check'
My script do half work and don't return the value 'never check' when it==null

"script_fields" : {
      "check" : {
          "script" : {
"lang": "groovy",
"inline":
         "doc['sessionDate'].collect{ if (it!=null){ if (((DateTime.now().getMillis()-it)/(1000*60*60*24))<60) 'check ok';else  'check no';   }else 'Never check' }"
      }
  }

}

My data exemple :
"_id" :8582a,
"model" : {
"name" : "FO",
"members" : {
"697d" : {
"id" : "693y",
"sessionDate" : null
},
"38b8" : {
"id" : "587f",
"sessionDate" : ISODate("2017-12-22T11:21:08.280Z")
},
"0f96" : {
"id" : "635f",
"lastSessionDate" :ISODate("2017-05-05T15:05:57.076Z")
},
"3dfd-62e5-d063-4f42af01ccde" : {
"id" :"525c" ,
"lastSessionDate" : null
}
}
}

Result of script :

"_id": "8582a",
"_score": 1,
"fields": {
"checks": [
"check ok",
"check no"
]
}

"lastSessionDate" : null does not return '"Never check"

hi @radi,

have you tried testing this in isolation?

I'd also just compare the field value right away. e.g.

if (doc['sessionDate'].value == null) {
  return 'never check';
} else{ 
 return 'not null';
}

Does that give the expected result?

hi, i've already tested that and it's not working when the field is null the script can't
get the value

i have I have another example with value is null and i can't get it !

it's as if kibana (elastic)don't treats the null data in index !

in my case doc[' sessionDate'] it's a list that's why I use the collection and in the collection don't retrieve null values, it's not indexed when it's null,
I found a solution by changing the mapping when it is null to replace the value null value

I wanted to know if there is another way?

Could you use painless instead of groovy

Checking for null seems to work there.

e.g.:

or

for some examples

on first I wanted to used painless but I need to get the date right now this option don't exists in painless" topic get date right now in painless"

i have this answer for Elastic Team Member :
Scripted fields in Kibana simply use Elasticsearch's Painless scripting language. Unfortunately, that language can only be used to work with numbers, booleans, voids, and arrays. Since there is no way to operate on dates in the language, there is no way to calculate the difference between dates in scripted fields.

it's way i used groovy !

in the first exemple of topic Fields aggregation or field formatter?
he test if the value it's not null it's work for me too but return value when it's null it's not working
his exemple :

if (doc['name.keyword'].value != null)
return doc['name.keyword'].value;
if (doc['firstName.keyword'].value != null)
return doc['firstName.keyword'].value;
if (doc['fullName.keyword'].value != null)
return doc['fullName.keyword'].value;

what I want

if (doc['name.keyword'].value == null){
return 'it's null '
}

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