Skip to content

Commit

Permalink
NIFI-13214 Replaced deprecated Lucene IndexReader methods with Stored…
Browse files Browse the repository at this point in the history
…Fields

This closes apache#8815

Signed-off-by: David Handermann <exceptionfactory@apache.org>
  • Loading branch information
dan-s1 authored and exceptionfactory committed May 12, 2024
1 parent 6fc374b commit c66a3cf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.StoredFields;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
Expand Down Expand Up @@ -333,8 +334,9 @@ long getMaxEventId(final String partitionName) {

try {
final IndexReader reader = searcher.getIndexSearcher().getIndexReader();
final StoredFields storedFields = reader.storedFields();
final int maxDocId = reader.maxDoc() - 1;
final Document document = reader.document(maxDocId);
final Document document = storedFields.document(maxDocId);
final long eventId = document.getField(SearchableFields.Identifier.getSearchableFieldName()).numericValue().longValue();
logger.info("Determined that Max Event ID indexed for Partition {} is approximately {} based on index {}", partitionName, eventId, directory);
return eventId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
package org.apache.nifi.provenance.index.lucene;

import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.StoredFields;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Sort;
import org.apache.lucene.search.SortField;
Expand Down Expand Up @@ -125,7 +126,9 @@ public void run() {
// Query lucene
final IndexReader indexReader = searcher.getIndexSearcher().getIndexReader();
final TopDocs topDocs;
final StoredFields storedFields;
try {
storedFields = indexReader.storedFields();

// Sort based on document id, descending. This gives us most recent events first.
final Sort sort = new Sort(new SortField(null, SortField.Type.DOC, true));
Expand All @@ -152,7 +155,7 @@ public void run() {
return;
}

final Tuple<List<ProvenanceEventRecord>, Long> eventsAndTotalHits = readDocuments(topDocs, indexReader);
final Tuple<List<ProvenanceEventRecord>, Long> eventsAndTotalHits = readDocuments(topDocs, storedFields);

if (eventsAndTotalHits == null) {
queryResult.update(Collections.emptyList(), 0L);
Expand All @@ -174,7 +177,7 @@ public void run() {
}
}

private Tuple<List<ProvenanceEventRecord>, Long> readDocuments(final TopDocs topDocs, final IndexReader indexReader) {
private Tuple<List<ProvenanceEventRecord>, Long> readDocuments(final TopDocs topDocs, final StoredFields storedFields) {
// If no topDocs is supplied, just provide a Tuple that has no records and a hit count of 0.
if (topDocs == null || topDocs.totalHits.value == 0) {
return new Tuple<>(Collections.<ProvenanceEventRecord> emptyList(), 0L);
Expand All @@ -185,7 +188,7 @@ private Tuple<List<ProvenanceEventRecord>, Long> readDocuments(final TopDocs top
.mapToInt(scoreDoc -> scoreDoc.doc)
.mapToObj(docId -> {
try {
return indexReader.document(docId, LUCENE_FIELDS_TO_LOAD);
return storedFields.document(docId, LUCENE_FIELDS_TO_LOAD);
} catch (final Exception e) {
throw new SearchFailedException("Failed to read Provenance Events from Event File", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexableField;
import org.apache.lucene.index.StoredFields;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.nifi.provenance.ProvenanceEventRecord;
Expand All @@ -58,10 +59,11 @@ public Set<ProvenanceEventRecord> read(final TopDocs topDocs, final EventAuthori
final ScoreDoc[] scoreDocs = topDocs.scoreDocs;
final int numDocs = Math.min(scoreDocs.length, maxResults);
final List<Document> docs = new ArrayList<>(numDocs);
final StoredFields storedFields = indexReader.storedFields();

for (int i = numDocs - 1; i >= 0; i--) {
final int docId = scoreDocs[i].doc;
final Document d = indexReader.document(docId);
final Document d = storedFields.document(docId);
docs.add(d);
}

Expand Down

0 comments on commit c66a3cf

Please sign in to comment.