# Adaptive faceted searching

**URL:** https://discuss.elastic.co/t/adaptive-faceted-searching/10622
**Category:** Elasticsearch
**Created:** [February 5, 2013, 7:40am UTC](https://discuss.elastic.co/t/adaptive-faceted-searching/10622 "2013-02-05T07:40:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![JL1](https://avatars.discourse-cdn.com/v4/letter/j/90db22/32.png) [@JL1](https://discuss.elastic.co/u/JL1)
#### Post date: [February 5, 2013, 7:40am UTC](https://discuss.elastic.co/t/adaptive-faceted-searching/10622/1 "2013-02-05T07:40:14Z")

</div>

Hi,

Filters can narrow down the number of documents returned in the search, but  
this won't affect the document counts per facet due to the narrowing  
applied by the filter.

I want to be able to select any number of facets in my search solution (via  
facet filters) and then have the list of facets update with the number of  
results per facet after the narrowing has been applied. This means that,  
of the documents remain after the facet filter has been applied, the list  
of facets will return only the counts that apply to the remaining  
documents.

How can I achieve this adaptive faceted search in an elasticsearch query?

//  
Example:  
I have 10 documents in my search index, 5 of which have the tag "rails",  
and all 10 of which have the tag "ruby"

if I select the "rails" tag, then I should end up with the following  
available facets (and counts):

- ruby (5)
- rails (5)

If I select the "ruby" tag, then I should end up with the following  
available facets (and counts):

- ruby (10)
- rails (5)

If I select both the "ruby" and the "rails" tags (this means results that  
have both the ruby tag and the rails tag), then I should end up with the  
following facets (and counts):

- ruby (5)
- rails (5)

--  
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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 5, 2013, 7:46am UTC](https://discuss.elastic.co/t/adaptive-faceted-searching/10622/2 "2013-02-05T07:46:03Z")

</div>

You have to apply the same filters to each facet using facet filter:  
{  
"facets" : {  
"" : {  
"" : {  
...  
},  
"facet\_filter" : {  
"term" : { "user" : "kimchy"}  
}  
}  
}  
}  
HTH  
David

Le 5 févr. 2013 à 08:40, JL [jonathan.liang@gmail.com](mailto:jonathan.liang@gmail.com) a écrit :

> Hi,
> 
> Filters can narrow down the number of documents returned in the search, but this won't affect the document counts per facet due to the narrowing applied by the filter.
> 
> I want to be able to select any number of facets in my search solution (via facet filters) and then have the list of facets update with the number of results per facet after the narrowing has been applied. This means that, of the documents remain after the facet filter has been applied, the list of facets will return only the counts that apply to the remaining documents.
> 
> How can I achieve this adaptive faceted search in an elasticsearch query?
> 
> //  
> Example:  
> I have 10 documents in my search index, 5 of which have the tag "rails", and all 10 of which have the tag "ruby"
> 
> if I select the "rails" tag, then I should end up with the following available facets (and counts):
> 
> - ruby (5)
> - rails (5)
> 
> If I select the "ruby" tag, then I should end up with the following available facets (and counts):
> 
> - ruby (10)
> - rails (5)
> 
> If I select both the "ruby" and the "rails" tags (this means results that have both the ruby tag and the rails tag), then I should end up with the following facets (and counts):
> 
> - ruby (5)
> - rails (5)
> 
> --  
> 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).  
> 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).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

### Author: ![roytmana](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/roytmana/32/44855_2.png) [@roytmana](https://discuss.elastic.co/u/roytmana)
#### Post date: [February 5, 2013, 6:10pm UTC](https://discuss.elastic.co/t/adaptive-faceted-searching/10622/3 "2013-02-05T18:10:12Z")

</div>

Does it make any difference if I do it via filtered search? This way I can  
combine it with search and also limit hits using my facet-derived filter  
(do not know if facet\_filter affects number of hits or not - have not tried  
it yet). I've built entirely generic facet navigation UI using this  
approach seems to work well...

"query": {  
"filtered": {  
"query": {  
"match\_all": {}  
},  
"filter": {  
"bool": {  
"must": [  
{  
"terms": {  
"award.type.id": [4000, 1000]  
}  
},  
{  
"terms": {  
"fy": [2012, 2011, 2010, 2009, 2008, 2007, 2006, 2005]  
}  
}  
]  
}  
}  
}  
}

--  
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).  
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, 2:52am UTC](https://discuss.elastic.co/t/adaptive-faceted-searching/10622/4 "2017-07-06T02:52:53Z")

</div>


