简体   繁体   English

Hibernate Search-在查询上应用构面不会返回任何结果

[英]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. 第一次调用list()方法时,我得到8个产品(总共15个产品,这些产品受setMaxResults()方法限制)和两个构面的列表:一个构面为14,另一个构面为1。我将第一个方面应用于当前查询,并使用list()方法重新执行该查询,我希望看到总共14种产品,但将返回一个空列表。 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: 产品实体上的productCategoryId字段:

@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 . 此错误的票证是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: 只需删除@NumericField批注并使用org.hibernate.search.bridge.builtin.LongBridge进行索引:

@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

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

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