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

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

Introduction

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

Prototype

public static DirectoryReader open(final IndexWriter writer, boolean applyAllDeletes, boolean writeAllDeletes)
        throws IOException 

Source Link

Document

Expert: open a near real time IndexReader from the org.apache.lucene.index.IndexWriter , controlling whether past deletions should be applied.

Usage

From source file:com.rapidminer.search.GlobalSearchIndexer.java

License:Open Source License

/**
 * Creates an instance of {@link IndexReader}.
 *
 * @return the reader, never {@code null}
 * @throws IOException//  ww  w.  j  a  v  a  2  s.  com
 *       if something goes wrong
 */
protected IndexReader createIndexReader() throws IOException {
    return DirectoryReader.open(indexWriter, true, false);
}

From source file:com.vmware.xenon.services.common.LuceneBlobIndexService.java

License:Open Source License

private IndexSearcher updateSearcher(IndexWriter w) throws IOException {
    long now = Utils.getNowMicrosUtc();

    // we do not synchronize the searcher update since this service uses a single thread
    // to schedule all queries, updates and maintenance. If this changes, the code below
    // must become synchronized similar to LuceneDocumentIndexService.updateSearcher

    IndexSearcher s = this.searcher;
    if (s != null && this.searcherUpdateTimeMicros > this.indexUpdateTimeMicros) {
        return s;
    }/*  ww  w.j a v  a  2  s.  c  o  m*/

    s = new IndexSearcher(DirectoryReader.open(w, true, true));

    if (this.searcherUpdateTimeMicros < now) {
        closeSearcherSafe();
        this.searcher = s;
        this.searcherUpdateTimeMicros = now;
    }
    return this.searcher;
}

From source file:com.vmware.xenon.services.common.LuceneDocumentIndexBackupService.java

License:Open Source License

private void performTimeSnapshotRecovery(Long timeSnapshotBoundaryMicros, IndexWriter newWriter)
        throws IOException {

    // For documents with metadata indexing enabled, the version which was current at
    // the restore time may have subsequently been marked as not current. Update the
    // current field for any such documents.

    IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(newWriter, true, true));

    Query updateTimeQuery = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS,
            timeSnapshotBoundaryMicros + 1, Long.MAX_VALUE);

    Sort selfLinkSort = new Sort(new SortField(
            LuceneIndexDocumentHelper.createSortFieldPropertyName(ServiceDocument.FIELD_NAME_SELF_LINK),
            SortField.Type.STRING));

    final int pageSize = 10000;

    Set<String> prevPageLinks = new HashSet<>();
    ScoreDoc after = null;//from   w w w .j  ava 2  s  .  c o  m
    while (true) {
        TopDocs results = searcher.searchAfter(after, updateTimeQuery, pageSize, selfLinkSort, false, false);
        if (results == null || results.scoreDocs == null || results.scoreDocs.length == 0) {
            break;
        }

        Set<String> pageLinks = new HashSet<>();
        DocumentStoredFieldVisitor visitor = new DocumentStoredFieldVisitor();
        for (ScoreDoc sd : results.scoreDocs) {
            visitor.reset(ServiceDocument.FIELD_NAME_SELF_LINK);
            searcher.doc(sd.doc, visitor);
            if (prevPageLinks.contains(visitor.documentSelfLink)) {
                pageLinks.add(visitor.documentSelfLink);
                continue;
            }

            if (!pageLinks.add(visitor.documentSelfLink)) {
                continue;
            }

            updateCurrentAttributeForSelfLink(searcher, timeSnapshotBoundaryMicros, visitor.documentSelfLink,
                    newWriter);
        }

        if (results.scoreDocs.length < pageSize) {
            break;
        }

        after = results.scoreDocs[results.scoreDocs.length - 1];
        prevPageLinks = pageLinks;
    }

    // Now that metadata indexing attributes have been updated appropriately, delete any
    // documents which were created after the restore point.
    Query luceneQuery = LongPoint.newRangeQuery(ServiceDocument.FIELD_NAME_UPDATE_TIME_MICROS,
            timeSnapshotBoundaryMicros + 1, Long.MAX_VALUE);
    newWriter.deleteDocuments(luceneQuery);
}

From source file:com.vmware.xenon.services.common.LuceneDocumentIndexService.java

License:Open Source License

/**
 * Issues a query to verify index is healthy
 *//*from ww  w .  ja  v a2s.co m*/
private void doSelfValidationQuery() throws Throwable {
    TermQuery tq = new TermQuery(new Term(ServiceDocument.FIELD_NAME_SELF_LINK, getSelfLink()));
    ServiceDocumentQueryResult rsp = new ServiceDocumentQueryResult();

    Operation op = Operation.createGet(getUri());
    EnumSet<QueryOption> options = EnumSet.of(QueryOption.INCLUDE_ALL_VERSIONS);
    IndexSearcher s = new IndexSearcher(DirectoryReader.open(this.writer, true, true));
    queryIndex(op, options, s, tq, null, Integer.MAX_VALUE, 0, null, rsp, null);
}

From source file:com.vmware.xenon.services.common.LuceneDocumentIndexService.java

License:Open Source License

private IndexSearcher createPaginatedQuerySearcher(long expirationMicros, IndexWriter w) throws IOException {
    if (w == null) {
        throw new IllegalStateException("Writer not available");
    }//from w w w. j ava 2  s .c  o  m
    IndexSearcher s = new IndexSearcher(DirectoryReader.open(w, true, true));
    synchronized (this.searchSync) {
        List<IndexSearcher> searchers = this.searchersForPaginatedQueries.get(expirationMicros);
        if (searchers == null) {
            searchers = new ArrayList<>();
        }
        searchers.add(s);
        this.searchersForPaginatedQueries.put(expirationMicros, searchers);
    }
    return s;
}

From source file:com.vmware.xenon.services.common.LuceneDocumentIndexService.java

License:Open Source License

/**
 * Returns an updated {@link IndexSearcher} to query {@code selfLink}.
 *
 * If the index has been updated since the last {@link IndexSearcher} was created, those
 * changes will not be reflected by that {@link IndexSearcher}. However, for performance
 * reasons, we do not want to create a new one for every query either.
 *
 * We create one in one of following conditions:
 *
 *   1) No searcher for this index exists.
 *   2) The query is across many links or multiple version, not a specific one,
 *      and the index was changed./*www .  j  av a 2s .co m*/
 *   3) The query is for a specific self link AND the self link has seen an update
 *      after the searcher was last updated.
 *
 * @param selfLink
 * @param resultLimit
 * @param w
 * @return an {@link IndexSearcher} that is fresh enough to execute the specified query
 * @throws IOException
 */
private IndexSearcher updateSearcher(String selfLink, int resultLimit, IndexWriter w) throws IOException {
    IndexSearcher s;
    boolean needNewSearcher = false;
    long now = Utils.getNowMicrosUtc();

    synchronized (this.searchSync) {
        s = this.searcher;
        if (s == null) {
            needNewSearcher = true;
        } else if (selfLink != null && resultLimit == 1) {
            Long latestUpdate = this.linkAccessTimes.get(selfLink);
            if (latestUpdate != null && latestUpdate.compareTo(this.searcherUpdateTimeMicros) >= 0) {
                needNewSearcher = true;
            }
        } else if (this.searcherUpdateTimeMicros < this.indexUpdateTimeMicros) {
            needNewSearcher = true;
        }

        if (!needNewSearcher) {
            return s;
        }
    }

    // Create a new searcher outside the lock. Another thread might race us and
    // also create a searcher, but that is OK: the most recent one will be used.
    s = new IndexSearcher(DirectoryReader.open(w, true, true));

    if (hasOption(ServiceOption.INSTRUMENTATION)) {
        ServiceStat st = getStat(STAT_NAME_SEARCHER_UPDATE_COUNT);
        adjustStat(st, 1);
    }

    synchronized (this.searchSync) {
        this.searchersPendingClose.add(s);
        if (this.searcherUpdateTimeMicros < now) {
            this.searcher = s;
            this.searcherUpdateTimeMicros = now;
        }
        return s;
    }
}

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

License:Open Source License

/**
 * Creates all writer, reader, and searcher objects if necessary
 * /*from   w ww.  jav  a  2 s.  com*/
 * @throws CorruptIndexException
 * @throws LockObtainFailedException
 * @throws IOException
 */
public void openLuceneStuff() throws CorruptIndexException, LockObtainFailedException, IOException {
    if (m_indexWriter == null) {
        IndexWriterConfig config = new IndexWriterConfig(new KeywordAnalyzer());
        config.setOpenMode(OpenMode.CREATE_OR_APPEND);

        m_indexWriter = new IndexWriter(new SimpleFSDirectory(Paths.get(m_strHistoryPath)), config);
    }

    if (m_indexReader == null)
        m_indexReader = DirectoryReader.open(m_indexWriter, true, true);

    if (m_indexSearcher == null)
        m_indexSearcher = new IndexSearcher(m_indexReader);
}