private synchronized Map<String, ItemIndex<? extends FreetextResult>> getIndexerMap() { Map<String, ItemIndex<? extends FreetextResult>> indexerMap = indexTracker.getBeanMap(); if( !indexesHaveBeenInited ) { // This simply exists in order to reset the indices for the first // time. IndexBuilder resetBuilder = new IndexBuilder() { @Override public long buildIndex(NRTManager nrtManager) throws Exception { return nrtManager.getCurrentSearchingGen(true); } }; for( ItemIndex<? extends FreetextResult> itemIndex : indexerMap.values() ) { itemIndex.modifyIndex(resetBuilder); } indexesHaveBeenInited = true; } return indexerMap; }
@Override public long addDocuments(Collection<IndexedItem> documents, NRTManager nrtManager) { long generation = -1; for( IndexedItem item : documents ) { if( item.isAdd() ) { List<Document> docs = getDocuments(item); if( docs != null ) { for( Document doc : docs ) { try { long g = nrtManager.addDocument(doc); if( item.isNewSearcherRequired() ) { generation = g; } } catch( IOException e ) { throw new RuntimeException(e); } } } } } return generation; }
protected long removeDocuments(Collection<IndexedItem> documents, NRTManager nrtManager) { long generation = -1; for( IndexedItem item : documents ) { ItemIdKey itemId = item.getItemIdKey(); String unique = ItemId.fromKey(itemId).toString(); try { BooleanQuery delQuery = new BooleanQuery(); delQuery.add(new TermQuery(new Term(FreeTextQuery.FIELD_ID, Long.toString(itemId.getKey()))), Occur.MUST); delQuery.add( new TermQuery(new Term(FreeTextQuery.FIELD_INSTITUTION, Long.toString(item.getInstitution() .getUniqueId()))), Occur.MUST); long g = nrtManager.deleteDocuments(delQuery); if( item.isNewSearcherRequired() ) { generation = g; } } catch( IOException e ) { LOGGER.error("An error occurred while attempting to delete item " + unique, e); //$NON-NLS-1$ throw new RuntimeException(e); } } return generation; }
public long addDocuments(Collection<IndexedItem> documents, NRTManager nrtManager) { long generation = -1; for( IndexedItem item : documents ) { if( item.isAdd() ) { synchronized( item ) { Document doc = item.getItemdoc(); if( doc.getFields().isEmpty() ) { LOGGER.error("Trying to add an empty document for item:" + item.getId()); continue; } try { long g = nrtManager.addDocument(doc); if( item.isNewSearcherRequired() ) { generation = g; } } catch( IOException e ) { throw new RuntimeException(e); } } } } return generation; }
public void indexBatch(final Collection<IndexedItem> batch) { modifyIndex(new IndexBuilder() { @Override public long buildIndex(NRTManager nrtManager) throws Exception { long generation = -1; generation = Math.max(generation, removeDocuments(batch, nrtManager)); generation = Math.max(generation, addDocuments(batch, nrtManager)); return generation; } }); }
public void deleteForInstitution(final long id) { modifyIndex(new IndexBuilder() { @Override public long buildIndex(NRTManager nrtManager) throws Exception { nrtManager.deleteDocuments(new Term(FreeTextQuery.FIELD_INSTITUTION, Long.toString(id))); return -1; } }); }
public LuceneIndexer(String indexName) throws IOException { this.indexName = indexName; luceneWriterService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors(), new NamedThreadFactory(threadGroup, indexName + " Lucene writer")); luceneWriterFutureCheckerService = Executors.newFixedThreadPool(1, new NamedThreadFactory(threadGroup, indexName + " Lucene future checker")); setupRoot(); File indexDirectoryFile = new File(root.getPath() + "/" + indexName); System.out.println("Index: " + indexDirectoryFile); Directory indexDirectory = initDirectory(indexDirectoryFile); indexDirectory.clearLock("write.lock"); IndexWriterConfig config = new IndexWriterConfig(luceneVersion, new StandardAnalyzer(luceneVersion)); MergePolicy mergePolicy = new LogByteSizeMergePolicy(); config.setMergePolicy(mergePolicy); config.setSimilarity(new ShortTextSimilarity()); IndexWriter indexWriter = new IndexWriter(indexDirectory, config); trackingIndexWriter = new NRTManager.TrackingIndexWriter(indexWriter); boolean applyAllDeletes = false; searcherManager = new NRTManager(trackingIndexWriter, null, applyAllDeletes); // Refreshes searcher every 5 seconds when nobody is waiting, and up to 100 msec delay // when somebody is waiting: reopenThread = new NRTManagerReopenThread(searcherManager, 5.0, 0.1); this.startThread(); }
@Test public void testNrtManager() throws IOException { TrackingIndexWriter trackingIndexWriter = new TrackingIndexWriter(indexWriter); NRTManager nrtManager = new NRTManager(trackingIndexWriter, new SearcherFactory()); IndexSearcher indexSearcher = null; try { indexSearcher = nrtManager.acquire(); } finally { if (indexSearcher == null) { nrtManager.release(indexSearcher); } } }
@PostConstruct public void afterPropertiesSet() throws IOException { if( !indexPath.exists() ) { if( !indexPath.mkdirs() ) { throw new Error("Error creating index:" + indexPath); //$NON-NLS-1$ } } directory = FSDirectory.open(indexPath); if( IndexWriter.isLocked(directory) ) { LOGGER.info("Unlocking index:" + indexPath); //$NON-NLS-1$ IndexWriter.unlock(directory); } LOGGER.info("Opening writer for index:" + indexPath); //$NON-NLS-1$ indexWriter = new IndexWriter(directory, new IndexWriterConfig(LuceneConstants.LATEST_VERSION, getAnalyser())); nrtManager = new NRTManager(indexWriter, null); // Possibly reopen a searcher every 5 seconds if necessary in the // background nrtReopenThread = new NRTManagerReopenThread(nrtManager, 5.0, 0.1); nrtReopenThread.setName("NRT Reopen Thread: " + getClass()); nrtReopenThread.setPriority(Math.min(Thread.currentThread().getPriority() + 2, Thread.MAX_PRIORITY)); nrtReopenThread.setDaemon(true); nrtReopenThread.start(); // Commit any changes to disk every 5 minutes commiterThread = new Timer(true); commiterThread.schedule(new TimerTask() { @Override public void run() { try { indexWriter.commit(); } catch( IOException ex ) { LOGGER.error("Error attempting to commit index writer", ex); } } }, 5 * 60 * 1000, 5 * 60 * 1000); }
/** * @return The index generation to wait for, or -1 if you don't care. */ long buildIndex(NRTManager nrtManager) throws Exception;