Combining result sets from two or more queries

I'm trying to compile a DSL query to satisfy the following requirement
(test case) but I can't:


Customer documents stored in ES, each customer has nested objects of
type Invoice and each invoice has nested objects of type InvoiceLine

The only relevant fields are Invoice.InvoiceDate and

The query I'm trying to write must return all customers who:

  1. Does not have an Invoice in the last year (simple range filter)
  2. Have bought ProductNumber 123,456 at some point in time (simple in

I managed to construct a query (both in Java API and in DSL JSON) which
will find customer who bought the specified products in the last year
(which is not what I describe above).
In traditional SQL, I would write a query like:

Select * from Customer C
Exists (Select 1 from Invoice I Where I.InvoiceDate Between 'xx' and 'yy'
AND I.CustomerId = C.CustomerId)
Exists (Select 1 from Invoice I INNER JOIN InvoiceLine IL ON I.InvoiceId =
IL.InvoiceId Where I.CustomerId = C.CustomerId AND IL.ProductNumber IN

Can this be achieved in ES query/filter DSL?

My only alternative would be to perform two queries and perform the
intersection client-side, but I'd like to avoid this solution.

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
To view this discussion on the web visit
For more options, visit

1 Like