Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aplz/cc6f5071eb76f09c2b5e0d660a949e64 to your computer and use it in GitHub Desktop.
Save aplz/cc6f5071eb76f09c2b5e0d660a949e64 to your computer and use it in GitHub Desktop.
elasticsearch: script query to filter by number of items in field
// create the script query
ScriptQueryBuilder scriptQuery = QueryBuilders.scriptQuery(new Script("params._source.your_field.size() > 1"));
// add the query in a boolean term to enforce the filter
client.prepareSearch(indices).setQuery(QueryBuilders.boolQuery().must(scriptQuery)).get();
// the response will only contain documents whith more than one item in the field "your_field"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment