# KIBANA : String Length based Filter to my documents

**URL:** <https://discuss.elastic.co/t/kibana-string-length-based-filter-to-my-documents/242927>\
**Category:** Kibana\
**Created:** [July 28, 2020, 3:37pm UTC](https://discuss.elastic.co/t/kibana-string-length-based-filter-to-my-documents/242927 "2020-07-28T15:37:27Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Nivas\_Sundar](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@Nivas\_Sundar](https://discuss.elastic.co/u/Nivas_Sundar)\
**Post date:** [July 28, 2020, 3:37pm UTC](https://discuss.elastic.co/t/kibana-string-length-based-filter-to-my-documents/242927/1 "2020-07-28T15:37:27Z")

</div>

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"  
}  
}  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![bhavyarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavyarm/32/22392_2.png) [@bhavyarm](https://discuss.elastic.co/u/bhavyarm)\
**Post date:** [July 30, 2020, 9:48pm UTC](https://discuss.elastic.co/t/kibana-string-length-based-filter-to-my-documents/242927/2 "2020-07-30T21:48:40Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Nivas\_Sundar](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@Nivas\_Sundar](https://discuss.elastic.co/u/Nivas_Sundar)\
**Post date:** [July 31, 2020, 12:19pm UTC](https://discuss.elastic.co/t/kibana-string-length-based-filter-to-my-documents/242927/3 "2020-07-31T12:19:52Z")

</div>

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" :   
}  
}

---

<div class="post-metadata">

**Author:** ![bhavyarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavyarm/32/22392_2.png) [@bhavyarm](https://discuss.elastic.co/u/bhavyarm)\
**Post date:** [July 31, 2020, 4:31pm UTC](https://discuss.elastic.co/t/kibana-string-length-based-filter-to-my-documents/242927/4 "2020-07-31T16:31:13Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![LeeDr](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/leedr/32/9289_2.png) [@LeeDr](https://discuss.elastic.co/u/LeeDr)\
**Post date:** [July 31, 2020, 4:40pm UTC](https://discuss.elastic.co/t/kibana-string-length-based-filter-to-my-documents/242927/5 "2020-07-31T16:40:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bhavyarm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bhavyarm/32/22392_2.png) [@bhavyarm](https://discuss.elastic.co/u/bhavyarm)\
**Post date:** [July 31, 2020, 4:47pm UTC](https://discuss.elastic.co/t/kibana-string-length-based-filter-to-my-documents/242927/6 "2020-07-31T16:47:01Z")

</div>

Yes that works. Thanks Lee 🙂

---

<div class="post-metadata">

**Author:** ![Nivas\_Sundar](https://avatars.discourse-cdn.com/v4/letter/n/f0a364/32.png) [@Nivas\_Sundar](https://discuss.elastic.co/u/Nivas_Sundar)\
**Post date:** [August 1, 2020, 6:38am UTC](https://discuss.elastic.co/t/kibana-string-length-based-filter-to-my-documents/242927/7 "2020-08-01T06:38:08Z")

</div>

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()"  
}  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [August 29, 2020, 6:38am UTC](https://discuss.elastic.co/t/kibana-string-length-based-filter-to-my-documents/242927/8 "2020-08-29T06:38:11Z")

</div>

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