You could keep an instance of the search results in the dataprovider.
Then on size() and iterator() check to see if results is null. If it
is perform the query and store the results
ie:
add this to your searchdataprovider
private transient SearchResults results;
private void searchIfNecessary() {
//if(results!=null) return;
//get results
}
public int size() {
searchIfNecessary();
return results.getTotal();
}
On 18/02/2008, Java Programmer <[EMAIL PROTECTED]> wrote:
> Hello,
> I want to use DataView to present Search Results from Hibernate
> Search, but I got stuck with implementation of DataProvider - maybe
> first I show you classes:
> private class SearchDataProvider implements IDataProvider {
>
> private String query;
>
> public SearchDataProvider(String query) {
> this.query = query;
> }
>
> @Override
> public Iterator iterator(int first, int count) {
> SearchDAO.SearchResults searchedAdverts =
> searchDAO.getSearchedAdverts(this.query, first, count);
> return searchedAdverts.getResults().iterator();
> }
>
> @Override
> public IModel model(Object object) {
> return new Model((Advertisement) object);
> }
>
> @Override
> public int size() {
> SearchDAO.SearchResults searchedAdverts =
> searchDAO.getSearchedAdverts(this.query, 0, 0);
> return searchedAdverts.getCount();
> }
>
> @Override
> public void detach() {
>
> }
> }
>
> in SearchDAO I have:
> public class SearchDAO extends HibernateDaoSupport {
> public SearchResults getSearchedAdverts(final String queryString, int
> first, int count) {
> Session session = getSession(true);
> FullTextSession fullTextSession =
> Search.createFullTextSession(session);
> SearchResults results = null;
> Transaction tx = fullTextSession.beginTransaction();
> MultiFieldQueryParser parser = new MultiFieldQueryParser( new
> String[]{"title", "rawBody"},
> new StandardAnalyzer());
> Query query;
> try {
> query = parser.parse(queryString);
> FullTextQuery hibQuery =
> fullTextSession.createFullTextQuery(query,
> Advertisement.class)
> .setFirstResult(first)
> .setMaxResults(count);
> results = new SearchResults(hibQuery.list(),
> hibQuery.getResultSize());
> } catch (ParseException e) {
> e.printStackTrace();
> } finally {
> tx.commit();
> session.close();
> }
> }
> SearchResults is a wrapper for sublist of results and number of all:
> public class SearchResults {
>
> private List<Advertisement> results = null;
>
> private int count = 0;
>
> public SearchResults(List<Advertisement> results, int count) {
> super();
> this.results = results;
> this.count = count;
> }
> //getters/setters
> }
>
> As you see I have to repeat searching in size() and iterator()
> (SearchDAO.SearchResults searchedAdverts =
> searchDAO.getSearchedAdverts(this.query, 0, 0); and
> SearchDAO.SearchResults searchedAdverts =
> searchDAO.getSearchedAdverts(this.query, first, count);) in size I do
> not get collection, but searching happens because this method is
> launched before iterator() which could answer with number of all
> results as hibQuery.getResultSize();
> It's very inefficient, and I don't know how can I repair that, without
> writing my own DataView implementation. DataProvider is good for
> databases which need to make additional select count(*), but Lucene
> Hibernate Search make it in one pass.
>
> Anybody has a suggestion on this topic?
>
> Best regards,
> Adr
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
>
>
--
Ryan Gravener
http://ryangravener.com
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]