简体   繁体   中英

Hibernate search empty result with working query

Hibernate: 5.2.8.Final
Hibernate Search: 5.7.0.Final
Configuration:

spring.jpa.properties.hibernate.search.analyzer=org.apache.lucene.analysis.en.EnglishAnalyzer
logging.level.org.hibernate.search.fulltext_query=debug

My code:

FullTextEntityManager ftsEntityManager = Search.getFullTextEntityManager(entityManager);
        QueryBuilder queryBuilder = ftsEntityManager.getSearchFactory()
                .buildQueryBuilder().forEntity(Report.class).get();
        BooleanQuery.Builder builder = new BooleanQuery.Builder();
        BooleanQuery.Builder cmpBuilder = new BooleanQuery.Builder();
        companies.forEach(cmp -> cmpBuilder.add(
                NumericRangeQuery.newLongRange("merchant.company.company_id", cmp.getId(), cmp.getId(), true, true),
                BooleanClause.Occur.SHOULD));
        builder.add(cmpBuilder.build(), BooleanClause.Occur.MUST);
        builder.add(new TermQuery(new Term("status", status.name())), BooleanClause.Occur.MUST);
        org.apache.lucene.search.Query fromQuery = queryBuilder.range()
                .onField("createdAt")
                .above(from)
                .createQuery();
        org.apache.lucene.search.Query toQuery = queryBuilder.range()
                .onField("createdAt")
                .below(to)
                .createQuery();
        builder.add(fromQuery, BooleanClause.Occur.MUST);
        builder.add(toQuery, BooleanClause.Occur.MUST);           
        FullTextQuery fullTextQuery = ftsEntityManager.createFullTextQuery(builder.build(), Report.class);
        int results = fullTextQuery.getResultSize();
        EntityGraph graph = this.entityManager.getEntityGraph("Report.fetchMerchantAndAccount");
        List resultList = fullTextQuery
                .setFirstResult(pageable.getOffset())
                .setMaxResults(pageable.getPageSize())
                .setHint("javax.persistence.fetchgraph", graph)
                .getResultList();

Logged query:

2017-03-24 02:39:50.942 DEBUG 1984 --- [tp1560534902-96] org.hibernate.search.fulltext_query      : HSEARCH000274: Executing Lucene query '+(merchant.company.company_id:[2 TO 2]) +status:COMPLETE +createdAt:[0000020161102000000000000000 TO *] +createdAt:[* TO 0000020170324235959999999999]'

I've tested this query in the Luke and it works well: 在此处输入图片说明 Looks like the problem in this parameters +(merchant.company.company_id:[2 TO 2]) +status:COMPLETE but I can't figure out what is wrong.

The problem was I should use QueryBuilder instead of Lucene classes. So, changing this:

companies.forEach(cmp -> cmpBuilder.add(
                NumericRangeQuery.newLongRange("merchant.company.company_id", cmp.getId(), cmp.getId(), true, true),
                BooleanClause.Occur.SHOULD));

to this:

companies.forEach(cmp -> cmpBuilder.add(queryBuilder.range().onField("merchant.company.company_id")
                .from(cmp.getId()).to(cmp.getId()).createQuery(), BooleanClause.Occur.SHOULD));

and this:

builder.add(new TermQuery(new Term("status", status.name())), BooleanClause.Occur.MUST);

to this:

builder.add(queryBuilder.keyword().onField("status").matching(status).createQuery(), BooleanClause.Occur.MUST);

resolves a problem!

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