Hello Michaël , I cant think of a way to do this in a single call. May be you should try the following
(Terms aggregation on element) -> (Top N hits aggregation , sort by date by asc and size = 1 ) -> (Filter aggregation by type A) With this you will get the elements that you are looking for. Now do a filter on those elements and a terms aggregation query on element filed to get the results. Thanks Vineeth On Sun, Oct 26, 2014 at 1:04 AM, Michaël Gallego <mich...@maestrooo.com> wrote: > Hi, > > I have a type whose data looks like this: > > { > "date": "2014-01-01" > "element": "abc", > "type": "A" > }, > { > "date": "2014-01-02" > "element": "abc", > "type": "B" > }, > { > "date": "2014-01-03" > "element": "def", > "type": "A" > } > > I'd like to be able to group the data by element, and count the documents > where the LAST document by date have a type of A. In this case, I want the > result to be "1" (because the second document, that has the same element as > the first document, has a date that is after the first document, but as its > type is not B, I don't want it to be counted ; for the last document, it is > the only one with element "def" and the type is A). > > I'm not sure this is even possible. Please note that the cardinality of > "element" can be quite high (up to 20 000 different values). > > Thank you in advance! > > -- > You received this message because you are subscribed to the Google Groups > "elasticsearch" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to elasticsearch+unsubscr...@googlegroups.com. > To view this discussion on the web visit > https://groups.google.com/d/msgid/elasticsearch/46509869-4afa-4062-8c34-ad828dcf680c%40googlegroups.com > <https://groups.google.com/d/msgid/elasticsearch/46509869-4afa-4062-8c34-ad828dcf680c%40googlegroups.com?utm_medium=email&utm_source=footer> > . > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "elasticsearch" group. To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mGGdrY-K3maf4H0QeGuDjS-GUTCbV3MSxdE62wdMYpyA%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.