Best way to proceed after a facet click


(paranoiabla) #1

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 :frowning: .. 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.


(Shay Banon) #2

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 :frowning: .. 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.


(paranoiabla) #3

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 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 :frowning: .. 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.


(system) #4