带有条件限制的 Hibernate 搜索返回错误的计数

发布于 2024-11-19 00:11:22 字数 1109 浏览 1 评论 0原文

结果列表是完美的,但 getResultSize() 不正确。

我已经敲出了一些代码来说明。

Criteria criteria2 = this.getSession().createCriteria(Film.class);                      

Criterion genre = Restrictions.eq("genreAlias.genreName", details.getSearch().getGenreName());
criteria2.createAlias("genres", "genreAlias", CriteriaSpecification.INNER_JOIN);
criteria2.add(genre);    

criteria2.setMaxResults(details.getMaxRows())
.setFirstResult(details.getStartResult());

FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.createFullTextEntityManager(entityManager);

org.apache.lucene.queryParser.QueryParser parser2 = new QueryParser("title", new StopAnalyzer() );

org.apache.lucene.search.Query luceneQuery2 = parser2.parse( "title:"+details.getSearch()");

FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery( luceneQuery2, Film.class);
fullTextQuery.setCriteriaQuery(criteria2);

fullTextQuery.getResultList()); // Returns the correctly filtered list
fullTextQuery.getResultSize()); // Returns the retsult size without the genre resrtiction

The result list is perfect but the getResultSize() is incorrect.

I've knocked up some code to illustrate.

Criteria criteria2 = this.getSession().createCriteria(Film.class);                      

Criterion genre = Restrictions.eq("genreAlias.genreName", details.getSearch().getGenreName());
criteria2.createAlias("genres", "genreAlias", CriteriaSpecification.INNER_JOIN);
criteria2.add(genre);    

criteria2.setMaxResults(details.getMaxRows())
.setFirstResult(details.getStartResult());

FullTextEntityManager fullTextEntityManager = org.hibernate.search.jpa.Search.createFullTextEntityManager(entityManager);

org.apache.lucene.queryParser.QueryParser parser2 = new QueryParser("title", new StopAnalyzer() );

org.apache.lucene.search.Query luceneQuery2 = parser2.parse( "title:"+details.getSearch()");

FullTextQuery fullTextQuery = fullTextEntityManager.createFullTextQuery( luceneQuery2, Film.class);
fullTextQuery.setCriteriaQuery(criteria2);

fullTextQuery.getResultList()); // Returns the correctly filtered list
fullTextQuery.getResultSize()); // Returns the retsult size without the genre resrtiction

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

念三年u 2024-11-26 00:11:22

来自 http://docs.jboss。 org/hibernate/search/3.3/api/org/hibernate/search/jpa/FullTextQuery.html

int getResultSize()

返回此搜索的命中数 注意:结果数可能与 getResultList().size() 略有不同,因为 getResultList() 在查询时可能与数据库不同步。

您应该尝试使用一些更专业的查询,例如:

    Query query = new FuzzyQuery(new Term("title", q));
    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, Film.class);
    int filmCount = fullTextQuery.getResultSize();

这就是您执行分页请求的方式(我猜您没有正确实现分页):

FullTextQuery hits = Search.getFullTextSession(getSession()).createFullTextQuery(query,      Film.class)
.setFirstResult((pageNumber - 1) * perPageItems).setMaxResults(perPageItems);

以上每次都对我有用。您应该记住 getResultSize() 的结果更多的是估计值。我经常使用分页,并且经历过页面之间的数字变化。所以你应该说“about xxxx”结果。

From http://docs.jboss.org/hibernate/search/3.3/api/org/hibernate/search/jpa/FullTextQuery.html

int getResultSize()

Returns the number of hits for this search Caution: The number of results might be slightly different from getResultList().size() because getResultList() may be not in sync with the database at the time of query.

You should try to use some of the more specialized queries like this one:

    Query query = new FuzzyQuery(new Term("title", q));
    FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(query, Film.class);
    int filmCount = fullTextQuery.getResultSize();

and this is how you do pagination requests (I'm guessing you have improperly implemented your paggination):

FullTextQuery hits = Search.getFullTextSession(getSession()).createFullTextQuery(query,      Film.class)
.setFirstResult((pageNumber - 1) * perPageItems).setMaxResults(perPageItems);

The above works for me every time. You should keep in mind that the result of getResultSize() more of estimate. I use pagination a lot and I have experienced the number changing between pages. So you should say "about xxxx" results.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文