Search code examples
javaspring-bootfacethibernate-searchfaceted-search

Selected facet should not filter own group using hibernate search


If I have some Products with color and size. Jeans Blue Size L Jeans Red Size S T-shirt REd Size S and so on..

Doing a facet search using hibernate-search works as quite well. But if I select one of the criteria, I would expect that the values of the own group are not filtered. for Example

Color Red (2) Blue (3)

Size S (1) M (2) L (3)

If I selected the red color using something like this

fullTextQuery.getFacetManager().getFacetGroup(FACET_COLOR).selectFacets(colorFacet)

This selection should influence the overall Results of the Item List and the Result of the Size Facets. But of course I don't want that the Blue option to vanish. My expected behavior is pretty much how its done on bigger ecommerce sites. But in my example the selected color red also changes the own group.

I’m I missing something? This is my example:

public SearchResponse search(SearchRequest request)
    {

        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);

        QueryBuilder queryBuilder =
          fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Product.class).get();

        BooleanJunction bool = queryBuilder.bool().must(queryBuilder.all().createQuery());

        FacetingRequest colorFacetRequest = queryBuilder.facet().name(FACET_COLOR).onField(FACET_COLOR)
          .discrete().createFacetingRequest();

        FacetingRequest sizeFacetRequest = queryBuilder.facet().name(FACET_SIZE).onField(FACET_SIZE)
          .discrete().createFacetingRequest();

        FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery(bool.createQuery(), Product.class);
        fullTextQuery.getFacetManager().enableFaceting(colorFacetRequest);
        fullTextQuery.getFacetManager().enableFaceting(sizeFacetRequest);

        if (!StringUtils.isEmpty(request.color))
        {
            fullTextQuery.getFacetManager().getFacets(FACET_COLOR)
              .stream().filter(f -> f.getValue().equals(request.color)).forEach(found -> {
                  fullTextQuery.getFacetManager().getFacetGroup(FACET_COLOR).selectFacets(found);
              });
        }

        if (!StringUtils.isEmpty(request.size))
        {
            fullTextQuery.getFacetManager().getFacets(FACET_SIZE)
              .stream().filter(f -> f.getValue().equals(request.size)).forEach(found -> {
                  fullTextQuery.getFacetManager().getFacetGroup(FACET_SIZE).selectFacets(found);

              });
        }

        List<SearchFacet> colorFacet = fullTextQuery.getFacetManager().getFacets(FACET_COLOR)
          .stream().map(f -> new SearchFacet(f.getValue(), f.getCount())).collect(Collectors.toList());
        List<SearchFacet> sizeFacet = fullTextQuery.getFacetManager().getFacets(FACET_SIZE)
          .stream().map(f -> new SearchFacet(f.getValue(), f.getCount())).collect(Collectors.toList());

        return new SearchResponse(sizeFacet, colorFacet, fullTextQuery.getResultList());

    }

disabling each group before the getFacets Methods does not seem very clean

Thanks


Solution

  • There is already an enhancement request about this: https://hibernate.atlassian.net/browse/HSEARCH-713