简体   繁体   中英

Hibernate Search - applying facet on a query brings back no results

I'm trying to narrow down my original query with one of the returned facets, but this doesn't seem to work, ie it brings back no results. The first time I call list() method, I get 8 products (out of 15 total, these are limited by setMaxResults() method) and the list of two facets: one with the count 14 and the other one with count 1. When I apply the first facet to the current query and re-execute the query with the list() method, I am expecting to see a total of 14 products, but an empty list is being returned instead. I can't see anything I'm doing that's different from the documentation.

Bulding query:

Session session = entityManager.unwrap(Session.class);
        FullTextSession fullTextSession = org.hibernate.search.Search.getFullTextSession(session);

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

        Query luceneQuery = queryBuilder.all().createQuery();

        FacetingRequest categoryFacetingRequest = queryBuilder.facet()
                .name("categoryFaceting")
                .onField("productCategoryId")
                .discrete()
                .orderedBy(FacetSortOrder.COUNT_DESC)
                .includeZeroCounts(false)
                .createFacetingRequest();

        FullTextQuery hibernateQuery = fullTextSession.createFullTextQuery(luceneQuery, Product.class);

        hibernateQuery.setFirstResult(pageable.getOffset());
        hibernateQuery.setMaxResults(pageable.getPageSize());

        FacetManager facetManager = hibernateQuery.getFacetManager();
        facetManager.enableFaceting(categoryFacetingRequest);

        @SuppressWarnings("unchecked")
        List<Product> products = hibernateQuery.list();

        List<Facet> categoryFacets = facetManager.getFacets("categoryFaceting");

        FacetSelection facetSelection = facetManager.getFacetGroup("categoryFaceting");
        facetSelection.selectFacets(categoryFacets.get(0));
        products = hibernateQuery.list();

productCategoryId field on Product entity:

@Field(analyze = Analyze.NO)
@NumericField
@Facet(encoding = FacetEncodingType.STRING)
private Long productCategoryId;

I am afraid this is a bug. Thanks for asking though: now that we are aware of it, we can work on fixing it. The ticket for this bug is HSEARCH-2668 .

There is a workaround though: you can make your field non-numeric. Just remove the @NumericField annotation and use org.hibernate.search.bridge.builtin.LongBridge to index it:

@Field(analyze = Analyze.NO, bridge = @FieldBridge(impl = org.hibernate.search.bridge.builtin.LongBridge.class))
@Facet(encoding = FacetEncodingType.STRING)
private Long productCategoryId;

If you need this field for other purposes, you can just create a second field, and apply faceting on that second field:

@Field(analyze = Analyze.NO)
@NumericField
@Field(name = "productCategoryId_facet", analyze = Analyze.NO, bridge = @FieldBridge(impl = org.hibernate.search.bridge.builtin.LongBridge.class))
@Facet(forField = "productCategoryId_facet", encoding = FacetEncodingType.STRING)
private Long productCategoryId;
// Then use productCategoryId_facet instead of productCategoryId in your facet-enabled queries

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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