7

i am trying to get records the has in 'title' more then X characters.

NOTE: not all records contains title field.

i have tried:

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

how can i solve it?

Eyal Ch
  • 9,552
  • 5
  • 44
  • 54

2 Answers2

11

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"
          }
      }
}
Val
  • 207,596
  • 13
  • 358
  • 360
0

You can also use custom tokenizers to count the number of characters. Check this answer for a possible help: https://stackoverflow.com/a/47556098/463846

Mousa
  • 2,926
  • 1
  • 27
  • 35