1 41 package com.mvnforum.search.post; 42 43 import java.io.IOException ; 44 45 import net.myvietnam.mvncore.exception.SearchException; 46 import net.myvietnam.mvncore.util.DateUtil; 47 import net.myvietnam.mvncore.util.TimerUtil; 48 49 import org.apache.commons.logging.Log; 50 import org.apache.commons.logging.LogFactory; 51 import org.apache.lucene.analysis.Analyzer; 52 import org.apache.lucene.analysis.standard.StandardAnalyzer; 53 import org.apache.lucene.document.*; 54 import org.apache.lucene.index.*; 55 import org.apache.lucene.search.IndexSearcher; 56 import org.apache.lucene.store.Directory; 57 58 import com.mvnforum.*; 59 import com.mvnforum.db.PostBean; 60 import com.mvnforum.search.IntegerFilter; 61 62 public class PostIndexer 63 { 64 private static Log log = LogFactory.getLog(PostIndexer.class); 65 66 public static final String FIELD_POST_ID = "postID"; 68 public static final String FIELD_THREAD_ID = "threadID"; 69 public static final String FIELD_FORUM_ID = "forumID"; 70 public static final String FIELD_MEMBER_ID = "memberID"; 71 public static final String FIELD_POST_TOPIC = "postTopic"; 72 public static final String FIELD_POST_BODY = "postBody"; 73 public static final String FIELD_POST_DATE = "postDate"; 74 75 public static final String FIELD_WITH_ATTACHMENT = "withAttachment"; 76 77 public static final String FIELD_ATTACHMENT_COUNT = "attachmentCount"; 78 79 82 private static Analyzer analyzer; 84 85 private static long lastOptimizeTime = 0; 86 87 static { 88 initializeAnalyzer(); 89 } 90 91 public static void scheduleAddPostTask(PostBean postBean) { 92 AddUpdatePostIndexTask task = new AddUpdatePostIndexTask(postBean, AddUpdatePostIndexTask.OPERATION_ADD); 93 TimerUtil.getInstance().schedule(task, 0); 94 } 95 96 public static void scheduleUpdatePostTask(PostBean postBean) { 97 AddUpdatePostIndexTask task = new AddUpdatePostIndexTask(postBean, AddUpdatePostIndexTask.OPERATION_UPDATE); 98 TimerUtil.getInstance().schedule(task, 0); 99 } 100 101 public static void scheduleDeletePostTask(int objectID, int objectType) { 102 DeletePostIndexTask task = new DeletePostIndexTask(objectID, objectType); 103 TimerUtil.getInstance().schedule(task, 0); 104 } 105 106 public static void scheduleUpdateThreadTask(int threadID) { 107 UpdateThreadTask task = new UpdateThreadTask(threadID); 108 TimerUtil.getInstance().schedule(task, 0); 109 } 110 111 public static void scheduleRebuildIndexTask() { 112 int maxPostID = 0; 113 RebuildPostIndexTask task = new RebuildPostIndexTask(maxPostID); 114 TimerUtil.getInstance().schedule(task, 0); 115 } 116 117 static Analyzer getAnalyzer() { 118 return analyzer; 119 } 120 121 125 private static void initializeAnalyzer() { 126 String analyzerClassName = MVNForumFactoryConfig.getLuceneAnalyzerClassName(); 127 if ( (analyzerClassName == null) || (analyzerClassName.equals("")) ) { 128 analyzer = new StandardAnalyzer(); 131 log.debug("Using StandardAnalyzer for indexing"); 132 } else { 133 try { 135 log.debug("About to load Analyzer [" + analyzerClassName + "] for indexing"); 136 analyzer = (Analyzer) Class.forName(analyzerClassName).newInstance(); 137 } catch (Exception e) { 138 log.warn("Cannot load " + analyzerClassName + ". Loading StandardAnalyzer"); 139 analyzer = new StandardAnalyzer(); 140 } 141 } 142 } 143 144 152 static IndexWriter getIndexWriter(Directory directory, boolean create) throws SearchException { 153 154 IndexWriter writer = null; 155 156 if (create == false) { 158 try { 159 writer = new IndexWriter(directory, analyzer, false); 160 if (MVNForumConfig.getSearchPostIndexType() == MVNForumGlobal.SEARCH_INDEX_TYPE_DISK) { 161 writer.setUseCompoundFile(true); 162 } 163 return writer; 164 } catch (IOException e) { 165 log.warn("Cannot open existed index. New index will be created.", e); 166 } 168 } 169 try { 172 writer = new IndexWriter(directory, analyzer, true); if (MVNForumConfig.getSearchPostIndexType() == MVNForumGlobal.SEARCH_INDEX_TYPE_DISK) { 176 writer.setUseCompoundFile(true); 177 } 178 return writer; 179 } catch (IOException e) { 180 log.error("IOException during get index writer", e); 182 throw new SearchException("Error while creating index writer"); 183 } 184 } 185 186 193 static void doIndexPost(PostBean post, IndexWriter writer) throws SearchException { 194 195 if (post == null) return; 196 if ( (post.getPostTopic() == null || post.getPostTopic().equals("")) || 198 (post.getPostBody() == null || post.getPostBody().equals(""))) { 199 return; 200 } 201 202 Document postDocument = new Document(); 204 postDocument.add(Field.Keyword(FIELD_POST_ID, Integer.toString(post.getPostID()))); 206 postDocument.add(Field.Keyword(FIELD_THREAD_ID, Integer.toString(post.getThreadID()))); 207 postDocument.add(Field.Keyword(FIELD_FORUM_ID, Integer.toString(post.getForumID()))); 208 postDocument.add(Field.Keyword(FIELD_MEMBER_ID, Integer.toString(post.getMemberID()))); 209 postDocument.add(Field.Keyword(FIELD_WITH_ATTACHMENT, new Boolean (post.getPostAttachCount()>0).toString())); postDocument.add(Field.Keyword(FIELD_ATTACHMENT_COUNT, IntegerFilter.intToString(post.getPostAttachCount()))); 211 213 postDocument.add(Field.UnStored(FIELD_POST_TOPIC, post.getPostTopic())); 215 postDocument.add(Field.UnStored(FIELD_POST_BODY, post.getPostBody())); 216 postDocument.add(Field.Keyword(FIELD_POST_DATE, DateField.dateToString(post.getPostCreationDate()))); 218 219 try { 221 writer.addDocument(postDocument); 222 } catch (IOException e) { 223 log.error("PostIndexer.doIndexPost failed", e); 224 throw new SearchException("Error writing new post to index"); 226 } 227 } 228 229 234 static void addPostToIndex(PostBean post) throws SearchException, IOException { 235 236 Directory directory = null; 237 IndexWriter writer = null; 238 try { 239 directory = MVNForumConfig.getSearchPostIndexDir(); 240 writer = getIndexWriter(directory, false); 241 if (writer == null) { 242 log.warn("Cannot get the IndexWriter"); 243 return; 244 } 245 doIndexPost(post, writer); 246 247 long now = System.currentTimeMillis(); 249 long timeFromLastOptimize = now - lastOptimizeTime; 250 if (timeFromLastOptimize > DateUtil.HOUR) { 251 log.debug("writer.optimize() called in addPostToIndex"); 252 writer.optimize(); 253 lastOptimizeTime = now; 254 } 255 } catch (SearchException ex) { 256 throw ex; 257 } finally { 258 if (writer != null) { 259 try { 260 writer.close(); 261 } catch (IOException e) { 262 log.debug("Error closing Lucene IndexWriter", e); 263 } 264 } 265 if (directory != null) { 266 try { 267 directory.close(); 268 } catch (IOException e) { 269 log.debug("Cannot close directory.", e); 270 } 271 } 272 } 273 } 274 275 280 static void deletePostFromIndex(int postID) throws SearchException { 281 282 Directory directory = null; 283 IndexReader reader = null; 284 try { 285 directory = MVNForumConfig.getSearchPostIndexDir(); 286 reader = IndexReader.open(directory); 287 if (reader == null) { 288 log.warn("Cannot get the IndexReader"); 289 return; 290 } 291 292 Term term = new Term(FIELD_POST_ID, String.valueOf(postID)); 293 int deletedCount = reader.delete(term); 294 log.debug("deletePostFromIndex: deleted posts = " + deletedCount); 295 } catch (IOException e) { 296 throw new SearchException("Error trying to delete post with postID = " + postID); 298 } finally { 299 if (reader != null) { 300 try { 301 reader.close(); 302 } catch (IOException e) { 303 log.debug("Error closing Lucene IndexReader", e); 304 } 305 } 306 if (directory != null) { 307 try { 308 directory.close(); 309 } catch (IOException e) { 310 log.debug("Cannot close directory.", e); 311 } 312 } 313 } 314 } 315 316 321 static void deleteThreadFromIndex(int threadID) throws SearchException { 322 323 Directory directory = null; 324 IndexReader reader = null; 325 try { 326 directory = MVNForumConfig.getSearchPostIndexDir(); 327 reader = IndexReader.open(directory); 328 if (reader == null) { 329 log.warn("Cannot get the IndexReader"); 330 return; 331 } 332 333 Term term = new Term(FIELD_THREAD_ID, String.valueOf(threadID)); 334 int deletedCount = reader.delete(term); 335 log.debug("deleteThreadFromIndex: deleted posts = " + deletedCount); 336 } catch (IOException e) { 337 throw new SearchException("Error trying to delete posts in index with threadID = " + threadID); 339 } finally { 340 if (reader != null) { 341 try { 342 reader.close(); 343 } catch (IOException e) { 344 log.debug("Error closing Lucene IndexReader", e); 345 } 346 } 347 if (directory != null) { 348 try { 349 directory.close(); 350 } catch (IOException e) { 351 log.debug("Cannot close directory.", e); 352 } 353 } 354 } 355 } 356 357 362 static void deleteForumFromIndex(int forumID) throws SearchException { 363 364 Directory directory = null; 365 IndexReader reader = null; 366 try { 367 directory = MVNForumConfig.getSearchPostIndexDir(); 368 reader = IndexReader.open(directory); 369 if (reader == null) { 370 log.warn("Cannot get the IndexReader"); 371 return; 372 } 373 374 Term term = new Term(FIELD_FORUM_ID, String.valueOf(forumID)); 375 int deletedCount = reader.delete(term); 376 log.debug("deleteForumFromIndex: deleted posts = " + deletedCount); 377 } catch (IOException e) { 378 throw new SearchException("Error trying to delete posts in index with forumID = " + forumID); 380 } finally { 381 if (reader != null) { 382 try { 383 reader.close(); 384 } catch (IOException e) { 385 log.debug("Error closing Lucene IndexReader", e); 386 } 387 } 388 if (directory != null) { 389 try { 390 directory.close(); 391 } catch (IOException e) { 392 log.debug("Cannot close directory.", e); 393 } 394 } 395 } 396 } 397 398 public static int getNumDocs() { 399 400 int numDocs = -1; 401 Directory directory = null; 402 IndexReader reader = null; 403 try { 404 directory = MVNForumConfig.getSearchPostIndexDir(); 405 reader = IndexReader.open(directory); 406 if (reader == null) { 407 log.warn("Cannot get the IndexReader"); 408 return -1; 409 } 410 numDocs = reader.numDocs(); 411 } catch (IOException ioe) { 412 } finally { 414 if (reader != null) { 415 try { 416 reader.close(); 417 } catch (IOException e) { 418 log.debug("Error closing Lucene IndexReader", e); 419 } 420 } 421 if (directory != null) { 422 try { 423 directory.close(); 424 } catch (IOException e) { 425 log.debug("Cannot close directory.", e); 426 } 427 } 428 } 429 return numDocs; 430 } 431 432 } 433
| Popular Tags
|