Let's say we have four documents with the tags field. It can contain several lines, for example foo , bar and baz .
docA.tags = ['foo'] docB.tags = ['bar'] docC.tags = ['foo', 'bar'] docD.tags = ['foo', 'baz']
I request documents using aggregations, so I get four documents and a list of three codes with a count corresponding to a specific tag.
buckets = [ {key: 'bar', doc_count: 2}, // docB, docC {key: 'foo', doc_count: 3}, // docA, docC, docD {key: 'baz', doc_count: 1} // docD ]
If I run another request now and add one of these tags - say, foo - as a filter of conditions for the request, I get only documents ( docA , docC , docD )) there is this tag. This is what I want.
But I also get another list of possible aggregates with updated counts.
buckets = [ {key: 'bar', doc_count: 1}, // docC {key: 'baz', doc_count: 1}, // docD ]
But these calculations really do not match what is happening. They reflect the number of documents that match both tags that I selected first ( foo ) and one of the buckets ( bar or baz ).
But if I then select the second tag - say, baz - I get the documents marked with foo OR baz . This is because I use the terms filter.
So what I really want is
buckets = [ {key: 'bar', doc_count: 1}, //docB {key: 'baz', doc_count: 0}, ]
How can I achieve that the calculations are appropriate. They should reflect the number of documents to be added if I select the second tag. An example of this is here .
I already tried using post_filter , but that always gives me the first result. Than a min_doc_count -flag for aggs, but it only shows me combinations that will lead to count=0 .
I have a solution for this, but it seems to me rather complicated. To do this, I would have to run another query for each unit, where I invert the filter criteria. Therefore, in the above example, I have to make a request to all documents that do not have the foo tag and correspond to the rest of the request. The aggregation results will be exactly what I need.