1 25 26 package org.snipsnap.snip; 27 28 import org.apache.lucene.index.IndexReader; 29 import org.apache.lucene.index.IndexWriter; 30 import org.apache.lucene.index.Term; 31 import org.apache.lucene.queryParser.MultiFieldQueryParser; 32 import org.apache.lucene.search.Hits; 33 import org.apache.lucene.search.IndexSearcher; 34 import org.apache.lucene.search.Query; 35 import org.apache.lucene.search.Searcher; 36 import org.apache.lucene.analysis.standard.StandardAnalyzer; 37 import org.radeox.util.logging.Logger; 38 import org.snipsnap.app.Application; 39 import org.snipsnap.container.Components; 40 41 import java.io.File ; 42 import java.io.IOException ; 43 import java.util.HashMap ; 44 import java.util.Iterator ; 45 import java.util.Map ; 46 47 48 54 55 public class SnipIndexer { 56 private static final String [] searchFields = new String []{"content", "title"}; 57 private Map indexMap = new HashMap (); 58 59 private File indexFile() { 60 Application app = Application.get(); 61 String appOid = (String )app.getObject(Application.OID); 62 File indexFile = (File ) indexMap.get(appOid); 63 if (indexFile == null) { 64 indexFile = Application.get().getConfiguration().getIndexPath(); 65 indexMap.put(appOid, indexFile); 66 } 67 return indexFile; 68 } 69 70 public void deleteIndex() throws IOException { 71 File indexFile = indexFile(); 72 if(indexFile.exists() && indexFile.isDirectory()) { 73 File oldIndexFile = new File (indexFile.getParentFile(), indexFile.getName()+".old"); 74 boolean done = indexFile.renameTo(oldIndexFile); 75 if(done) { 76 removeDirectory(oldIndexFile); 77 } else { 78 throw new IOException ("unable to move old index directory to "+oldIndexFile); 79 } 80 } 81 } 82 83 private void removeDirectory(File root) { 84 File [] files = root.listFiles(); 85 for(int f = 0; f < files.length; f++) { 86 if(files[f].isDirectory()) { 87 removeDirectory(files[f]); 88 } 89 boolean done = files[f].delete(); 90 if(!done) { 91 Logger.fatal("Unable to delete old index directory: "+files[f]); 92 } 93 } 94 95 if (!root.delete()) { 96 Logger.fatal("Unable to delete old index directory: " + root); 97 } 98 } 99 100 public void removeIndex(Snip snip) { 101 IndexReader reader = null; 102 try { 103 reader = IndexReader.open(indexFile()); 104 int count = reader.delete(new Term("id", Integer.toHexString(snip.getName().hashCode()))); 105 } catch (IOException e) { 107 Logger.warn("Unable to delete snip " + snip.getName() + " from index", e); 108 } finally { 109 close(reader); 110 } 111 return; 112 } 113 114 public void reIndex(Snip snip) { 115 index(snip, true); 116 return; 117 } 118 119 public void index(Snip snip) { 120 index(snip, false); 121 return; 122 } 123 124 private synchronized void index(Snip snip, boolean exists) { 125 IndexWriter writer = null; 126 127 try { 128 File f; 129 boolean create = true; 130 if ((f = indexFile()).exists() && f.isDirectory()) { 132 create = false; 133 } else { 134 create = true; 135 } 136 137 if (exists && f.exists()) { 138 removeIndex(snip); 139 } 140 141 writer = new IndexWriter(f, new StandardAnalyzer(), create); 142 writer.mergeFactor = 20; 143 writer.addDocument(SnipDocument.Document(snip)); 144 writer.optimize(); 145 } catch (IOException e) { 146 Logger.warn("Unable to index snip", e); 147 } finally { 148 close(writer); 149 } 150 } 151 152 public Hits search(String queryString) { 153 Searcher searcher = null; 154 try { 155 searcher = new IndexSearcher(indexFile().getAbsolutePath()); 156 } catch (IOException e) { 157 Logger.warn("Unable to open index file: " + indexFile(), e); 158 } 159 160 166 Query query = null; 168 try { 169 query = MultiFieldQueryParser.parse(queryString, searchFields, new StandardAnalyzer()); 170 } catch (org.apache.lucene.queryParser.ParseException e1) { 171 close(searcher); 172 Logger.warn("Unable to parse: '" + queryString + "'"); 173 } 174 175 Hits hits = null; 178 try { 179 hits = searcher.search(query); 180 } catch (IOException e) { 181 close(searcher); 182 Logger.warn("SnipIndexer: unable to get hits", e); 183 } 184 return hits; 185 } 186 187 private static void close(IndexWriter writer) { 188 if (null != writer) { 189 try { 190 writer.close(); 191 } catch (Exception e) { 192 } 193 } 194 } 195 196 private static void close(Searcher searcher) { 197 if (null != searcher) { 198 try { 199 searcher.close(); 200 } catch (Exception e) { 201 } 202 } 203 } 204 205 private static void close(IndexReader reader) { 206 if (null != reader) { 207 try { 208 reader.close(); 209 } catch (Exception e) { 210 } 211 } 212 } 213 214 } 215 216 217 | Popular Tags |