public static long getModifiedTimeIndex(Class<?> entityClass) { try { DirectoryProvider[] directoryProviders = getSearchFactory() .getDirectoryProviders(entityClass); if (directoryProviders == null || directoryProviders.length < 1) return 0; Directory dir = directoryProviders[0].getDirectory(); if (dir == null) return 0; return IndexReader.lastModified(dir); } catch (Exception ex) { org.webdsl.logging.Logger.error( "Could not retrieve modified timestamp of search index.", ex); return 0; } }
/** * 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); }
public IndexReader getReader( ) { SearchFactory searchFactory = getFullTextSession( ).getSearchFactory( ); DirectoryProvider<?>[] providers = searchFactory .getDirectoryProviders( entityClass ); return searchFactory.getReaderProvider( ).openReader( providers ); }
/** * 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; }