JochreIndexBuilder implements Runnable, TokenExtractor { public void updateIndex() { long startTime = System.currentTimeMillis(); try { if (searchStatusHolder.getStatus() != SearchStatus.WAITING) { throw new IndexingUnderwayException(); } JochreSearchManager manager = JochreSearchManager.getInstance(configId); searchStatusHolder.setStatus(SearchStatus.PREPARING); Map<String, Analyzer> analyzerPerField = new HashMap<>(); analyzerPerField.put(JochreIndexField.text.name(), new JochreTextLayerAnalyser(this, configId)); analyzerPerField.put(JochreIndexField.author.name(), new JochreKeywordAnalyser(configId)); analyzerPerField.put(JochreIndexField.authorEnglish.name(), new JochreKeywordAnalyser(configId)); analyzerPerField.put(JochreIndexField.publisher.name(), new JochreKeywordAnalyser(configId)); PerFieldAnalyzerWrapper analyzer = new PerFieldAnalyzerWrapper(new JochreMetaDataAnalyser(configId), analyzerPerField); IndexWriterConfig iwc = new IndexWriterConfig(analyzer); iwc.setOpenMode(OpenMode.CREATE_OR_APPEND); try (IndexWriter indexWriter = new IndexWriter(manager.getIndexDir(), iwc)) { File[] subdirs = contentDir.listFiles(new FileFilter() { @Override public boolean accept(File pathname) { return pathname.isDirectory(); } }); if (subdirs.length == 0) throw new IllegalArgumentException("content dir is empty: " + contentDir.getPath()); Arrays.sort(subdirs); searchStatusHolder.setStatus(SearchStatus.BUSY); searchStatusHolder.setTotalCount(subdirs.length); for (File subdir : subdirs) { try { searchStatusHolder.setAction("Indexing " + subdir.getName()); this.processDocument(manager, indexWriter, subdir, forceUpdate); searchStatusHolder.incrementSuccessCount(1); } catch (Exception e) { LOG.error("Failed to index " + subdir.getName(), e); searchStatusHolder.incrementFailureCount(1); } } LOG.info("Commiting index..."); searchStatusHolder.setStatus(SearchStatus.COMMITING); indexWriter.commit(); indexWriter.close(); manager.getManager().maybeRefresh(); } } catch (IOException e) { LOG.error("Failed to update index", e); throw new RuntimeException(e); } catch (RuntimeException e) { LOG.error("Failed to update index", e); throw e; } catch (Exception e) { LOG.error("Failed to update index", e); throw new RuntimeException(e); } finally { searchStatusHolder.setStatus(SearchStatus.WAITING); long endTime = System.currentTimeMillis(); long totalTime = endTime - startTime; LOG.info("Total time (ms): " + totalTime); } } JochreIndexBuilder(String configId, boolean forceUpdate); @Override void run(); void updateIndex(); @Override List<JochreToken> findTokens(String fieldName, Reader input); List<JochreToken> getCurrentStrings(); void setCurrentStrings(List<JochreToken> currentStrings); boolean isForceUpdate(); File getContentDir();  }
@Test public void testUpdateIndex() throws IOException { System.setProperty("config.file", "src/test/resources/test.conf"); ConfigFactory.invalidateCaches(); String configId = "yiddish"; JochreIndexBuilder builder = new JochreIndexBuilder(configId, false); builder.updateIndex(); JochreSearchManager manager = JochreSearchManager.getInstance(configId); IndexSearcher indexSearcher = manager.getManager().acquire(); try { JochreIndexSearcher searcher = new JochreIndexSearcher(indexSearcher, configId); Map<Integer, Document> docs = searcher.findDocuments("MotlPeysiDemKhazns"); assertEquals(1, docs.size()); int docId = docs.keySet().iterator().next(); JochreIndexDocument jochreDoc = new JochreIndexDocument(indexSearcher, docId, configId); assertEquals("MotlPeysiDemKhazns", jochreDoc.getName()); LOG.debug(jochreDoc.toString()); JochreQuery query = new JochreQuery(configId, "זיך"); Pair<TopDocs, Integer> results = searcher.search(query, 0, 100); assertEquals(1, results.getRight().intValue()); for (ScoreDoc scoreDoc : results.getLeft().scoreDocs) { jochreDoc = new JochreIndexDocument(indexSearcher, scoreDoc.doc, configId); assertEquals("MotlPeysiDemKhazns", jochreDoc.getName()); } } finally { manager.getManager().release(indexSearcher); } }