# How to only return applicable nested documents

**URL:** <https://discuss.elastic.co/t/how-to-only-return-applicable-nested-documents/7729>\
**Category:** Elasticsearch\
**Created:** [May 16, 2012, 11:26pm UTC](https://discuss.elastic.co/t/how-to-only-return-applicable-nested-documents/7729 "2012-05-16T23:26:02Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Woody\_Peterson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/woody_peterson/32/830_2.png) [@Woody\_Peterson](https://discuss.elastic.co/u/Woody_Peterson)\
**Post date:** [May 16, 2012, 11:26pm UTC](https://discuss.elastic.co/t/how-to-only-return-applicable-nested-documents/7729/1 "2012-05-16T23:26:02Z")

</div>

I'd like to implement more relevant information in the search I'm exposing  
to our users, along the lines of showing locations and other nested content  
relevant to a users' filters on our 'front page' of search.

So, if I was indexing the locations of stores, I would like to be able to  
do the normal "show be stores with locations within 5 miles," and have the  
resulting stores' locations returned be ES be only those that match that  
filter. Currently, the behavior in the simplest filter case would be to  
return all stores that have one location within 5 miles, but includes _all_  
of the stores' locations in the results.

This makes sense as a default, but how do I identify which stores were  
applicable to the users' search without post-processing?

I've looked into highlighting, facets, nested documents, and filtered  
queries; I've ruled out the latter as being applicable, but beyond that  
it's not clear to me what would be the recommended strategy.

Suggestions?

---

<div class="post-metadata">

**Author:** ![Woody\_Peterson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/woody_peterson/32/830_2.png) [@Woody\_Peterson](https://discuss.elastic.co/u/Woody_Peterson)\
**Post date:** [May 16, 2012, 11:28pm UTC](https://discuss.elastic.co/t/how-to-only-return-applicable-nested-documents/7729/2 "2012-05-16T23:28:34Z")

</div>

> This makes sense as a default, but how do I identify which stores were  
> applicable to the users' search without post-processing?

Should read: how do I identify which _locations_ were applicable to the  
users' search without post-processing

On Wednesday, May 16, 2012 4:26:02 PM UTC-7, Woody Peterson wrote:

> I'd like to implement more relevant information in the search I'm exposing  
> to our users, along the lines of showing locations and other nested content  
> relevant to a users' filters on our 'front page' of search.
> 
> So, if I was indexing the locations of stores, I would like to be able to  
> do the normal "show be stores with locations within 5 miles," and have the  
> resulting stores' locations returned be ES be only those that match that  
> filter. Currently, the behavior in the simplest filter case would be to  
> return all stores that have one location within 5 miles, but includes _all_  
> of the stores' locations in the results.
> 
> This makes sense as a default, but how do I identify which stores were  
> applicable to the users' search without post-processing?
> 
> I've looked into highlighting, facets, nested documents, and filtered  
> queries; I've ruled out the latter as being applicable, but beyond that  
> it's not clear to me what would be the recommended strategy.
> 
> Suggestions?

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 20, 2012, 5:44pm UTC](https://discuss.elastic.co/t/how-to-only-return-applicable-nested-documents/7729/3 "2012-05-20T17:44:24Z")

</div>

You mean you use nested documents/mapping and you want to get back the  
nested docs that matched? You can't do it, nested is mainly used for  
filtering currently, though this feature has been asked several times and  
it makes a lot of sense. One other option is to denormalize your data and  
search on location documents.

On Thu, May 17, 2012 at 1:28 AM, Woody Peterson [woody.peterson@gmail.com](mailto:woody.peterson@gmail.com)wrote:

> > This makes sense as a default, but how do I identify which stores were  
> > applicable to the users' search without post-processing?
> 
> Should read: how do I identify which _locations_ were applicable to the  
> users' search without post-processing
> 
> On Wednesday, May 16, 2012 4:26:02 PM UTC-7, Woody Peterson wrote:
> 
> > I'd like to implement more relevant information in the search I'm  
> > exposing to our users, along the lines of showing locations and other  
> > nested content relevant to a users' filters on our 'front page' of search.
> > 
> > So, if I was indexing the locations of stores, I would like to be able to  
> > do the normal "show be stores with locations within 5 miles," and have the  
> > resulting stores' locations returned be ES be only those that match that  
> > filter. Currently, the behavior in the simplest filter case would be to  
> > return all stores that have one location within 5 miles, but includes _all_  
> > of the stores' locations in the results.
> > 
> > This makes sense as a default, but how do I identify which stores were  
> > applicable to the users' search without post-processing?
> > 
> > I've looked into highlighting, facets, nested documents, and filtered  
> > queries; I've ruled out the latter as being applicable, but beyond that  
> > it's not clear to me what would be the recommended strategy.
> > 
> > Suggestions?

---

<div class="post-metadata">

**Author:** ![Woody\_Peterson](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/woody_peterson/32/830_2.png) [@Woody\_Peterson](https://discuss.elastic.co/u/Woody_Peterson)\
**Post date:** [May 21, 2012, 4:27pm UTC](https://discuss.elastic.co/t/how-to-only-return-applicable-nested-documents/7729/4 "2012-05-21T16:27:06Z")

</div>

Thanks for the reply, I'll implement my own post-processing. Just didn't  
want to re-invent any wheels 🙂

On Sunday, May 20, 2012 10:44:24 AM UTC-7, kimchy wrote:

> You mean you use nested documents/mapping and you want to get back the  
> nested docs that matched? You can't do it, nested is mainly used for  
> filtering currently, though this feature has been asked several times and  
> it makes a lot of sense. One other option is to denormalize your data and  
> search on location documents.
> 
> On Thu, May 17, 2012 at 1:28 AM, Woody Peterson wrote:
> 
> > > This makes sense as a default, but how do I identify which stores were  
> > > applicable to the users' search without post-processing?
> > 
> > Should read: how do I identify which _locations_ were applicable to the  
> > users' search without post-processing
> > 
> > On Wednesday, May 16, 2012 4:26:02 PM UTC-7, Woody Peterson wrote:
> > 
> > > I'd like to implement more relevant information in the search I'm  
> > > exposing to our users, along the lines of showing locations and other  
> > > nested content relevant to a users' filters on our 'front page' of search.
> > > 
> > > So, if I was indexing the locations of stores, I would like to be able  
> > > to do the normal "show be stores with locations within 5 miles," and have  
> > > the resulting stores' locations returned be ES be only those that match  
> > > that filter. Currently, the behavior in the simplest filter case would be  
> > > to return all stores that have one location within 5 miles, but includes  
> > > _all_ of the stores' locations in the results.
> > > 
> > > This makes sense as a default, but how do I identify which stores were  
> > > applicable to the users' search without post-processing?
> > > 
> > > I've looked into highlighting, facets, nested documents, and filtered  
> > > queries; I've ruled out the latter as being applicable, but beyond that  
> > > it's not clear to me what would be the recommended strategy.
> > > 
> > > Suggestions?

---

<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, 3:27am UTC](https://discuss.elastic.co/t/how-to-only-return-applicable-nested-documents/7729/5 "2017-07-06T03:27:48Z")

</div>


