Hibernate Search Faceting not working

405 views Asked by At

I´m programming a web application with Java EE, Hibernate Search, JPA and JSF.

I have been reading the hibernate Search documantation over and over again, but I just can´t get faceting to work properly.

I have a database which includes several categorys. I made an example with football clubs.

I have the category Germany, which has the subclases Bundesliga, 2. Bundesliga and so on. I have also a category called ChampionsLeague, EuroLeague and some other leagues representing different countries.

If I search for "Deutschland" Hibernate Search gives me the correct list of all Football clubs playing in Germany. Some of the football clubs participate in the ChampionsLeague and Euroleage. The Faceting in my left navigation bar gives me the categories in which the german clubs take part in. Also it displays the correct facetedCount.

The problem is, that if I click on one of the categories, Hibernate Search displays me all of the clubs in this category not only the german Clubs, which I have searched for on my initial search.

Can anybody tell me how to fix this problem?

Here is my code: SearchBean:

public void startKeywordSearch(){ 
fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);
qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Company.class).get();
query = qb
            .keyword()
            .fuzzy().withEditDistanceUpTo(1).withPrefixLength(0)
            .onFields("companyName", "companyShortDescription", "companyLongDescription", "categoryList.categoryName", "and so on")
            .matching(keyword)
            .createQuery();
categoryNameFacetingRequest = qb.facet()
            .name("categoryNameFacet")
            .onField("categoryList.categoryName_forFaceting")
            .discrete()
            .orderedBy(FacetSortOrder.COUNT_DESC)
            .includeZeroCounts(false)
            .maxFacetCount(100)
            .createFacetingRequest();

            persistenceQuery = fullTextEntityManager.createFullTextQuery(query, Company.class);


    facetManager = fullTextEntityManager.createFullTextQuery(query, Company.class).getFacetManager();
    facetManager.enableFaceting(categoryNameFacetingRequest);


    result = persistenceQuery.getResultList();
    facetResults = facetManager.getFacets("categoryNameFacet");
    searchCount = result.size();

Here is my Code for the addFacet Method:

public void addFacet(Facet facet) {

    fullTextEntityManager = org.hibernate.search.jpa.Search.getFullTextEntityManager(em);

    Query luceneQuery = facet.getFacetQuery();
    persistenceQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Company.class);
    facetManager.enableFaceting(categoryNameFacetingRequest);

    result = persistenceQuery.getResultList();
    facetResults = facetManager.getFacets("categoryNameFacet");

    FacetSelection facetSelection = facetManager.getFacetGroup("categoryNameFacet");
    facetSelection.selectFacets(facet);

    result = persistenceQuery.getResultList();

And thats the Code for generating my Link:

<div>
            <h:form id="facetForm">
                <ul>
                    <ui:repeat value="#{searchBean.facetResults}" var="facet">
                        <li><h:commandLink value="#{facet.value}" action="#{searchBean.addFacet(facet)}">
                                <f:ajax render="@all" />
                            </h:commandLink> (#{facet.count})</li>
                    </ui:repeat>    
                </ul>   
            </h:form>
        </div>
1

There are 1 answers

1
Hardy On BEST ANSWER

Not sure where exactly you call addFacet, but it looks like you just run the query provided by facet#getFacetQuery. This won't work. The facet query is supposed to be applied on top of the existing query. Either via a boolean query or via a FacetSelection (which acts on top of the original query). The documentation has an example for that:

// create a fulltext query
Query luceneQuery = builder.all().createQuery(); // match all query
FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery( luceneQuery, clazz );

// retrieve facet manager and apply faceting request
FacetManager facetManager = fullTextQuery.getFacetManager();
facetManager.enableFaceting( priceFacetingRequest );

// get the list of Cd
List<Cd> cds = fullTextQuery.list();
assertTrue(cds.size() == 10);

// retrieve the faceting results
List<Facet> facets = facetManager.getFacets( "priceFaceting" );
assertTrue(facets.get(0).getCount() == 2)

// apply first facet as additional search criteria
FacetSelection facetSelection = facetManager.getFacetGroup( "priceFaceting" );
facetSelection.selectFacets( facets.get( 0 ) );

// re-execute the query
cds = fullTextQuery.list();
assertTrue(cds.size() == 2);

See http://docs.jboss.org/hibernate/search/5.3/reference/en-US/html_single/#_restricting_query_results