@Nullable private DirectoryReader _getReader () throws IOException { _checkClosing (); try { // Commit the writer changes only if a reader is requested if (m_aWriterChanges.intValue () > 0) { s_aLogger.info ("Lazily committing " + m_aWriterChanges.intValue () + " changes to the Lucene index"); _getWriter ().commit (); m_aWriterChanges.set (0); } // Is a new reader required because the index changed? final DirectoryReader aNewReader = m_aIndexReader != null ? DirectoryReader.openIfChanged (m_aIndexReader) : DirectoryReader.open (m_aDir); if (aNewReader != null) { // Something changed in the index m_aIndexReader = aNewReader; m_aSearcher = null; if (s_aLogger.isDebugEnabled ()) s_aLogger.debug ("Contents of index changed. Creating new index reader"); } return m_aIndexReader; } catch (final IndexNotFoundException ex) { // No such index return null; } }
/** * Returns the last {@link IndexCommit} found in the {@link Directory}, or * {@code null} if there are no commits. */ public static IndexCommit getLastCommit(Directory dir) throws IOException { try { if (DirectoryReader.indexExists(dir)) { List<IndexCommit> commits = DirectoryReader.listCommits(dir); // listCommits guarantees that we get at least one commit back, or // IndexNotFoundException which we handle below return commits.get(commits.size() - 1); } } catch (IndexNotFoundException e) { // ignore the exception and return null } return null; }
public void testNoDir() throws Throwable { File tempDir = createTempDir("doesnotexist"); TestUtil.rm(tempDir); Directory dir = getDirectory(tempDir); try { DirectoryReader.open(dir); fail("did not hit expected exception"); } catch (NoSuchDirectoryException | IndexNotFoundException nsde) { // expected } dir.close(); }
private void openIndexes(boolean overwrite) throws IOException { if (directory == null) { try { if (path == null) directory = new RAMDirectory(); else { //directory = new MMapDirectory(new File(config.getPath())); // as per http://wiki.apache.org/lucene-java/ImproveSearchingSpeed // we use NIOFSDirectory, provided we're not on Windows if (Utils.isWindowsOS()) directory = FSDirectory.open(new File(path)); else directory = NIOFSDirectory.open(new File(path)); } IndexWriterConfig cfg = new IndexWriterConfig(Version.LUCENE_CURRENT, analyzer); cfg.setOpenMode(overwrite ? IndexWriterConfig.OpenMode.CREATE : IndexWriterConfig.OpenMode.APPEND); iwriter = new IndexWriter(directory, cfg); iwriter.commit(); // so that the searcher doesn't fail } catch (IndexNotFoundException e) { if (!overwrite) { // the index was not there, so make a new one directory = null; // ensure we really do try again openIndexes(true); } else throw new DukeException(e); } } }