Search code examples
elasticsearchopensearch

ElasticSearch - Run terms aggregation that returns terms that ONLY exist in document set A and NOT document set B


I'm trying to determine if what I'm trying to achieve is possible with ElasticSearch.

I have 2 sets of documents, lets say they are documents that have fields "document_set": "a" and "document_set": "b".

These documents also have nested documents, lets say they look like this:

[{ "fruit": "apple" }, { "fruit": "orange": }, { "fruit": "pear" }]

I'd like to run a nested terms aggregation that gets me all the terms that ONLY exist in documents with "document_set": "b" and NOT in documents with "document_set": "a".

Is what I'm trying to do possible in the recent versions of ElasticSearch?

Thanks!


Solution

  • I ended up solving this issue. I used a combination of bucket_selector sub-aggs and filter aggregations under my terms aggregation.