# Best way to proceed after a facet click

**URL:** https://discuss.elastic.co/t/best-way-to-proceed-after-a-facet-click/4479
**Category:** Elasticsearch
**Created:** [May 26, 2011, 8:20am UTC](https://discuss.elastic.co/t/best-way-to-proceed-after-a-facet-click/4479 "2011-05-26T08:20:22Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![paranoiabla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paranoiabla/32/2160_2.png) [@paranoiabla](https://discuss.elastic.co/u/paranoiabla)
#### Post date: [May 26, 2011, 8:20am UTC](https://discuss.elastic.co/t/best-way-to-proceed-after-a-facet-click/4479/1 "2011-05-26T08:20:22Z")

</div>

Hey guys,

I am developing using the Java API, and here's the scenario: a user  
chooses a facet (for instance he chooses the BMW brand). So what shall  
I do on the server-side to narrow the selection of the user?  
I tried creating a query like this:

====

1. Create a match-all-query

XContentFilterBuilder queryFilter = FilterBuilders.matchAllFilter();  
XContentQueryBuilder query =  
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),  
queryFilter);  
searchRequestBuilder.setQuery(query)

1. And then I create a filter builder and add the term filter to it.

# andFilterBuilder.add(FilterBuilders.termsFilter("brand", "BMW")); searchRequestBuilder.setFilter(andFilterBuilder);

This actually works, but it only shrinks the results. The facets on  
the left side are still the same number as they were, before the user  
selected the facet.

So then my next approach was to add another filter (actually the same  
filter) when the facet was created:

===

1. I created the term facet:

TermsFacetBuilder termFacet =  
FacetBuilders.termsFacet(frm.getName()).field(frm.getField());

1. I create the filter

AndFilterBuilder andFilterBuilder = FilterBuilders.andFilter();  
andFilterBuilder.add(FilterBuilders.termsFilter("brand", "BMW"));

1. I add the filter to the term facet.  
termFacet.facetFilter(andFilterBuilder);

# searchRequestBuilder.addFacet(termFacet);

And, again, this works perfect, but only for the "brand" facet - the  
results are narrowed down, and the brand facets are also narrowed  
down, as expected. BUT the "price" facet, for instance, still stays  
the same!!! I can see the logic behind this - I didn't apply any  
filter on the "price" facet.

So then my next approach was to create a filter on a query-level, like  
this:

# === TermsFilterBuilder queryFilter = FilterBuilders.termsFilter("brand", "BMW"); XContentQueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), queryFilter); return searchRequestBuilder.setQuery(query);

but oddly enough, when I go to the home page, this returns no results  
whatsoever ☹ .. I tried also using FilterBuilders.inFilter("brand",  
"BMW"); but, again, I got no results whatsoever.

I really want is when a user selects a facet, to narrow his search  
results, and also to narrow ALL of his facets ("brand", "price",  
etc.). Do I need separate filters on the searchRequestBuilder and on  
the facets, or do I need one filter on the query?

I am really sorry that it took so long to explain what Im trying to  
do, and thank you for your time if you read it all.

Cheers, Petar.

---

<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 26, 2011, 10:01am UTC](https://discuss.elastic.co/t/best-way-to-proceed-after-a-facet-click/4479/2 "2011-05-26T10:01:51Z")

</div>

For what you are after, narrow everything once a user selects a facet, then using a filtered query wrapping the user query with filters representing the selected facets is the way to go. You should see results on your last try which does exactly what I suggested, assuming you see results in other areas. Maybe you want to gist a simple testcase that shows the problem you have?

On Thursday, May 26, 2011 at 11:20 AM, paranoiabla wrote:

> Hey guys,
> 
> I am developing using the Java API, and here's the scenario: a user  
> chooses a facet (for instance he chooses the BMW brand). So what shall  
> I do on the server-side to narrow the selection of the user?  
> I tried creating a query like this:
> 
> ====
> 
> 1. Create a match-all-query
> 
> XContentFilterBuilder queryFilter = FilterBuilders.matchAllFilter();  
> XContentQueryBuilder query =  
> QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),  
> queryFilter);  
> searchRequestBuilder.setQuery(query)
> 
> 1. And then I create a filter builder and add the term filter to it.
> 
> # andFilterBuilder.add(FilterBuilders.termsFilter("brand", "BMW")); searchRequestBuilder.setFilter(andFilterBuilder);
> 
> This actually works, but it only shrinks the results. The facets on  
> the left side are still the same number as they were, before the user  
> selected the facet.
> 
> So then my next approach was to add another filter (actually the same  
> filter) when the facet was created:
> 
> ===
> 
> 1. I created the term facet:
> 
> TermsFacetBuilder termFacet =  
> FacetBuilders.termsFacet(frm.getName()).field(frm.getField());
> 
> 1. I create the filter
> 
> AndFilterBuilder andFilterBuilder = FilterBuilders.andFilter();  
> andFilterBuilder.add(FilterBuilders.termsFilter("brand", "BMW"));
> 
> 1. I add the filter to the term facet.  
> termFacet.facetFilter(andFilterBuilder);
> 
> # searchRequestBuilder.addFacet(termFacet);
> 
> And, again, this works perfect, but only for the "brand" facet - the  
> results are narrowed down, and the brand facets are also narrowed  
> down, as expected. BUT the "price" facet, for instance, still stays  
> the same!!! I can see the logic behind this - I didn't apply any  
> filter on the "price" facet.
> 
> So then my next approach was to create a filter on a query-level, like  
> this:
> 
> # === TermsFilterBuilder queryFilter = FilterBuilders.termsFilter("brand", "BMW"); XContentQueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), queryFilter); return searchRequestBuilder.setQuery(query);
> 
> but oddly enough, when I go to the home page, this returns no results  
> whatsoever ☹ .. I tried also using FilterBuilders.inFilter("brand",  
> "BMW"); but, again, I got no results whatsoever.
> 
> I really want is when a user selects a facet, to narrow his search  
> results, and also to narrow ALL of his facets ("brand", "price",  
> etc.). Do I need separate filters on the searchRequestBuilder and on  
> the facets, or do I need one filter on the query?
> 
> I am really sorry that it took so long to explain what Im trying to  
> do, and thank you for your time if you read it all.
> 
> Cheers, Petar.

---

<div class="post-metadata">

### Author: ![paranoiabla](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paranoiabla/32/2160_2.png) [@paranoiabla](https://discuss.elastic.co/u/paranoiabla)
#### Post date: [May 26, 2011, 1:28pm UTC](https://discuss.elastic.co/t/best-way-to-proceed-after-a-facet-click/4479/3 "2011-05-26T13:28:39Z")

</div>

Thanks Shay

"You should see results on your last try which does exactly what I  
suggested,"

it all worked out with the last try.

Cheers, Petar

On May 26, 1:01 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> For what you are after, narrow everything once a user selects a facet, then using a filtered query wrapping the user query with filters representing the selected facets is the way to go. You should see results on your last try which does exactly what I suggested, assuming you see results in other areas. Maybe you want to gist a simple testcase that shows the problem you have?
> 
> On Thursday, May 26, 2011 at 11:20 AM, paranoiabla wrote:
> 
> > Hey guys,
> 
> > I am developing using the Java API, and here's the scenario: a user  
> > chooses a facet (for instance he chooses the BMW brand). So what shall  
> > I do on the server-side to narrow the selection of the user?  
> > I tried creating a query like this:
> 
> > ====
> > 
> > 1. Create a match-all-query
> 
> > XContentFilterBuilder queryFilter = FilterBuilders.matchAllFilter();  
> > XContentQueryBuilder query =  
> > QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),  
> > queryFilter);  
> > searchRequestBuilder.setQuery(query)
> 
> > 1. And then I create a filter builder and add the term filter to it.
> 
> > # andFilterBuilder.add(FilterBuilders.termsFilter("brand", "BMW")); searchRequestBuilder.setFilter(andFilterBuilder);
> 
> > This actually works, but it only shrinks the results. The facets on  
> > the left side are still the same number as they were, before the user  
> > selected the facet.
> 
> > So then my next approach was to add another filter (actually the same  
> > filter) when the facet was created:
> 
> > ===
> > 
> > 1. I created the term facet:
> 
> > TermsFacetBuilder termFacet =  
> > FacetBuilders.termsFacet(frm.getName()).field(frm.getField());
> 
> > 1. I create the filter
> 
> > AndFilterBuilder andFilterBuilder = FilterBuilders.andFilter();  
> > andFilterBuilder.add(FilterBuilders.termsFilter("brand", "BMW"));
> 
> > 1. I add the filter to the term facet.  
> > termFacet.facetFilter(andFilterBuilder);
> 
> > # searchRequestBuilder.addFacet(termFacet);
> 
> > And, again, this works perfect, but only for the "brand" facet - the  
> > results are narrowed down, and the brand facets are also narrowed  
> > down, as expected. BUT the "price" facet, for instance, still stays  
> > the same!!! I can see the logic behind this - I didn't apply any  
> > filter on the "price" facet.
> 
> > So then my next approach was to create a filter on a query-level, like  
> > this:
> 
> > # === TermsFilterBuilder queryFilter = FilterBuilders.termsFilter("brand", "BMW"); XContentQueryBuilder query = QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(), queryFilter); return searchRequestBuilder.setQuery(query);
> 
> > but oddly enough, when I go to the home page, this returns no results  
> > whatsoever ☹ .. I tried also using FilterBuilders.inFilter("brand",  
> > "BMW"); but, again, I got no results whatsoever.
> 
> > I really want is when a user selects a facet, to narrow his search  
> > results, and also to narrow ALL of his facets ("brand", "price",  
> > etc.). Do I need separate filters on the searchRequestBuilder and on  
> > the facets, or do I need one filter on the query?
> 
> > I am really sorry that it took so long to explain what Im trying to  
> > do, and thank you for your time if you read it all.
> 
> > Cheers, Petar.

---

<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, 4:05am UTC](https://discuss.elastic.co/t/best-way-to-proceed-after-a-facet-click/4479/4 "2017-07-06T04:05:15Z")

</div>


