diff --git a/server/src/main/java/org/elasticsearch/index/codec/vectors/DocIdsWriter.java b/server/src/main/java/org/elasticsearch/index/codec/vectors/DocIdsWriter.java index ca4bbf0377e06..d46a6301b60a2 100644 --- a/server/src/main/java/org/elasticsearch/index/codec/vectors/DocIdsWriter.java +++ b/server/src/main/java/org/elasticsearch/index/codec/vectors/DocIdsWriter.java @@ -70,15 +70,14 @@ final class DocIdsWriter { DocIdsWriter() {} void writeDocIds(IntToIntFunction docIds, int count, DataOutput out) throws IOException { - // docs can be sorted either when all docs in a block have the same value - // or when a segment is sorted if (count == 0) { - out.writeByte(CONTINUOUS_IDS); return; } if (count > scratch.length) { scratch = new int[count]; } + // docs can be sorted either when all docs in a block have the same value + // or when a segment is sorted boolean strictlySorted = true; int min = docIds.apply(0); int max = min; @@ -215,6 +214,9 @@ private static void writeIdsAsBitSet(IntToIntFunction docIds, int count, DataOut /** Read {@code count} integers into {@code docIDs}. */ void readInts(IndexInput in, int count, int[] docIDs) throws IOException { + if (count == 0) { + return; + } if (count > scratch.length) { scratch = new int[count]; } diff --git a/server/src/test/java/org/elasticsearch/index/codec/vectors/DocIdsWriterTests.java b/server/src/test/java/org/elasticsearch/index/codec/vectors/DocIdsWriterTests.java index 17270abdbf76d..8f26369e6ded4 100644 --- a/server/src/test/java/org/elasticsearch/index/codec/vectors/DocIdsWriterTests.java +++ b/server/src/test/java/org/elasticsearch/index/codec/vectors/DocIdsWriterTests.java @@ -40,6 +40,12 @@ public class DocIdsWriterTests extends LuceneTestCase { + public void testNoDocs() throws Exception { + try (Directory dir = newDirectory()) { + test(dir, new int[0]); + } + } + public void testRandom() throws Exception { int numIters = atLeast(100); try (Directory dir = newDirectory()) {