1 16 package org.javabb.lucene.index; 17 18 19 import java.io.IOException ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.commons.lang.builder.ToStringBuilder; 24 import org.apache.commons.lang.builder.ToStringStyle; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.lucene.analysis.Analyzer; 28 import org.apache.lucene.document.Document; 29 import org.apache.lucene.document.Field; 30 import org.apache.lucene.index.IndexReader; 31 import org.apache.lucene.index.IndexWriter; 32 import org.apache.lucene.index.Term; 33 import org.apache.lucene.store.Directory; 34 import org.apache.lucene.store.FSDirectory; 35 36 import org.javabb.infra.Monitor; 37 import org.javabb.infra.Utils; 38 import org.javabb.vo.Post; 39 40 import org.springframework.core.io.Resource; 41 42 43 50 public class Indexer { 51 52 private static final Log logger = LogFactory.getLog(Indexer.class); 53 54 private Directory path; 55 private Analyzer analyzer; 56 57 private boolean optimize; 58 private boolean createNew; 59 private boolean initialized = false; 60 61 private static Object monitor = Monitor.MONITOR; 62 63 70 public Indexer ( Analyzer analyzer, Directory path, boolean opt, 71 boolean createNew ) throws IOException { 72 73 this.analyzer = analyzer; 74 this.path = path; 75 this.optimize = opt; 76 this.createNew = createNew; 77 78 initialize(); 79 } 80 81 88 public Indexer ( Analyzer analyzer, Resource path, boolean opt, 89 boolean createNew ) throws IOException { 90 91 this(analyzer, FSDirectory.getDirectory(path.getFile(), false), opt, 92 createNew); 93 94 } 95 96 99 public Analyzer getAnalyzer() { 100 101 return analyzer; 102 103 } 104 105 108 public Directory getPath() { 109 110 return path; 111 112 } 113 114 117 public boolean isOptimize() { 118 119 return optimize; 120 121 } 122 123 126 public void index( Post post ) { 127 128 Document document = postToDocument(post); 129 130 indexDocument(document); 131 132 } 133 134 private Document postToDocument(Post post) { 135 136 Document document = new Document(); 137 document.add(Field.Keyword("postId", String.valueOf(post.getId()))); 138 document.add(Field.Keyword("date", post.getPostDate())); 139 document.add(Field.UnStored("subject", Utils.avoidNull(post.getSubject()))); 141 document.add(Field.UnStored("text", Utils.avoidNull(post.getPostBody()))); 142 String titleTopic = post.getTopic().getTitleTopic(); 146 document.add(Field.UnStored("title", Utils.avoidNull(titleTopic))); 147 148 return document; 149 150 } 151 152 public void createIndex(List posts) { 153 154 IndexWriter writer = null; 155 156 synchronized (monitor) { 157 158 try { 159 160 writer = new IndexWriter(path, analyzer, true); 161 162 for (Iterator iter = posts.iterator(); iter.hasNext();) { 163 164 Post post = (Post) iter.next(); 165 Document doc = postToDocument(post); 166 writer.addDocument(doc); 167 168 } 169 170 writer.optimize(); 171 172 } catch (IOException e) { 173 174 if (logger.isDebugEnabled()) { 175 176 logger.debug("A error occur when creating index", e); 177 178 } 179 180 throw new LuceneIndexerException(e); 181 182 } finally { 183 184 if(writer != null) { 185 186 try { 187 188 writer.close(); 189 190 } catch (IOException e) { 191 } 193 194 } 195 196 } 197 198 } 199 200 } 201 202 205 public void update( Post post ) { 206 207 this.delete(post); 208 this.index(post); 209 210 } 211 212 215 public void delete( Post post ) { 216 217 Term term = new Term("postId", String.valueOf(post.getId())); 218 219 deleteUsingTerm(term); 220 221 } 222 223 226 public void delete( Long postId ) { 227 228 Term term = new Term("postId", String.valueOf(postId)); 229 230 deleteUsingTerm(term); 231 232 } 233 234 private IndexWriter makeWriter() throws IOException { 235 236 IndexWriter writer; 237 if (IndexReader.indexExists(path)) { 238 239 writer = new IndexWriter(path, analyzer, false); 240 241 } else { 242 243 writer = new IndexWriter(path, analyzer, true); 244 245 } 246 247 return writer; 248 249 } 250 251 private void indexDocument( Document doc ) { 252 253 try { 254 255 synchronized (monitor) { 256 257 IndexWriter writer = makeWriter(); 258 writer.addDocument(doc); 259 260 if (optimize) { 261 262 writer.optimize(); 263 264 } 265 266 writer.close(); 267 268 } 269 270 } catch (Exception e) { 271 272 if (logger.isDebugEnabled()) { 273 274 logger.debug("A error occur when indexing document", e); 275 276 } 277 278 throw new LuceneIndexerException(e); 279 280 } 281 282 } 283 284 287 private void deleteUsingTerm( Term term ) { 288 289 try { 290 291 synchronized (monitor) { 292 293 IndexReader reader = IndexReader.open(this.path); 294 reader.delete(term); 295 reader.close(); 296 297 } 298 299 } catch (IOException ioex) { 300 301 if (logger.isDebugEnabled()) { 302 303 logger.debug("A error occur when deleting document", ioex); 304 305 } 306 307 throw new LuceneIndexerException(ioex); 308 309 } 310 } 311 312 private void initialize() throws IOException { 313 314 if (!initialized && createNew) { 315 316 logger.info("Initializing lucene index at " + path); 317 318 IndexWriter writer = new IndexWriter(path, analyzer, createNew); 319 writer.addDocument(new Document()); 320 writer.close(); 321 322 } 323 324 logger.info("Lucene indexer was successful initialized."); 325 326 initialized = true; 327 } 328 329 332 public String toString() { 333 334 ToStringStyle style = ToStringStyle.MULTI_LINE_STYLE; 335 return ToStringBuilder.reflectionToString(this, style); 336 } 337 } 338 | Popular Tags |