elasticsearch按字符串字段的长度过滤 [英] elasticsearch filter by length of a string field

查看:1076
本文介绍了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"
          }
      }
}

结果是,我收到此错误:

as a result, i get this error:

GroovyScriptExecutionException[NullPointerException[Cannot invoke method length() on null object

我该如何解决?

推荐答案

您需要考虑到某些文档的<$ c可能为空$ c>标题字段。因此,您可以使用常规的空安全运算符。还要确保改用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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆