public void checkIndexOnStartup() { //log.info("Observed event {1} from Thread {0}", Thread.currentThread().getName(), App.INIT_SUCCESS); // See if we need to rebuild the index during startup ... FullTextEntityManager ftEm = Search.getFullTextEntityManager(entityManager); SearchFactory searchFactory = ftEm.getSearchFactory(); ReaderProvider readerProvider = searchFactory.getReaderProvider(); IndexReader reader = readerProvider.openReader(searchFactory.getDirectoryProviders(NodeDocumentVersion.class)[0]); int maxDoc = 0; try { maxDoc = reader.maxDoc(); } finally { readerProvider.closeReader(reader); } if (maxDoc == 0) { log.warn("No objects indexed ... rebuilding Lucene search index from database ..."); long _exit = 0L; long _entr = System.currentTimeMillis(); try { int docs = doRebuildIndex(); _exit = System.currentTimeMillis(); log.info("Took " + (_exit - _entr) + " (ms) to re-build the index containing " + docs + " documents."); } catch (Exception exc) { if (exc instanceof RuntimeException) { throw (RuntimeException) exc; } else { throw new RuntimeException(exc); } } // build the spell checker index off of the HS index. buildSpellCheckerIndex(searchFactory); } }
protected void buildSpellCheckerIndex(SearchFactory searchFactory) { IndexReader reader = null; Directory dir = null; long _entr = System.currentTimeMillis(); File spellCheckIndexDir = new File("lucene_index/spellcheck"); log.info("Building SpellChecker index in {0}", spellCheckIndexDir.getAbsolutePath()); ReaderProvider readerProvider = searchFactory.getReaderProvider(); try { reader = readerProvider.openReader(searchFactory.getDirectoryProviders(NodeDocumentVersion.class)[0]); dir = FSDirectory.open(spellCheckIndexDir); SpellChecker spell = new SpellChecker(dir); spell.clearIndex(); spell.indexDictionary(new LuceneDictionary(reader, NodeDocument.TEXT_FIELD)); spell.close(); dir.close(); dir = null; long _exit = System.currentTimeMillis(); log.info("Took {1} (ms) to build SpellChecker index in {0}", spellCheckIndexDir.getAbsolutePath(), String.valueOf((_exit - _entr))); } catch (Exception exc) { log.error("Failed to build spell checker index!", exc); } finally { if (dir != null) { try { dir.close(); } catch (Exception zzz) { } } if (reader != null) { readerProvider.closeReader(reader); } } }
/** * Get Lucene index reader. */ @SuppressWarnings("rawtypes") private IndexReader getReader(FullTextSession session, Class entity) { SearchFactory searchFactory = session.getSearchFactory(); DirectoryProvider provider = searchFactory.getDirectoryProviders(entity)[0]; ReaderProvider readerProvider = searchFactory.getReaderProvider(); return readerProvider.openReader(provider); }
/** * Shows the main statistics page */ public void list() { IndexReader indexReader = null; ReaderProvider readerProvider = null; try { SearchFactory searchFactory = Search.createFullTextSession( this.sessionFactory.getCurrentSession()).getSearchFactory(); DirectoryProvider<?> directoryProvider = searchFactory .getDirectoryProviders(Post.class)[0]; readerProvider = searchFactory.getReaderProvider(); indexReader = readerProvider.openReader(directoryProvider); String indexDirectory = directoryProvider.getDirectory().toString(); indexDirectory = indexDirectory.substring(indexDirectory .indexOf('@') + 1); boolean indexExists = IndexReader.indexExists(indexDirectory); this.result.include("indexExists", indexExists); if (indexExists) { this.result.include("numberOfDocs", indexReader.numDocs()); this.result.include("indexLocation", indexDirectory); this.result.include("totalMessages", this.forumRepository.getTotalMessages()); this.result.include("isLocked", IndexReader.isLocked(indexDirectory)); this.result.include("lastModified", new Date(IndexReader.lastModified(indexDirectory))); } } catch (IOException e) { throw new ForumException(e); } finally { if (readerProvider != null && indexReader != null) { readerProvider.closeReader(indexReader); } } }
/** * Get Lucent document terms. */ @SuppressWarnings("unchecked") public List<String> getTerms(Class<?> entityType, String nodeUuid) throws CorruptIndexException, IOException { List<String> terms = new ArrayList<String>(); FullTextSession ftSession = null; IndexSearcher searcher = null; ReaderProvider provider = null; Session session = null; IndexReader reader = null; try { session = HibernateUtil.getSessionFactory().openSession(); ftSession = Search.getFullTextSession(session); SearchFactory sFactory = ftSession.getSearchFactory(); provider = sFactory.getReaderProvider(); QueryBuilder builder = sFactory.buildQueryBuilder().forEntity(entityType).get(); Query query = builder.keyword().onField("uuid").matching(nodeUuid).createQuery(); DirectoryProvider<Directory>[] dirProv = sFactory.getDirectoryProviders(NodeDocument.class); reader = provider.openReader(dirProv[0]); searcher = new IndexSearcher(reader); TopDocs topDocs = searcher.search(query, 1); for (ScoreDoc sDoc : topDocs.scoreDocs) { if (!reader.isDeleted(sDoc.doc)) { for (TermEnum te = reader.terms(); te.next(); ) { Term t = te.term(); if ("text".equals(t.field())) { for (TermDocs tds = reader.termDocs(t); tds.next(); ) { if (sDoc.doc == tds.doc()) { terms.add(t.text()); //log.info("Field: {} - {}", t.field(), t.text()); } } } } } } } finally { if (provider != null && reader != null) { provider.closeReader(reader); } if (searcher != null) { searcher.close(); } HibernateUtil.close(ftSession); HibernateUtil.close(session); } return terms; }
private static IndexReader getIndexReader(Class<?> entityClass) { ReaderProvider readerProvider = searchfactory.getReaderProvider(); return readerProvider.openReader(searchfactory.getDirectoryProviders(entityClass)); }