1 package net.sf.cantina.search; 2 3 import net.sf.cantina.ListIterator; 4 import org.apache.lucene.search.Hits; 5 import org.apache.lucene.search.Query; 6 import org.apache.lucene.search.Searcher; 7 import org.apache.lucene.queryParser.QueryParser; 8 import org.apache.lucene.analysis.standard.StandardAnalyzer; 9 import org.apache.log4j.Logger; 10 11 /*** 12 * @author Stephane JAIS 13 */ 14 public class LuceneDocumentSearchHandler 15 extends ListIterator 16 { 17 private final static Logger logger = Logger.getLogger(LuceneDocumentSearchHandler.class); 18 Hits itsHits; 19 Query itsQuery; 20 QueryParser itsQueryParser; 21 Searcher itsSearcher; 22 23 public LuceneDocumentSearchHandler(Searcher searcher, String query) 24 throws Exception 25 { 26 itsSearcher = searcher; 27 itsQueryParser = new QueryParser("content",new StandardAnalyzer()); 28 itsQuery = itsQueryParser.parse(query); 29 } 30 public void executeSearch() 31 throws Exception 32 { 33 logger.debug("Executing query: "+itsQuery.toString("content")); 34 itsHits = itsSearcher.search(itsQuery); 35 } 36 public Object getElementAt(int index) 37 throws Exception 38 { 39 logger.debug("Search result explanation: "+itsSearcher.explain(itsQuery,index).toString()); 40 return new LuceneDocumentSearchResult( 41 itsHits.doc(index), 42 itsQuery, 43 itsHits.score(index)); 44 } 45 46 public int getSize() 47 { 48 return itsHits.length(); 49 } 50 } 51