1 16 package scriptella.driver.lucene; 17 18 import org.apache.lucene.analysis.Analyzer; 19 import org.apache.lucene.analysis.standard.StandardAnalyzer; 20 import org.apache.lucene.document.Document; 21 import org.apache.lucene.index.IndexReader; 22 import org.apache.lucene.queryParser.MultiFieldQueryParser; 23 import org.apache.lucene.queryParser.ParseException; 24 import org.apache.lucene.queryParser.QueryParser; 25 import org.apache.lucene.search.Hits; 26 import org.apache.lucene.search.IndexSearcher; 27 import org.apache.lucene.search.Searcher; 28 import scriptella.expression.PropertiesSubstitutor; 29 import scriptella.spi.ParametersCallback; 30 import scriptella.spi.QueryCallback; 31 import scriptella.util.ExceptionUtils; 32 import scriptella.util.IOUtils; 33 34 import java.io.Closeable ; 35 import java.io.IOException ; 36 import java.io.Reader ; 37 import java.util.Collection ; 38 import java.util.logging.Level ; 39 import java.util.logging.Logger ; 40 41 47 public class LuceneQuery implements ParametersCallback, Closeable { 48 49 private static final Logger LOG = Logger.getLogger(LuceneQuery.class.getName()); 50 private PropertiesSubstitutor substitutor = new PropertiesSubstitutor(); 51 private String indexPath; 52 private Document result; 53 private QueryCallback queryCallback; 54 55 62 public LuceneQuery(String indexPath, ParametersCallback parametersCallback, QueryCallback queryCallback) { 63 this.indexPath = indexPath; 64 substitutor = new PropertiesSubstitutor(parametersCallback); 65 this.queryCallback = queryCallback; 66 } 67 68 76 public void execute(Reader queryReader, final Collection <String > fields, final Boolean useMultiFieldParser, final Boolean useLowercaseExpandedTerms) { 77 IndexReader ir = null; 78 Searcher searcher = null; 79 try { 80 try { 81 ir = IndexReader.open(indexPath); 82 } catch (IOException e) { 83 throw new LuceneProviderException("Failed to open index " + indexPath, e); 84 } 85 searcher = new IndexSearcher(ir); 86 final Analyzer analyzer = new StandardAnalyzer(); 87 String queryContent; 88 try { 89 queryContent = IOUtils.toString(queryReader); 90 } catch (IOException e) { 91 throw new LuceneProviderException("Failed to load query content.", e); 92 } 93 if (useMultiFieldParser) { 94 QueryParser p = new MultiFieldQueryParser(fields.toArray(new String [fields.size()]), analyzer); 95 p.setLowercaseExpandedTerms(useLowercaseExpandedTerms); 96 try { 97 iterate(searcher.search(p.parse(queryContent))); 98 } catch (IOException e) { 99 throw new LuceneProviderException("Failed to search query.", e); 100 } 101 } else { 102 for (String field : fields) { 103 QueryParser parser = new QueryParser(field, analyzer); 104 parser.setLowercaseExpandedTerms(useLowercaseExpandedTerms); 105 try { 106 iterate(searcher.search(parser.parse(queryContent))); 107 } catch (IOException e) { 108 throw new LuceneProviderException("Failed to search query.", e); 109 } 110 } 111 } 112 } catch (ParseException e) { 113 throw new LuceneProviderException("Failed to parse query.", e); 114 } finally { IOUtils.closeSilently(queryReader); 116 if (ir != null) { 117 try { 118 ir.close(); 119 } catch (Exception e) { 120 ExceptionUtils.ignoreThrowable(e); 121 } 122 } 123 if (searcher != null) { 124 try { 125 searcher.close(); 126 } catch (Exception e) { 127 ExceptionUtils.ignoreThrowable(e); 128 } 129 } 130 } 131 132 } 133 134 138 void iterate(Hits hits) { 139 for (int i = 0; i < hits.length(); i++) { 140 try { 141 result = hits.doc(i); 142 if (LOG.isLoggable(Level.FINE)) { 143 LOG.fine("Processing search result: " + result); 144 } 145 queryCallback.processRow(this); } catch (IOException e) { 147 throw new LuceneProviderException("Failed to get query result.",e); 148 } 149 } 150 } 151 152 public Object getParameter(final String name) { 153 String res = result.get(name); 154 if (res != null) { 155 return res; 156 } 157 return substitutor.getParameters().getParameter(name); 158 } 159 160 161 public void close() throws IOException { 162 substitutor = null; 163 queryCallback = null; 164 result = null; 165 } 166 } 167 | Popular Tags |