Example usage for org.apache.lucene.index DirectoryReader openIfChanged

List of usage examples for org.apache.lucene.index DirectoryReader openIfChanged

Introduction

In this page you can find the example usage for org.apache.lucene.index DirectoryReader openIfChanged.

Prototype

public static DirectoryReader openIfChanged(DirectoryReader oldReader) throws IOException 

Source Link

Document

If the index has changed since the provided reader was opened, open and return a new reader; else, return null.

Usage

From source file:au.org.ala.names.search.ALANameSearcher.java

License:Open Source License

public void reopenReaders() {
    //this should only need to reopen the cbSearcher because the others should NOT be changing
    try {//w w w  .  j  a  va2 s  .  c  om
        DirectoryReader newReader = DirectoryReader.openIfChanged(cbReader);

        //IndexReader tmpReader = cbReader.reopen();
        if (newReader != null) {
            cbReader.close();
            cbReader = newReader;
            //now reinit the searcher
            cbSearcher = new IndexSearcher(cbReader);
        }
    } catch (Exception e) {

    }

}

From source file:cn.hbu.cs.esearch.core.EsearchMultiReader.java

License:Apache License

public EsearchMultiReader<R> reopen() throws IOException {
    long t0 = System.currentTimeMillis();
    DirectoryReader inner = DirectoryReader.openIfChanged(in);
    if (inner == null) {
        t0 = System.currentTimeMillis() - t0;
        if (t0 > 1000) {
            LOGGER.info("reopen returns in " + t0 + "ms without change");
        } else {/*from   w  w w .j  ava 2  s.co m*/
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("reopen returns in " + t0 + "ms without change");
            }
        }
        return this;
    }

    EsearchMultiReader<R> ret = new EsearchMultiReader<R>(inner, _decorator,
            new EsearchSubReaderWrapper<R>(_decorator, _readerMap));
    t0 = System.currentTimeMillis() - t0;
    if (t0 > 1000) {
        LOGGER.info("reopen returns in " + t0 + "ms with change");
    } else {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("reopen returns in " + t0 + "ms with change");
        }
    }
    return ret;
}

From source file:com.bdaum.zoom.lal.internal.LireActivator.java

License:Open Source License

public IndexReader openIfChanged(IndexReader oldReader) throws IOException {
    if (oldReader instanceof DirectoryReader)
        return DirectoryReader.openIfChanged((DirectoryReader) oldReader);
    return null;//www .j  a  v  a  2 s  .  c  o m
}

From source file:com.google.gerrit.lucene.WrappableSearcherManager.java

License:Apache License

@Override
protected IndexSearcher refreshIfNeeded(IndexSearcher referenceToRefresh) throws IOException {
    final IndexReader r = referenceToRefresh.getIndexReader();
    assert r instanceof DirectoryReader : "searcher's IndexReader should be a DirectoryReader, but got " + r;
    final IndexReader newReader = DirectoryReader.openIfChanged((DirectoryReader) r);
    if (newReader == null) {
        return null;
    } else {/*from   ww w .  j a  v a2s . c  om*/
        return getSearcher(searcherFactory, newReader);
    }
}

From source file:com.helger.pd.indexer.lucene.PDLucene.java

License:Apache License

@Nullable
private DirectoryReader _getReader() throws IOException {
    _checkClosing();//w  ww  .j a  v a 2  s.c  o m
    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;
    }
}

From source file:com.liang.minisearch.domain.search.Engine.java

protected DirectoryReader getReader() throws IOException {
    if (this.indexReader == null) {
        this.indexReader = DirectoryReader.open(getDirectory());
    } else {/* ww w . j  a v  a 2 s .  c om*/
        DirectoryReader newReader = DirectoryReader.openIfChanged(this.indexReader);
        if (newReader != null) {
            this.indexReader = newReader;
        }
    }

    return this.indexReader;
}

From source file:com.mathworks.xzheng.admin.SearcherManager.java

License:Apache License

public void maybeReopen() //E
        throws InterruptedException, //E
        IOException { //E

    startReopen();/*from   www.j  a v  a  2  s  .c om*/

    try {
        final IndexSearcher searcher = get();
        try {
            IndexReader newReader = DirectoryReader
                    .openIfChanged((DirectoryReader) currentSearcher.getIndexReader());
            if (newReader != currentSearcher.getIndexReader()) {
                IndexSearcher newSearcher = new IndexSearcher(newReader);
                if (writer == null) {
                    warm(newSearcher);
                }
                swapSearcher(newSearcher);
            }
        } finally {
            release(searcher);
        }
    } finally {
        doneReopen();
    }
}

From source file:com.ostrichemulators.semtool.search.GraphTextSearch.java

License:Open Source License

/**
 * Replaces the current index with the data from the given graph
 *
 * @param graph//from  w  ww.j a v a  2s  .  c o  m
 * @param engine
 * @throws java.io.IOException
 */
public void index(Graph<SEMOSSVertex, SEMOSSEdge> graph, IEngine engine) throws IOException {
    log.trace("asking to update search index");

    if (indexing) {
        log.debug("already indexing");
    }

    log.debug("indexing graph for searchbar");
    indexing = true;
    Date lastIndexed = new Date();
    vertStore.clear();

    // pre-fetch the stuff we know we're going to need
    RetrievingLabelCache rlc = new RetrievingLabelCache(engine);
    Set<IRI> needLabels = new HashSet<>();
    for (SEMOSSEdge e : graph.getEdges()) {
        needLabels.addAll(e.getPropertyKeys());
    }
    for (SEMOSSVertex e : graph.getVertices()) {
        needLabels.addAll(e.getPropertyKeys());
    }
    rlc.putAll(Utility.getInstanceLabels(needLabels, engine));

    // now we can run the indexer
    RepositoryIndexer ri = new RepositoryIndexer(rlc);
    for (SEMOSSEdge e : graph.getEdges()) {
        vertStore.put(e.getIRI(), e);
        ri.handleProperties(e.getIRI(), e.getValues());
    }
    for (SEMOSSVertex v : graph.getVertices()) {
        vertStore.put(v.getIRI(), v);
        ri.handleProperties(v.getIRI(), v.getValues());
    }

    ri.finish();

    try {
        if (null == reader) {
            reader = DirectoryReader.open(ramdir);
        } else {
            DirectoryReader rdr = DirectoryReader.openIfChanged(reader);
            if (null != rdr) {
                reader = rdr;
            }
        }

        searcher = new IndexSearcher(reader);
    } catch (IOException ioe) {
        throw new IOException("cannot read newly-created search index", ioe);
    } finally {
        indexing = false;
        log.debug("done indexing graph: " + Utility.getDuration(lastIndexed, new Date()));
    }
}

From source file:dbn.db.FullTextTrigger.java

/**
 * Commit the index updates/* www.  j  av a2s. c  om*/
 *
 * @throws  SQLException        Unable to commit index updates
 */
private static void commitIndex() throws SQLException {
    indexLock.writeLock().lock();
    try {
        indexWriter.commit();
        DirectoryReader newReader = DirectoryReader.openIfChanged(indexReader);
        if (newReader != null) {
            indexReader.close();
            indexReader = newReader;
            indexSearcher = new IndexSearcher(indexReader);
        }
    } catch (IOException exc) {
        Logger.logErrorMessage("Unable to commit Lucene index updates", exc);
        throw new SQLException("Unable to commit Lucene index updates", exc);
    } finally {
        indexLock.writeLock().unlock();
    }
}

From source file:de.dfki.km.leech.parser.incremental.IncrementalCrawlingHistory.java

License:Open Source License

protected void refreshIndexReaderz() {
    try {//from ww w . ja v  a 2s  .  co m
        DirectoryReader newReader = DirectoryReader.openIfChanged(m_indexReader);

        if (newReader != null) {
            m_indexReader.close();
            m_indexReader = newReader;
            m_indexSearcher = new IndexSearcher(m_indexReader);
        }

    } catch (IOException e) {
        Logger.getLogger(IncrementalCrawlingHistory.class.getName()).log(Level.SEVERE, "Error", e);
    }
}