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:
====
- Create a match-all-query
XContentFilterBuilder queryFilter = FilterBuilders.matchAllFilter();
XContentQueryBuilder query =
QueryBuilders.filteredQuery(QueryBuilders.matchAllQuery(),
queryFilter);
searchRequestBuilder.setQuery(query)
- 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:
===
- I created the term facet:
TermsFacetBuilder termFacet =
FacetBuilders.termsFacet(frm.getName()).field(frm.getField());
- I create the filter
AndFilterBuilder andFilterBuilder = FilterBuilders.andFilter();
andFilterBuilder.add(FilterBuilders.termsFilter("brand", "BMW"));
- 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.