Java Code Examples for org.hibernate.search.SearchFactory#getReaderProvider()
The following examples show how to use
org.hibernate.search.SearchFactory#getReaderProvider() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexHelper.java From document-management-system with GNU General Public License v2.0 | 5 votes |
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); } }
Example 2
Source File: IndexHelper.java From document-management-system with GNU General Public License v2.0 | 5 votes |
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); } } }
Example 3
Source File: SearchDAO.java From document-management-system with GNU General Public License v2.0 | 5 votes |
/** * 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); }
Example 4
Source File: SearchDAO.java From document-management-system with GNU General Public License v2.0 | 4 votes |
/** * 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; }