# Returning (at least) one of each filter

**URL:** <https://discuss.elastic.co/t/returning-at-least-one-of-each-filter/11444>\
**Category:** Elasticsearch\
**Created:** [April 4, 2013, 5:08am UTC](https://discuss.elastic.co/t/returning-at-least-one-of-each-filter/11444 "2013-04-04T05:08:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Akhil\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Akhil\_Patel](https://discuss.elastic.co/u/Akhil_Patel)\
**Post date:** [April 4, 2013, 5:08am UTC](https://discuss.elastic.co/t/returning-at-least-one-of-each-filter/11444/1 "2013-04-04T05:08:40Z")

</div>

Given I have an index that has documents of flights with fields for origin  
and destination airports, how can I construct a query that given a single  
origin and a list of destinations, I will get at least 1 of each  
origin-destination pairing?

--  
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+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [April 4, 2013, 7:23am UTC](https://discuss.elastic.co/t/returning-at-least-one-of-each-filter/11444/2 "2013-04-04T07:23:03Z")

</div>

Do you have one instance of origin-destination airports or many of them in a single doc?  
If you have many of them, you should first consider to use nested docs.

That said, you can build a BoolFilter [2] with the first must clause with a TermFilter [3] on origin and the second must clause with a TermsFilter [1] on destination.

HTH

[1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter/)  
[2] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter/)  
[3] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/term-filter/)

--  
David Pilato | Technical Advocate | [Elasticsearch.com](http://Elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 4 avr. 2013 à 07:08, Akhil Patel [akhil.patel@gmail.com](mailto:akhil.patel@gmail.com) a écrit :

> Given I have an index that has documents of flights with fields for origin and destination airports, how can I construct a query that given a single origin and a list of destinations, I will get at least 1 of each origin-destination pairing?
> 
> --  
> 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+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Akhil\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Akhil\_Patel](https://discuss.elastic.co/u/Akhil_Patel)\
**Post date:** [April 4, 2013, 1:57pm UTC](https://discuss.elastic.co/t/returning-at-least-one-of-each-filter/11444/3 "2013-04-04T13:57:08Z")

</div>

Right, so the index could have 13 docs like this:

SEA LHR 1  
SEA LHR 2  
SEA DFW 1  
SEA LAX 1  
SEA SFO 1  
SFO YVR 1  
TPE BKK 1  
SEA SYD 1  
LAS JFK 1  
SEA DFW 2  
SEA EDI 1  
AMS DAR 1  
SEA BOM 1

And I provide it SEA as the origin airport and LAX,LHR and BOM as  
destinations. The documents I'd like returned are:

SEA-LHR 1  
SEA-LHR 2  
SEA-LAX 1  
SEA-BOM 1

Is that still possible using the method you described above, and what would  
it look like?

On Thursday, April 4, 2013 12:23:03 AM UTC-7, David Pilato wrote:

> Do you have one instance of origin-destination airports or many of them in  
> a single doc?  
> If you have many of them, you should first consider to use nested docs.
> 
> That said, you can build a BoolFilter [2] with the first must clause with  
> a TermFilter [3] on origin and the second must clause with a  
> TermsFilter [1] on destination.
> 
> HTH
> 
> [1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter/)  
> [2] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter/)  
> [3] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/term-filter/)
> 
> --  
> _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> 
> Le 4 avr. 2013 à 07:08, Akhil Patel \<[akhil...@gmail.com](mailto:akhil...@gmail.com) \<javascript:\>\> a  
> écrit :
> 
> Given I have an index that has documents of flights with fields for origin  
> and destination airports, how can I construct a query that given a single  
> origin and a list of destinations, I will get at least 1 of each  
> origin-destination pairing?
> 
> --  
> 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Akhil\_Patel](https://avatars.discourse-cdn.com/v4/letter/a/7ea924/32.png) [@Akhil\_Patel](https://discuss.elastic.co/u/Akhil_Patel)\
**Post date:** [April 4, 2013, 4:43pm UTC](https://discuss.elastic.co/t/returning-at-least-one-of-each-filter/11444/4 "2013-04-04T16:43:39Z")

</div>

I guess I should also clarify; if there are \> 10,000 documents, I'd like to  
restrict my query to returning me 100 that guarantees I have something that  
matches each thing in my terms filter.

On Thursday, April 4, 2013 6:57:08 AM UTC-7, Akhil Patel wrote:

> Right, so the index could have 13 docs like this:
> 
> SEA LHR 1  
> SEA LHR 2  
> SEA DFW 1  
> SEA LAX 1  
> SEA SFO 1  
> SFO YVR 1  
> TPE BKK 1  
> SEA SYD 1  
> LAS JFK 1  
> SEA DFW 2  
> SEA EDI 1  
> AMS DAR 1  
> SEA BOM 1
> 
> And I provide it SEA as the origin airport and LAX,LHR and BOM as  
> destinations. The documents I'd like returned are:
> 
> SEA-LHR 1  
> SEA-LHR 2  
> SEA-LAX 1  
> SEA-BOM 1
> 
> Is that still possible using the method you described above, and what  
> would it look like?
> 
> On Thursday, April 4, 2013 12:23:03 AM UTC-7, David Pilato wrote:
> 
> > Do you have one instance of origin-destination airports or many of them  
> > in a single doc?  
> > If you have many of them, you should first consider to use nested docs.
> > 
> > That said, you can build a BoolFilter [2] with the first must clause with  
> > a TermFilter [3] on origin and the second must clause with a  
> > TermsFilter [1] on destination.
> > 
> > HTH
> > 
> > [1] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/terms-filter/)  
> > [2] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/bool-filter/)  
> > [3] [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/term-filter/)
> > 
> > --  
> > _David Pilato_ | _Technical Advocate_ | _[Elasticsearch.com](http://Elasticsearch.com)_  
> > @dadoonet [https://twitter.com/dadoonet](https://twitter.com/dadoonet) | @elasticsearchfr[https://twitter.com/elasticsearchfr](https://twitter.com/elasticsearchfr)  
> > | @scrutmydocs [https://twitter.com/scrutmydocs](https://twitter.com/scrutmydocs)
> > 
> > Le 4 avr. 2013 à 07:08, Akhil Patel [akhil...@gmail.com](mailto:akhil...@gmail.com) a écrit :
> > 
> > Given I have an index that has documents of flights with fields for  
> > origin and destination airports, how can I construct a query that given a  
> > single origin and a list of destinations, I will get at least 1 of each  
> > origin-destination pairing?
> > 
> > --  
> > 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 [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com).  
> > For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:42am UTC](https://discuss.elastic.co/t/returning-at-least-one-of-each-filter/11444/5 "2017-07-06T02:42:44Z")

</div>


