# Complex or impossible query

**URL:** <https://discuss.elastic.co/t/complex-or-impossible-query/15447>\
**Category:** Elasticsearch\
**Created:** [January 28, 2014, 3:00pm UTC](https://discuss.elastic.co/t/complex-or-impossible-query/15447 "2014-01-28T15:00:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ernesto\_Reig](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ernesto_reig/32/951_2.png) [@Ernesto\_Reig](https://discuss.elastic.co/u/Ernesto_Reig)\
**Post date:** [January 28, 2014, 3:00pm UTC](https://discuss.elastic.co/t/complex-or-impossible-query/15447/1 "2014-01-28T15:00:18Z")

</div>

Hello, I have a requirement we need to implement and I really don´t know  
how to do it or even if it is possible (maybe the solution is easy but I  
just don´t know).  
We have Person documents like this:  
{  
"id": 1,  
"name": "Ernesto",  
"NID": "AAA"  
}

{  
"id": 2,  
"name": "Enrique",  
"NID": "AAA"  
}

{  
"id": 3,  
"name": "Antonio",  
"NID": "BBB"  
}

{  
"id": 4,  
"name": "Karlos",  
"NID": "CCC"  
}

Imagine we search for Persons with ["NID": "AAA"], we would get 2 documents  
(id 1 and id 2).  
If we search for Persons with ["NID": "BBB"], we would get 1 document (id  
3).  
And if we search for Persons with ["NID": "CCC"], we would get 1 document  
(id 3).  
Ok, so what we need is get all the documents in which the NID is unique. I  
mean, the \_count would be only 1 if we search for the NID of those  
documents. The results from that query/filter would be documents 3 and 4,  
since the NID value is unique for those documents (there are no more  
documents in the index with that NID).

Thank you very much,

Ernesto

--  
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/977fa0d9-1b88-4147-8073-dd4d29aff1a6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/977fa0d9-1b88-4147-8073-dd4d29aff1a6%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![Ivan](https://avatars.discourse-cdn.com/v4/letter/i/df788c/32.png) [@Ivan](https://discuss.elastic.co/u/Ivan)\
**Post date:** [January 28, 2014, 6:23pm UTC](https://discuss.elastic.co/t/complex-or-impossible-query/15447/2 "2014-01-28T18:23:00Z")

</div>

What you are probably looking for is field collapsing, which is not yet  
supported in elasticsearch (it is planned). You can use a term facet to  
retrieve the count for all terms and then do a separate query for each  
unique term. In addition to the slowness of having to do multiple queries,  
you would also face the issue of the facets not returning all the values,  
especially on fields with many values. Not sure if the new aggregations  
framework will help with this last part (I really need to try it out).

--  
Ivan

On Tue, Jan 28, 2014 at 7:00 AM, Ernesto Reig [ernirulez@gmail.com](mailto:ernirulez@gmail.com) wrote:

> Hello, I have a requirement we need to implement and I really don´t know  
> how to do it or even if it is possible (maybe the solution is easy but I  
> just don´t know).  
> We have Person documents like this:  
> {  
> "id": 1,  
> "name": "Ernesto",  
> "NID": "AAA"  
> }
> 
> {  
> "id": 2,  
> "name": "Enrique",  
> "NID": "AAA"  
> }
> 
> {  
> "id": 3,  
> "name": "Antonio",  
> "NID": "BBB"  
> }
> 
> {  
> "id": 4,  
> "name": "Karlos",  
> "NID": "CCC"  
> }
> 
> Imagine we search for Persons with ["NID": "AAA"], we would get 2  
> documents (id 1 and id 2).  
> If we search for Persons with ["NID": "BBB"], we would get 1 document (id  
> 3).  
> And if we search for Persons with ["NID": "CCC"], we would get 1 document  
> (id 3).  
> Ok, so what we need is get all the documents in which the NID is unique. I  
> mean, the \_count would be only 1 if we search for the NID of those  
> documents. The results from that query/filter would be documents 3 and 4,  
> since the NID value is unique for those documents (there are no more  
> documents in the index with that NID).
> 
> Thank you very much,
> 
> Ernesto
> 
> --  
> 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/977fa0d9-1b88-4147-8073-dd4d29aff1a6%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/977fa0d9-1b88-4147-8073-dd4d29aff1a6%40googlegroups.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).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBL%2B4T44RTucL4cHVDt4Laqpc2fsC94yox9jRxQT1R%3D5g%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CALY%3DcQBL%2B4T44RTucL4cHVDt4Laqpc2fsC94yox9jRxQT1R%3D5g%40mail.gmail.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, 1:54am UTC](https://discuss.elastic.co/t/complex-or-impossible-query/15447/3 "2017-07-06T01:54:20Z")

</div>


