From aaf1060b645482aa8d55b1b95266d54bc55a16a3 Mon Sep 17 00:00:00 2001 From: Ignacio Vera Date: Tue, 17 Jun 2025 16:41:44 +0200 Subject: [PATCH 1/2] Handle properly when trying to serialize a zero documents cluster in IVF --- .../elasticsearch/index/codec/vectors/DocIdsWriter.java | 7 +++++-- .../index/codec/vectors/DocIdsWriterTests.java | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) 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..bd65957005b6b 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,8 +70,6 @@ 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; @@ -79,6 +77,8 @@ void writeDocIds(IntToIntFunction docIds, int count, DataOutput out) throws IOEx 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; @@ -258,6 +258,9 @@ private DocIdSetIterator readBitSetIterator(IndexInput in, int count) throws IOE } private static void readContinuousIds(IndexInput in, int count, int[] docIDs) throws IOException { + if (count == 0) { + return; + } int start = in.readVInt(); for (int i = 0; i < count; i++) { docIDs[i] = start + i; 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()) { From feaae39dd551842a5760535706c474db74b650fe Mon Sep 17 00:00:00 2001 From: Ignacio Vera Date: Tue, 17 Jun 2025 16:51:22 +0200 Subject: [PATCH 2/2] iter --- .../elasticsearch/index/codec/vectors/DocIdsWriter.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) 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 bd65957005b6b..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 @@ -71,7 +71,6 @@ final class DocIdsWriter { void writeDocIds(IntToIntFunction docIds, int count, DataOutput out) throws IOException { if (count == 0) { - out.writeByte(CONTINUOUS_IDS); return; } if (count > scratch.length) { @@ -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]; } @@ -258,9 +260,6 @@ private DocIdSetIterator readBitSetIterator(IndexInput in, int count) throws IOE } private static void readContinuousIds(IndexInput in, int count, int[] docIDs) throws IOException { - if (count == 0) { - return; - } int start = in.readVInt(); for (int i = 0; i < count; i++) { docIDs[i] = start + i;