# Deduplicating using nested query

**URL:** <https://discuss.elastic.co/t/deduplicating-using-nested-query/18044>\
**Category:** Elasticsearch\
**Created:** [June 11, 2014, 12:52pm UTC](https://discuss.elastic.co/t/deduplicating-using-nested-query/18044 "2014-06-11T12:52:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jilles\_van\_Gurp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jilles_van_gurp/32/879_2.png) [@Jilles\_van\_Gurp](https://discuss.elastic.co/u/Jilles_van_Gurp)\
**Post date:** [June 11, 2014, 12:52pm UTC](https://discuss.elastic.co/t/deduplicating-using-nested-query/18044/1 "2014-06-11T12:52:20Z")

</div>

I have a simple problem where it would be useful to a query like: get me  
everything that matches except if field foo is in results of  
.

The simple solution is to first do query2, fetch the foo field for all the  
results (potentially thousands), stuff it in some hash and generate a  
gigantic set with all the values of the foo field and turn that into a bool  
filter with a lot of term queries in the not that gets added to query1.

Since I need to support paging through the results, I can't get away with  
simply tossing deduplicating the result set in memory.

Is there a more elegant way to do this in elasticsearch?

Jilles

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/b0567016-730c-4531-bf8a-84eb50e4579b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/b0567016-730c-4531-bf8a-84eb50e4579b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Antonio\_Augusto\_Sant](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/antonio_augusto_sant/32/82851_2.png) [@Antonio\_Augusto\_Sant](https://discuss.elastic.co/u/Antonio_Augusto_Sant)\
**Post date:** [June 11, 2014, 2:56pm UTC](https://discuss.elastic.co/t/deduplicating-using-nested-query/18044/2 "2014-06-11T14:56:57Z")

</div>

Don't know for sure, but did you try using a filter query ?

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

On Wednesday, June 11, 2014 9:52:20 AM UTC-3, Jilles van Gurp wrote:

> I have a simple problem where it would be useful to a query like: get me  
> everything that matches except if field foo is in results of  
> .
> 
> The simple solution is to first do query2, fetch the foo field for all the  
> results (potentially thousands), stuff it in some hash and generate a  
> gigantic set with all the values of the foo field and turn that into a bool  
> filter with a lot of term queries in the not that gets added to query1.
> 
> Since I need to support paging through the results, I can't get away with  
> simply tossing deduplicating the result set in memory.
> 
> Is there a more elegant way to do this in elasticsearch?
> 
> Jilles

--  
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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/69009623-e0d9-406f-8217-a2e5c26a2515%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/69009623-e0d9-406f-8217-a2e5c26a2515%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:23am UTC](https://discuss.elastic.co/t/deduplicating-using-nested-query/18044/3 "2017-07-06T01:23:15Z")

</div>


