-->
These old forums are deprecated now and set to read-only. We are waiting for you on our new forums!
More modern, Discourse-based and with GitHub/Google/Twitter authentication built-in.

All times are UTC - 5 hours [ DST ]



Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 
Author Message
 Post subject: Pagination with Hibernate Search and Criteria API issue
PostPosted: Tue Oct 30, 2007 11:01 am 
Newbie

Joined: Tue Oct 30, 2007 10:35 am
Posts: 2
Hi All,

I am trying to implement paging (using setFirstResult and setMaxResults) over a search result that I have from Hibernate Search and Criteria API. Using Hibernate Search I search Products using theirs description and I limit the result further using some criteria expressed with Criteria API.

The problem is that the size of a collection that I have on the output is not what it should be (or at least what I want). Simply put, the limit is applied to the Hibernate Search query and not to the overall query. Criteria discard some objects so I always have less objects then I intend. E.g. I want all the products that contain word "helmet" and are cheaper than 100 USD, and I want to have 10 of them. Let’s say that 2 helmets from the first 10 found by Hibernate Search are more than 100 USD, then the size of a resulting collection is 8… but should be 10.

Isn't that a bug? My code is:

FullTextSession fullTextSession = Search.createFullTextSession(session);

MultiFieldQueryParser queryParser =
new MultiFieldQueryParser(new String[] { "name", "descr" }, new StandardAnalyzer());

Criteria criteria = session
.createCriteria(Product.class)
.createAlias("offers", "offer")
.add(Restrictions.lt("offer.price", 100));

Query hibernateQuery = fullTextSession
.createFullTextQuery(queryParser.parse(searchQuery), Product.class)
.setCriteriaQuery(criteria).setFirstResult(offset).setFetchSize(size);

products = hibernateQuery.list();

@Entity
public class Product {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY) @DocumentId
private Long productId;

@OneToMany(mappedBy="product")
private Set<ProductOffer> offers;

@Field(index = Index.TOKENIZED, store=Store.NO)
private String descr;

(...)
}

@Entity
public class ProductOffer {

@Id @GeneratedValue(strategy = GenerationType.IDENTITY)
private Long offerId;

@ManyToOne @JoinColumn(name="productId")
private Product product;

private Double price;

(...)
}


Top
 Profile  
 
 Post subject:
PostPosted: Tue Oct 30, 2007 1:40 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
You cannot apply restrictions on the criteria you use, it's only to change fetching strategy.
So you need to move your price restriction to the Lucene query rather than to the Criteria query.

Your use case (mixing SQL and index query) is sort of legitimate but implementing such a thing would be fairly inefficient I suspect so it's low on my list.

_________________
Emmanuel


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 5:06 am 
Newbie

Joined: Tue Oct 30, 2007 10:35 am
Posts: 2
I cannot apply restrictions? Hmmm... I haven't run heavy testings on that, but from what I've run I believe that "mixing SQL and index query" actually works. The thing is that paging on such a mix doesn't work.

OK, I will try to include collection of ProductOffers in the index and will see if it works. THX for your reply.


Top
 Profile  
 
 Post subject:
PostPosted: Wed Oct 31, 2007 2:53 pm 
Hibernate Team
Hibernate Team

Joined: Sun Sep 14, 2003 3:54 am
Posts: 7256
Location: Paris, France
It works in certain situations but not every time, especially the pagination. Today I just consider it as a non supported feature.

_________________
Emmanuel


Top
 Profile  
 
Display posts from previous:  Sort by  
Forum locked This topic is locked, you cannot edit posts or make further replies.  [ 4 posts ] 

All times are UTC - 5 hours [ DST ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
© Copyright 2014, Red Hat Inc. All rights reserved. JBoss and Hibernate are registered trademarks and servicemarks of Red Hat, Inc.