How to filter based on the count of terms in a field

So I have a field that's basically just an array of tags and what I want to do is filter out any documents where the length of that tags array (i.e. the number of tags in the field) is greater than some specific number. This seems like it should be pretty simple but I've pored over the docs and I can't seem to find anything in the query DSL for querying based on a field's term count. I know you could probably achieve this using a script_score combined with min_score, but that feels like it wouldn't be very performant and I also just want to make sure I'm not missing the "right" way to do something like this.

Assuming there is a way to do this that isn't script_score, I'm also wondering if there's a way to filter based on the number of tags in a field that don't match any term in a terms query. In other words I need to somehow filter based on (doc['tags'] - query['tags']).length. Any help would be much appreciated.

Have you considered using an ingest pipeline? You could use that to automatically add a new field to your documents at index time, with the number of tags as the value. You could use a script processor for that. You'd then have a number in your documents that you can query with a simple range query.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.