1 12 13 package com.openedit.modules.search; 14 15 import java.util.Iterator ; 16 17 import org.apache.lucene.analysis.Analyzer; 18 import org.apache.lucene.analysis.standard.StandardAnalyzer; 19 import org.apache.lucene.queryParser.QueryParser; 20 import org.apache.lucene.search.Hits; 21 import org.apache.lucene.search.IndexSearcher; 22 import org.apache.lucene.search.Query; 23 import org.apache.lucene.search.Searcher; 24 25 import com.openedit.OpenEditException; 26 import com.openedit.WebPageRequest; 27 import com.openedit.modules.BaseModule; 28 29 30 37 public class SearchCommand extends BaseModule 38 { 39 42 public void search( WebPageRequest inContext ) throws OpenEditException 43 { 44 45 String q = inContext.getRequestParameter("query"); 46 47 String path = getRoot().getAbsolutePath(); 48 49 try 50 { 51 Searcher searcher = new IndexSearcher(path + "/search/index"); 52 53 Iterator hits = search(q, searcher); 54 inContext.putPageValue("hits", hits); 55 inContext.putPageValue("searcher", searcher); 56 57 } 58 catch (Exception ex) 59 { 60 throw new OpenEditException(ex); 61 } 62 } 63 64 75 public Iterator search(String inQuery, Searcher searcher) 76 throws Exception 77 { 78 try 79 { 80 Analyzer analyzer = new StandardAnalyzer(); 82 83 if ((inQuery == null) || (inQuery.length() == -1)) 84 { 85 return null; 86 } 87 88 Query query = new QueryParser("contents", analyzer).parse(inQuery); 89 90 Hits hits = searcher.search(query); 92 93 return new HitIterator(hits); 95 96 129 130 } 132 catch (Exception e) 133 { 134 throw new OpenEditException(e); 135 } 136 } 137 } 138 | Popular Tags |