Skip to content

Commit

Permalink
BasicUsageExample: ArtifactInfo may be null
Browse files Browse the repository at this point in the history
`IndexUtils.constructArtifactInfo(doc, centralContext)` may return null,
for example deleted artifacts. Also `IndexReader.document(int)` is
deprecated, use `.storedFields()` to retrieve document.

Signed-off-by: 司芳源 <[email protected]>
  • Loading branch information
sify21 committed Oct 20, 2024
1 parent a4ffdfb commit 1a6bf5b
Showing 1 changed file with 7 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.MultiBits;
import org.apache.lucene.index.StoredFields;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
Expand Down Expand Up @@ -162,13 +163,16 @@ public void perform() throws IOException, InvalidVersionSpecificationException {
final IndexSearcher searcher = centralContext.acquireIndexSearcher();
try {
final IndexReader ir = searcher.getIndexReader();
final StoredFields storedFields = ir.storedFields();
Bits liveDocs = MultiBits.getLiveDocs(ir);
for (int i = 0; i < ir.maxDoc(); i++) {
if (liveDocs == null || liveDocs.get(i)) {
final Document doc = ir.document(i);
final Document doc = storedFields.document(i);
final ArtifactInfo ai = IndexUtils.constructArtifactInfo(doc, centralContext);
System.out.println(ai.getGroupId() + ":" + ai.getArtifactId() + ":" + ai.getVersion() + ":"
+ ai.getClassifier() + " (sha1=" + ai.getSha1() + ")");
if (ai != null) {
System.out.println(ai.getGroupId() + ":" + ai.getArtifactId() + ":" + ai.getVersion() + ":"
+ ai.getClassifier() + " (sha1=" + ai.getSha1() + ")");
}
}
}
} finally {
Expand Down

0 comments on commit 1a6bf5b

Please sign in to comment.