//implement an interator which keep the id/class for each hit and get the object on demand
//cause I can't keep the searcher and hence the hit opened. I dont have any hook to know when the
//user stop using it
//scrollable is better in this area
SearchFactoryImplementor searchFactoryImplementor = ContextHelper.getSearchFactoryBySFI( session );
//find the directories
IndexSearcher searcher = buildSearcher( searchFactoryImplementor );
if ( searcher == null ) {
return new IteratorImpl( Collections.EMPTY_LIST, noLoader );
}
try {
QueryAndHits queryAndHits = getQueryAndHits( searcher );
int first = first();
int max = max( first, queryAndHits.hits );
Session sess = (Session) this.session;
int size = max - first + 1 < 0 ? 0 : max - first + 1;
List<EntityInfo> infos = new ArrayList<EntityInfo>( size );
DocumentExtractor extractor = new DocumentExtractor( queryAndHits.preparedQuery, searcher, searchFactoryImplementor, indexProjection );
for (int index = first; index <= max; index++) {
//TODO use indexSearcher.getIndexReader().document( hits.id(index), FieldSelector(indexProjection) );
infos.add( extractor.extract( queryAndHits.hits, index ) );
}
Loader loader = getLoader( sess, searchFactoryImplementor );
return new IteratorImpl( infos, loader );
}
catch (IOException e) {
throw new HibernateException( "Unable to query Lucene index", e );
}
finally {
try {
closeSearcher( searcher, searchFactoryImplementor.getReaderProvider() );
}
catch (SearchException e) {
log.warn( "Unable to properly close searcher during lucene query: " + getQueryString(), e );
}
}