diff --git a/server/src/main/java/org/opensearch/indices/query/PluggableQueryCache.java b/server/src/main/java/org/opensearch/indices/query/PluggableQueryCache.java index 3c49a67ce26ed..269e778d19168 100644 --- a/server/src/main/java/org/opensearch/indices/query/PluggableQueryCache.java +++ b/server/src/main/java/org/opensearch/indices/query/PluggableQueryCache.java @@ -910,6 +910,7 @@ private int serializeDocIdSet(DocIdSet set, BytesStreamOutput os) { } os.writeByte(classByte); int numDocs = 0; + int prevDoc = 0; try { DocIdSetIterator iterator = set.iterator(); @@ -919,7 +920,9 @@ private int serializeDocIdSet(DocIdSet set, BytesStreamOutput os) { byte[] nextBlock = new byte[BLOCK_SIZE]; while (nextDoc != NO_MORE_DOCS) { nextDoc = iterator.nextDoc(); - blockPos = writeVInt(nextDoc, currentBlock, nextBlock, blockPos); + int delta = nextDoc - prevDoc; + prevDoc = nextDoc; + blockPos = writeVInt(delta, currentBlock, nextBlock, blockPos); numDocs++; if (blockPos >= BLOCK_SIZE) { os.writeBytes(currentBlock); @@ -1025,6 +1028,7 @@ private long readVInt(byte[] currentBlock, byte[] nextBlock, int pos) throws IOE private int addDocs(BytesStreamInput is, Consumer docAdder) { // Returns bitSetLength which may or may not be needed. int bitSetLength = 0; + int prevDoc = 0; try { byte[] currentBlock = new byte[BLOCK_SIZE]; byte[] nextBlock = new byte[BLOCK_SIZE]; @@ -1034,7 +1038,8 @@ private int addDocs(BytesStreamInput is, Consumer docAdder) { is.readBytes(nextBlock, 0, BLOCK_SIZE); long packedVInt = readVInt(currentBlock, nextBlock, blockPos); - int nextDoc = (int) packedVInt; // Apparently equivalent to packedVInt & 0xffffffff + int delta = (int) packedVInt; // Apparently equivalent to packedVInt & 0xffffffff + int nextDoc = prevDoc + delta; blockPos = (int) (packedVInt >> 32); // If nextPos is into nextBlock, we need to fetch another block if (blockPos >= BLOCK_SIZE) { @@ -1046,10 +1051,12 @@ private int addDocs(BytesStreamInput is, Consumer docAdder) { while (nextDoc != NO_MORE_DOCS) { docAdder.accept(nextDoc); + prevDoc = nextDoc; bitSetLength++; packedVInt = readVInt(currentBlock, nextBlock, blockPos); - nextDoc = (int) packedVInt; // Equivalent to packedVInt & 0xffffffff + delta = (int) packedVInt; // Equivalent to packedVInt & 0xffffffff + nextDoc = prevDoc + delta; blockPos = (int) (packedVInt >> 32); // If nextPos is into nextBlock, we need to fetch another block if (blockPos >= BLOCK_SIZE) {