# Filtering nested documents for faceting

**URL:** <https://discuss.elastic.co/t/filtering-nested-documents-for-faceting/9913>\
**Category:** Elasticsearch\
**Created:** [December 1, 2012, 10:02pm UTC](https://discuss.elastic.co/t/filtering-nested-documents-for-faceting/9913 "2012-12-01T22:02:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dan\_Spangenberger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dan_spangenberger/32/2598_2.png) [@Dan\_Spangenberger](https://discuss.elastic.co/u/Dan_Spangenberger)\
**Post date:** [December 1, 2012, 10:02pm UTC](https://discuss.elastic.co/t/filtering-nested-documents-for-faceting/9913/1 "2012-12-01T22:02:37Z")

</div>

Hi all-

I'm trying to build a faceted navigation UX on a corpus with documents  
containing many (thousands of) properties. Users will be applying multiple  
filters (filter such as: documents must contain an assignment for property  
'X' that matches 'Y') to reduce their current document set to manageable  
size. To power this UI, I'd like to provide a 'typeahead' experience--users  
can begin typing out the names of properties (or assignments), and this  
text will be matched using match\_phrase\_prefix. That is solved for  
filtering documents (without typeahead) very easily (I already have it  
working).

What I'd like to now do is _filter facets_ based on the search term. That  
is, I'd like to return property names (and values) as the user types into a  
search box, with counts of documents that have assignments for that  
property name and value. As an example, a user has selected 'Category \>  
Camcorders' and the user is now searching the text 'Son', they will be  
presented with an option 'Manufacturer \> Sony (53)', where 53 is the count  
of documents which have a property value assignment of 'Sony' for the  
'Manufacturer' field, _as it pertains to the already applied filters_,  
i.e., there are 53 Sony camcorders in the corpus.

My (slimmed down) mapping is here:

> <https://gist.github.com/dspangen/4185356>

As in the gist, I have added assignments as nested docs, and I'm using  
nested faceting to provide an unfiltered view of the property assignments  
based on the user's current set of filters (i.e., I can show a user what  
properties and values are assigned to documents that are tagged as Sony  
Camcorders, as well as the number of documents that those properties and  
values are tagged on). But I can't figure out how to filter the facet  
results based on a user's text.

Any suggestions would be appreciated.

Thanks,

-Dan

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [December 3, 2012, 12:49pm UTC](https://discuss.elastic.co/t/filtering-nested-documents-for-faceting/9913/2 "2012-12-03T12:49:17Z")

</div>

Hi Dan,

Can you share your search request or the relevant parts of it?

At all times if you want the filter on nested fields you need to use  
the nested filter:

> **[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.

Inside the nested filter you can embed whatever filter you need to  
execute on the nested docs.

Martijn

On 2 December 2012 09:02, Dan Spangenberger [dspangenberger@salsify.com](mailto:dspangenberger@salsify.com) wrote:

> Hi all-
> 
> I'm trying to build a faceted navigation UX on a corpus with documents  
> containing many (thousands of) properties. Users will be applying multiple  
> filters (filter such as: documents must contain an assignment for property  
> 'X' that matches 'Y') to reduce their current document set to manageable  
> size. To power this UI, I'd like to provide a 'typeahead' experience--users  
> can begin typing out the names of properties (or assignments), and this text  
> will be matched using match\_phrase\_prefix. That is solved for filtering  
> documents (without typeahead) very easily (I already have it working).
> 
> What I'd like to now do is filter facets based on the search term. That is,  
> I'd like to return property names (and values) as the user types into a  
> search box, with counts of documents that have assignments for that property  
> name and value. As an example, a user has selected 'Category \> Camcorders'  
> and the user is now searching the text 'Son', they will be presented with an  
> option 'Manufacturer \> Sony (53)', where 53 is the count of documents which  
> have a property value assignment of 'Sony' for the 'Manufacturer' field, as  
> it pertains to the already applied filters, i.e., there are 53 Sony  
> camcorders in the corpus.
> 
> My (slimmed down) mapping is here:
> 
> [ES mapping for docs · GitHub](https://gist.github.com/4185356)
> 
> As in the gist, I have added assignments as nested docs, and I'm using  
> nested faceting to provide an unfiltered view of the property assignments  
> based on the user's current set of filters (i.e., I can show a user what  
> properties and values are assigned to documents that are tagged as Sony  
> Camcorders, as well as the number of documents that those properties and  
> values are tagged on). But I can't figure out how to filter the facet  
> results based on a user's text.
> 
> Any suggestions would be appreciated.
> 
> Thanks,
> 
> -Dan
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![Dan\_Spangenberger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dan_spangenberger/32/2598_2.png) [@Dan\_Spangenberger](https://discuss.elastic.co/u/Dan_Spangenberger)\
**Post date:** [December 3, 2012, 3:26pm UTC](https://discuss.elastic.co/t/filtering-nested-documents-for-faceting/9913/3 "2012-12-03T15:26:36Z")

</div>

I've updated the gist: [ES mapping for docs · GitHub](https://gist.github.com/4185356)

In the (fairly simplified) query I've provided, I can filter the document  
set to facet on, using facet\_filter, but the facet values coming back are  
not filtered--they still include entries for other manufacturers. That's  
what I'd like to filter. I suspect that this will likely involve another  
index, or a faceting plugin, but I'm hopeful that it may not (or someone  
can give me some guidance on where to start building that)

-Dan

On Monday, December 3, 2012 7:49:17 AM UTC-5, Martijn v Groningen wrote:

> Hi Dan,
> 
> Can you share your search request or the relevant parts of it?
> 
> At all times if you want the filter on nested fields you need to use  
> the nested filter:  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/nested-filter.html)
> 
> Inside the nested filter you can embed whatever filter you need to  
> execute on the nested docs.
> 
> Martijn
> 
> On 2 December 2012 09:02, Dan Spangenberger \<[dspange...@salsify.com](mailto:dspange...@salsify.com)\<javascript:\>\>  
> wrote:
> 
> > Hi all-
> > 
> > I'm trying to build a faceted navigation UX on a corpus with documents  
> > containing many (thousands of) properties. Users will be applying  
> > multiple  
> > filters (filter such as: documents must contain an assignment for  
> > property  
> > 'X' that matches 'Y') to reduce their current document set to manageable  
> > size. To power this UI, I'd like to provide a 'typeahead'  
> > experience--users  
> > can begin typing out the names of properties (or assignments), and this  
> > text  
> > will be matched using match\_phrase\_prefix. That is solved for filtering  
> > documents (without typeahead) very easily (I already have it working).
> > 
> > What I'd like to now do is filter facets based on the search term. That  
> > is,  
> > I'd like to return property names (and values) as the user types into a  
> > search box, with counts of documents that have assignments for that  
> > property  
> > name and value. As an example, a user has selected 'Category \>  
> > Camcorders'  
> > and the user is now searching the text 'Son', they will be presented  
> > with an  
> > option 'Manufacturer \> Sony (53)', where 53 is the count of documents  
> > which  
> > have a property value assignment of 'Sony' for the 'Manufacturer' field,  
> > as  
> > it pertains to the already applied filters, i.e., there are 53 Sony  
> > camcorders in the corpus.
> > 
> > My (slimmed down) mapping is here:
> > 
> > [ES mapping for docs · GitHub](https://gist.github.com/4185356)
> > 
> > As in the gist, I have added assignments as nested docs, and I'm using  
> > nested faceting to provide an unfiltered view of the property  
> > assignments  
> > based on the user's current set of filters (i.e., I can show a user what  
> > properties and values are assigned to documents that are tagged as Sony  
> > Camcorders, as well as the number of documents that those properties and  
> > values are tagged on). But I can't figure out how to filter the facet  
> > results based on a user's text.
> > 
> > Any suggestions would be appreciated.
> > 
> > Thanks,
> > 
> > -Dan
> > 
> > --
> 
> --  
> Met vriendelijke groet,
> 
> Martijn van Groningen

--

---

<div class="post-metadata">

**Author:** ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)\
**Post date:** [December 4, 2012, 6:00am UTC](https://discuss.elastic.co/t/filtering-nested-documents-for-faceting/9913/4 "2012-12-04T06:00:37Z")

</div>

Hi Dan,

Inside the `facet_filter` search request parts you also need to use  
the `nested` filters, just like you already do in the `query` request  
part.

Martijn

On 4 December 2012 02:26, Dan Spangenberger [dspangenberger@salsify.com](mailto:dspangenberger@salsify.com) wrote:

> I've updated the gist: [ES mapping for docs · GitHub](https://gist.github.com/4185356)
> 
> In the (fairly simplified) query I've provided, I can filter the document  
> set to facet on, using facet\_filter, but the facet values coming back are  
> not filtered--they still include entries for other manufacturers. That's  
> what I'd like to filter. I suspect that this will likely involve another  
> index, or a faceting plugin, but I'm hopeful that it may not (or someone can  
> give me some guidance on where to start building that)
> 
> -Dan
> 
> On Monday, December 3, 2012 7:49:17 AM UTC-5, Martijn v Groningen wrote:
> 
> > Hi Dan,
> > 
> > Can you share your search request or the relevant parts of it?
> > 
> > At all times if you want the filter on nested fields you need to use  
> > the nested filter:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/nested-filter.html)
> > 
> > Inside the nested filter you can embed whatever filter you need to  
> > execute on the nested docs.
> > 
> > Martijn
> > 
> > On 2 December 2012 09:02, Dan Spangenberger [dspange...@salsify.com](mailto:dspange...@salsify.com)  
> > wrote:
> > 
> > > Hi all-
> > > 
> > > I'm trying to build a faceted navigation UX on a corpus with documents  
> > > containing many (thousands of) properties. Users will be applying  
> > > multiple  
> > > filters (filter such as: documents must contain an assignment for  
> > > property  
> > > 'X' that matches 'Y') to reduce their current document set to manageable  
> > > size. To power this UI, I'd like to provide a 'typeahead'  
> > > experience--users  
> > > can begin typing out the names of properties (or assignments), and this  
> > > text  
> > > will be matched using match\_phrase\_prefix. That is solved for filtering  
> > > documents (without typeahead) very easily (I already have it working).
> > > 
> > > What I'd like to now do is filter facets based on the search term. That  
> > > is,  
> > > I'd like to return property names (and values) as the user types into a  
> > > search box, with counts of documents that have assignments for that  
> > > property  
> > > name and value. As an example, a user has selected 'Category \>  
> > > Camcorders'  
> > > and the user is now searching the text 'Son', they will be presented  
> > > with an  
> > > option 'Manufacturer \> Sony (53)', where 53 is the count of documents  
> > > which  
> > > have a property value assignment of 'Sony' for the 'Manufacturer' field,  
> > > as  
> > > it pertains to the already applied filters, i.e., there are 53 Sony  
> > > camcorders in the corpus.
> > > 
> > > My (slimmed down) mapping is here:
> > > 
> > > [ES mapping for docs · GitHub](https://gist.github.com/4185356)
> > > 
> > > As in the gist, I have added assignments as nested docs, and I'm using  
> > > nested faceting to provide an unfiltered view of the property  
> > > assignments  
> > > based on the user's current set of filters (i.e., I can show a user what  
> > > properties and values are assigned to documents that are tagged as Sony  
> > > Camcorders, as well as the number of documents that those properties and  
> > > values are tagged on). But I can't figure out how to filter the facet  
> > > results based on a user's text.
> > > 
> > > Any suggestions would be appreciated.
> > > 
> > > Thanks,
> > > 
> > > -Dan
> > > 
> > > --
> > 
> > --  
> > Met vriendelijke groet,
> > 
> > Martijn van Groningen
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<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:01am UTC](https://discuss.elastic.co/t/filtering-nested-documents-for-faceting/9913/5 "2017-07-06T03:01:44Z")

</div>


