简体   繁体   English

Hibernate Search Faceting无法正常工作

[英]Hibernate Search Faceting not working

I´m programming a web application with Java EE, Hibernate Search, JPA and JSF. 我正在用Java EE,Hibernate Search,JPA和JSF编程Web应用程序。

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. 我的类别为德国,其中包含德甲联赛,2德甲联赛等。 I have also a category called ChampionsLeague, EuroLeague and some other leagues representing different countries. 我也有一个类别,称为ChampionsLeague,EuroLeague和其他一些代表不同国家的联赛。

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. 一些足球俱乐部参加了ChampionsLeague和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. 我左侧导航栏中的“构面”提供了德国俱乐部参加的类别。它还显示正确的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. 问题是,如果我单击一个类别,则Hibernate Search会向我显示该类别中的所有俱乐部,而不仅仅是我最初搜索时所搜索的德国俱乐部。

Can anybody tell me how to fix this problem? 谁能告诉我如何解决这个问题?

Here is my code: SearchBean: 这是我的代码: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: 这是我的addFacet方法的代码:

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>

Not sure where exactly you call addFacet , but it looks like you just run the query provided by facet#getFacetQuery . 不知道确切地在哪里调用addFacet ,但是看起来您只是在运行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). 通过布尔查询或FacetSelection (作用在原始查询之上)。 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 参见http://docs.jboss.org/hibernate/search/5.3/reference/zh-CN/html_single/#_restricting_query_results

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM