按字符串字段的长度进行elasticsearch过滤 [英] elasticsearch filter by length of a string field
本文介绍了按字符串字段的长度进行elasticsearch过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试获取标题"中超过 X 个字符的记录.
i am trying to get records the has in 'title' more then X characters.
注意:并非所有记录都包含标题字段.
NOTE: not all records contains title field.
我已经尝试过:
GET books/_search
{
"filter" : {
"script" : {
"script" : "_source.title.length() > 10"
}
}
}
结果,我收到此错误:
GroovyScriptExecutionException[NullPointerException[Cannot invoke method length() on null object
我该如何解决?
推荐答案
您需要考虑到某些文档可能具有空 title
字段.因此,您可以使用 groovy 空安全运算符.还要确保改用 POST 方法:
You need to take into account that some documents might have a null title
field. So you can use the groovy null-safe operator. Also make sure to use the POST method instead:
POST books/_search
{
"filter" : {
"script" : {
"script" : "_source.title?.size() > 10"
}
}
}
这篇关于按字符串字段的长度进行elasticsearch过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文