Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

elasticsearch filter by length of a string field

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?

like image 364
Eyal Ch Avatar asked Dec 02 '15 11:12

Eyal Ch


1 Answers

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"
          }
      }
}
like image 149
Val Avatar answered Oct 19 '22 23:10

Val