# Conditional filtering of documents based on other documents

**URL:** <https://discuss.elastic.co/t/conditional-filtering-of-documents-based-on-other-documents/16478>\
**Category:** Elasticsearch\
**Created:** [March 19, 2014, 8:51pm UTC](https://discuss.elastic.co/t/conditional-filtering-of-documents-based-on-other-documents/16478 "2014-03-19T20:51:37Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mahmoud\_Alnahlawi](https://avatars.discourse-cdn.com/v4/letter/m/ccd318/32.png) [@Mahmoud\_Alnahlawi](https://discuss.elastic.co/u/Mahmoud_Alnahlawi)\
**Post date:** [March 19, 2014, 8:51pm UTC](https://discuss.elastic.co/t/conditional-filtering-of-documents-based-on-other-documents/16478/1 "2014-03-19T20:51:37Z")

</div>

I have the following use case...

Document has three fields:  
id: Int  
name: String  
environment: Int

The \_id of the document is a composite id and environment.

Here's sample data

id Name environment \_id  
1 one one 1 1\_1  
1 one two 2 1\_2  
3 three one 1 3\_1

Now, I want to issue a query that give me a single document (id) per  
environment. If a document is in multiple environments, I want to pick the  
one with the highest environment number.

The solution, further more, needs to play nice with pagination.

The way I would do that with solr is using field collapsing. I would group  
the results by id (and then return the one with the highest environment).  
The number of hits is the number of groups (unique).

I considered three approaches but don't really like either.

First is to sort the results by the id and then dedup them in the client.  
Pagination would be hairy to work out and total number of hits would not  
be straight forward to get.

Second, Facet on the id. Not quite sure how this would work given that,  
based on my reading, that I can only return some sort of a count from the  
facts.

Issue multiple queries (seem like it's too much of brute force).

Ideas? Extra points if the solution works with ES 0.9.10

Thanks!

--  
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/3fea6b74-37db-40f9-80f8-3c71650c1182%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/3fea6b74-37db-40f9-80f8-3c71650c1182%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:42am UTC](https://discuss.elastic.co/t/conditional-filtering-of-documents-based-on-other-documents/16478/2 "2017-07-06T01:42:04Z")

</div>


