diff --git a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java index 039c5ac73e9..5292459595e 100644 --- a/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java +++ b/lucene/core/src/java/org/apache/lucene/index/CheckIndex.java @@ -24,6 +24,7 @@ import java.nio.file.Path; import java.nio.file.Paths; import java.text.NumberFormat; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.Locale; @@ -455,8 +456,15 @@ public class CheckIndex implements Closeable { SegmentInfos sis = new SegmentInfos(); Status result = new Status(); result.dir = dir; + String[] files = dir.listAll(); + String lastSegmentsFile = SegmentInfos.getLastCommitSegmentsFileName(files); + if (lastSegmentsFile == null) { + throw new IndexNotFoundException("no segments* file found in " + dir + ": files: " + Arrays.toString(files)); + } try { - sis.read(dir); + // Do not use SegmentInfos.read(Directory) since the spooky + // retrying it does is not necessary here (we hold the write lock): + sis.read(dir, lastSegmentsFile); } catch (Throwable t) { if (failFast) { IOUtils.reThrow(t); diff --git a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java index 7e55dd54f80..a7f63f39452 100644 --- a/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java +++ b/lucene/core/src/java/org/apache/lucene/index/IndexWriter.java @@ -794,7 +794,15 @@ public class IndexWriter implements Closeable, TwoPhaseCommit, Accountable { // segments) pending: changed(); } else { - segmentInfos.read(directory); + String[] files = directory.listAll(); + String lastSegmentsFile = SegmentInfos.getLastCommitSegmentsFileName(files); + if (lastSegmentsFile == null) { + throw new IndexNotFoundException("no segments* file found in " + directory + ": files: " + Arrays.toString(files)); + } + + // Do not use SegmentInfos.read(Directory) since the spooky + // retrying it does is not necessary here (we hold the write lock): + segmentInfos.read(directory, lastSegmentsFile); IndexCommit commit = config.getIndexCommit(); if (commit != null) {