Search code examples
elasticsearchmvel

Filter facet terms based on their length when using scripts in Elasticsearch queries


I want to create a terms facet which returns results only for terms of length greater than a certain value. Ideally it would look something like:

 "facets": {
    "myFacet": {
      "terms": {
        "field": "content",
        "size": 10,
        "script": "term.length > 3 ? true: false"
      }
    }
  }

However, the MVEL scripting language doesn't seem to like the term.length property. Is there another way to achieve this? The MVEL docs don't provide much info on this use case.


Solution

  • According to error from elasticsearch (Error: could not access: length; in class: java.lang.String) mvel delegates length to java.lang.String class, so you should use java.lang.String#length() method.

    "facets": {
      "myFacet": {
        "terms": {
          "field": "content",
          "size": 10,
          "script": "term.length() > 3 ? true: false"
        }
      }
    }