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;
(...)
}
|