Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ElasticSearch:filtering documents based on field length?

Is there a way to filter ElasticSearch documents based on the length of a specific field?

For instance, I have a bunch of documents with the field "body", and I only want to return results where the number of characters in body is > 1000. Is there a way to do this in ES without having to add an extra column with the length in the index?

like image 408
Henley Avatar asked Jul 28 '13 17:07

Henley


1 Answers

Use the script filter, like this:

"filtered" : {
    "query" : {
        ...
    }, 
    "filter" : {
        "script" : {
            "script" : "doc['body'].length > 1000"
        }
    }
}

EDIT Sorry, meant to reference the query DSL guide on script filters

like image 168
Phil Avatar answered Sep 17 '22 14:09

Phil