1 41 package com.mvnforum.search.company; 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 57 import com.mvnforum.*; 58 import com.mvnforum.db.CompanyBean; 59 60 public class CompanyIndexer 61 { 62 private static Log log = LogFactory.getLog(CompanyIndexer.class); 63 64 public static final String FIELD_COMPANY_ID = "CompanyID"; 66 public static final String FIELD_COMPANY_NAME = "CompanyName"; 67 public static final String FIELD_COMPANY_ADDRESS = "companyAddress"; 68 public static final String FIELD_CREATION_DATE = "CompanyCreationDate"; 69 public static final String FIELD_MODIFIED_DATE = "CompanyModifiedDate"; 70 71 74 private static Analyzer analyzer; 76 77 private static long lastOptimizeTime = 0; 78 79 static { 80 initializeAnalyzer(); 81 } 82 83 public static void scheduleAddCompanyTask(CompanyBean companyBean) { 84 AddUpdateCompanyIndexTask task = new AddUpdateCompanyIndexTask(companyBean, AddUpdateCompanyIndexTask.OPERATION_ADD); 85 TimerUtil.getInstance().schedule(task, 0); 86 } 87 88 public static void scheduleUpdateCompanyTask(CompanyBean companyBean) { 89 AddUpdateCompanyIndexTask task = new AddUpdateCompanyIndexTask(companyBean, AddUpdateCompanyIndexTask.OPERATION_UPDATE); 90 TimerUtil.getInstance().schedule(task, 0); 91 } 92 93 public static void scheduleDeleteCompanyTask(int objectID) { 94 DeleteCompanyIndexTask task = new DeleteCompanyIndexTask(objectID); 95 TimerUtil.getInstance().schedule(task, 0); 96 } 97 98 public static void scheduleRebuildIndexTask() { 99 RebuildCompanyIndexTask task = new RebuildCompanyIndexTask(); 100 TimerUtil.getInstance().schedule(task, 0); 101 } 102 103 static Analyzer getAnalyzer() { 104 return analyzer; 105 } 106 107 111 private static void initializeAnalyzer() { 112 String analyzerClassName = MVNForumFactoryConfig.getLuceneAnalyzerClassName(); 113 if ( (analyzerClassName == null) || (analyzerClassName.equals("")) ) { 114 analyzer = new StandardAnalyzer(); 117 log.debug("Using StandardAnalyzer for indexing"); 118 } else { 119 try { 121 log.debug("About to load Analyzer [" + analyzerClassName + "] for indexing"); 122 analyzer = (Analyzer) Class.forName(analyzerClassName).newInstance(); 123 } catch (Exception e) { 124 log.warn("Cannot load " + analyzerClassName + ". Loading StandardAnalyzer"); 125 analyzer = new StandardAnalyzer(); 126 } 127 } 128 } 129 130 138 static IndexWriter getIndexWriter(boolean create) throws SearchException { 139 IndexWriter writer = null; 140 141 if (create == false) { 143 try { 144 writer = new IndexWriter(MVNForumConfig.getSearchCompanyIndexDir(), analyzer, false); 145 if (MVNForumConfig.getSearchCompanyIndexType() == MVNForumGlobal.SEARCH_INDEX_TYPE_DISK) { 146 writer.setUseCompoundFile(true); 147 } 148 return writer; 149 } catch (IOException e) { 150 log.warn("Cannot open existed index. New index will be created.", e); 151 } 153 } 154 try { 157 writer = new IndexWriter(MVNForumConfig.getSearchCompanyIndexDir(true), analyzer, true); 159 if (MVNForumConfig.getSearchCompanyIndexType() == MVNForumGlobal.SEARCH_INDEX_TYPE_DISK) { 160 writer.setUseCompoundFile(true); 161 } 162 return writer; 163 } catch (IOException e) { 164 throw new SearchException("Error while creating index writer"); 165 } 166 } 167 168 175 static void doIndexCompany(CompanyBean companyBean, IndexWriter writer) throws SearchException { 176 if (companyBean == null) return; 180 if ( (companyBean.getCompanyID() == 0)|| 181 (companyBean.getCompanyName() == null || companyBean.getCompanyName().equals("")) || 182 (companyBean.getCompanyAddress() == null) || 183 (companyBean.getCompanyCreationDate() == null) || 184 (companyBean.getCompanyModifiedDate() == null)) { 185 return; 186 } 187 188 Document companyDocument = new Document(); 190 companyDocument.add(Field.Keyword(FIELD_COMPANY_ID, Integer.toString(companyBean.getCompanyID()))); 192 companyDocument.add(Field.UnStored(FIELD_COMPANY_NAME, companyBean.getCompanyName())); 194 companyDocument.add(Field.UnStored(FIELD_COMPANY_ADDRESS, companyBean.getCompanyAddress())); 195 companyDocument.add(Field.Keyword(FIELD_CREATION_DATE, DateField.dateToString(companyBean.getCompanyCreationDate()))); companyDocument.add(Field.Keyword(FIELD_MODIFIED_DATE, DateField.dateToString(companyBean.getCompanyModifiedDate()))); try { 200 writer.addDocument(companyDocument); 201 } catch (IOException e) { 202 log.error("CompanyIndexer.doIndexCompany failed", e); 203 throw new SearchException("Error writing new company to index"); 204 } 205 } 206 207 212 static void addCompanyToIndex(CompanyBean companyBean) throws SearchException, IOException { 213 IndexWriter writer = null; 214 try { 215 writer = getIndexWriter(false); 216 if (writer == null) { 217 log.warn("Cannot get the IndexWriter"); 218 return; 219 } 220 doIndexCompany(companyBean, writer); 221 222 long now = System.currentTimeMillis(); 224 long timeFromLastOptimize = now - lastOptimizeTime; 225 if (timeFromLastOptimize > DateUtil.HOUR) { 226 log.debug("writer.optimize() called in addCompanyToIndex"); 227 writer.optimize(); 228 lastOptimizeTime = now; 229 } 230 } catch (SearchException ex) { 231 throw ex; 232 } finally { 233 try { 234 if (writer != null) { 235 writer.close(); 236 } 237 } catch (IOException e) { 238 } 239 } 240 } 241 242 247 static void deleteCompanyFromIndex(int companyID) throws SearchException { 248 IndexReader reader = null; 249 try { 250 reader = IndexReader.open(MVNForumConfig.getSearchCompanyIndexDir()); 251 if (reader == null) { 252 log.warn("Cannot get the IndexReader"); 253 return; 254 } 255 256 Term term = new Term(FIELD_COMPANY_ID, String.valueOf(companyID)); 257 int deletedCount = reader.delete(term); 258 log.debug("deleteCompanyFromIndex: deleted companies = " + deletedCount); 259 } catch (IOException e) { 260 throw new SearchException("Error trying to delete company with companyID = " + companyID); 261 } finally { 262 try { 263 if (reader != null) { 264 reader.close(); 265 } 266 } catch (IOException e) { 267 } 268 } 269 } 270 271 public static IndexSearcher getSearcher() throws IOException { 272 try { 273 IndexSearcher searcher = new IndexSearcher(MVNForumConfig.getSearchCompanyIndexDir()); 274 return searcher; 275 } catch (IOException ex) { 276 log.error("Cannot access the lucene search index for query. Please check if you have configed mvnForumHome properly. You can also go to Admin Zone to rebuild the Lucene index files.", ex); 279 throw new IOException ("Cannot access the lucene search index. Please report this error to web site Administrator (check mvnForumHome or rebuild Lucene index)."); 281 } 282 } 283 284 public static int getNumDocs() { 285 int numDocs = -1; 286 IndexReader reader = null; 287 try { 288 reader = IndexReader.open(MVNForumConfig.getSearchCompanyIndexDir()); 289 if (reader == null) { 290 log.warn("Cannot get the IndexReader"); 291 return -1; 292 } 293 numDocs = reader.numDocs(); 294 } catch ( IOException ioe) { 295 } finally { 297 try { 298 if (reader != null) reader.close(); 299 } catch (IOException e) { 300 log.debug("Error closing Lucene IndexReader", e); 301 } 302 } 303 return numDocs; 304 } 305 306 } 307 | Popular Tags |