KIBANA : String Length based Filter to my documents

I need to Get the Length of a String datatype value and exclude the documents which have more than 4 characters using Elastic search custom query. I tried below query and didn't work. NO LUCK ! Could anyone help

. {
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['mycolumn'].values.length() > 4",
"lang": "painless"
}
}
}
}
}
}

Hello,

Can you please tell me where are you using this custom query? Which part of Kibana UI? and what results are getting displayed?

Thanks,
Bhavya

Hi bhavyarm, Thanks for your reply. I am using this custom query inside KIBANA console.
Actually, I need to exclude values with 3 & 6 digits. and get results only with 4 digits value in my result set. I have also tried the below one and got the below as the output. Is there any other way to achieve this. The problem is the column is a String but contains 3,4,6 digit numbers

CUSTOM QUERY :
{
"query": {
"bool": {
"filter": {
"script": {
"script": {
"source": "doc['mycolumn.keyword'].size() == 4",
"lang": "painless"
}
}
}
}
}
}

RESULT SET :

{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" :
}
}

@LeeDr can I get some help on this please? I am ending up with incorrect results in my dev tools console. Thanks!

Could you try with .length() instead of .size() ? I tried a scripted field and .length() worked for me.

2 Likes

Yes that works. Thanks Lee :slight_smile:

Thank you @bhavyarm and @LeeDr... Scripted Field worked for me. I did like the below script. One last Clarification. Can we filter based on Scripted Field? Only to have value.length() == 4

{
"_source": true,
"query": {
"match": {
"key": "Value"
}
},
"script_fields": {
"Length_Id": {
"script": {
"inline": "doc['mycolumn2'].value.length()"
}
}
}
}

1 Like

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