1 package prefuse.data.search; 2 3 import java.io.IOException ; 4 import java.util.HashMap ; 5 6 import org.apache.lucene.analysis.Analyzer; 7 import org.apache.lucene.analysis.standard.StandardAnalyzer; 8 import org.apache.lucene.document.Document; 9 import org.apache.lucene.index.IndexReader; 10 import org.apache.lucene.index.IndexWriter; 11 import org.apache.lucene.queryParser.MultiFieldQueryParser; 12 import org.apache.lucene.queryParser.ParseException; 13 import org.apache.lucene.queryParser.QueryParser; 14 import org.apache.lucene.search.Hits; 15 import org.apache.lucene.search.IndexSearcher; 16 import org.apache.lucene.search.Query; 17 import org.apache.lucene.search.Searcher; 18 import org.apache.lucene.store.Directory; 19 import org.apache.lucene.store.RAMDirectory; 20 21 31 public class LuceneSearcher { 32 33 34 public static final String FIELD = "prefuse-text"; 35 36 public static final String ID = "prefuse-id"; 37 38 private Directory directory; 39 private Analyzer analyzer; 40 private String [] fields; 41 42 private Searcher searcher; 43 private IndexReader reader; 44 private IndexWriter writer; 45 private boolean m_readMode = true; 46 private boolean m_readOnly = false; 47 48 private HashMap m_hitCountCache; 49 50 53 public LuceneSearcher() { 54 this(new RAMDirectory(), FIELD, false); 55 } 56 57 61 public LuceneSearcher(Directory dir) { 62 this(dir, FIELD, false); 63 } 64 65 72 public LuceneSearcher(Directory dir, String field, boolean readOnly) { 73 this(dir, new String []{field}, readOnly); 74 } 75 76 83 public LuceneSearcher(Directory dir, String [] fields, boolean readOnly) { 84 m_hitCountCache = new HashMap (); 85 directory = dir; 86 analyzer = new StandardAnalyzer(); 87 this.fields = (String [])fields.clone(); 88 try { 89 writer = new IndexWriter(directory, analyzer, !readOnly); 90 writer.close(); 91 writer = null; 92 } catch (IOException e1) { 93 e1.printStackTrace(); 94 } 95 m_readOnly = readOnly; 96 if ( !readOnly ) { 97 setReadMode(false); 98 } else { 99 m_readMode = false; 100 setReadMode(true); 101 } 102 } 103 104 106 113 public boolean setReadMode(boolean mode) { 114 if ( m_readOnly && mode == false ) return false; 116 if ( m_readMode == mode ) return true; 118 if ( !mode ) { 120 try { 122 if ( searcher != null ) searcher.close(); 123 if ( reader != null ) reader.close(); 124 } catch ( Exception e ) { 125 e.printStackTrace(); 126 return false; 127 } 128 try { 130 writer = new IndexWriter(directory, analyzer, false); 131 } catch (IOException e1) { 132 e1.printStackTrace(); 133 return false; 134 } 135 } else { 136 try { 138 if ( writer != null ) { 139 writer.optimize(); 140 writer.close(); 141 } 142 } catch (IOException e1) { 143 e1.printStackTrace(); 144 return false; 145 } 146 try { 148 reader = IndexReader.open(directory); 149 searcher = new IndexSearcher(reader); 150 } catch ( Exception e ) { 151 e.printStackTrace(); 152 return false; 153 } 154 } 155 m_readMode = mode; 156 return true; 157 } 158 159 168 public Hits search(String query) throws ParseException, IOException { 169 if ( m_readMode ) { 170 Query q; 171 if ( fields.length == 1 ) { 172 q = QueryParser.parse(query, fields[0], analyzer); 173 } else { 174 q = MultiFieldQueryParser.parse(query, fields, analyzer); 175 } 176 return searcher.search(q); 177 } else { 178 throw new IllegalStateException ( 179 "Searches can only be performed when " + 180 "the LuceneSearcher is in read mode"); 181 } 182 } 183 184 194 public int numHits(String query) throws ParseException, IOException { 195 Integer count; 196 if ( (count=(Integer )m_hitCountCache.get(query)) == null ) { 197 Hits hits = search(query); 198 count = new Integer (hits.length()); 199 m_hitCountCache.put(query, count); 200 } 201 return count.intValue(); 202 } 203 204 209 public void addDocument(Document d) { 210 if ( !m_readMode ) { 211 try { 212 writer.addDocument(d); 213 m_hitCountCache.clear(); 214 } catch (IOException e) { 215 e.printStackTrace(); 216 } 217 } else { 218 throw new IllegalStateException ( 219 "Documents can not be added to the index unless" + 220 "the LuceneSearcher is not in read mode"); 221 } 222 } 223 224 229 public Analyzer getAnalyzer() { 230 return analyzer; 231 } 232 233 238 public void setAnalyzer(Analyzer analyzer) { 239 this.analyzer = analyzer; 240 } 241 242 248 public String [] getFields() { 249 return (String [])fields.clone(); 250 } 251 252 258 public void setFields(String [] fields) { 259 this.fields = (String [])fields.clone(); 260 } 261 262 267 public IndexReader getIndexReader() { 268 return reader; 269 } 270 271 276 public Searcher getIndexSearcher() { 277 return searcher; 278 } 279 280 284 public boolean isReadOnly() { 285 return m_readOnly; 286 } 287 288 } | Popular Tags |