diff --git a/server/src/main/java/org/elasticsearch/index/codec/postings/ES814InlineFieldsProducer.java b/server/src/main/java/org/elasticsearch/index/codec/postings/ES814InlineFieldsProducer.java index 23b4e7c11f6a3..b84cf6e59b8d5 100644 --- a/server/src/main/java/org/elasticsearch/index/codec/postings/ES814InlineFieldsProducer.java +++ b/server/src/main/java/org/elasticsearch/index/codec/postings/ES814InlineFieldsProducer.java @@ -362,13 +362,17 @@ private long findBlockIndex(BytesRef target) throws IOException { @Override public SeekStatus seekCeil(BytesRef target) throws IOException { - state.blockIndex = Math.toIntExact(findBlockIndex(target)); - if (state.blockIndex == meta.numBlocks) { + final int blockIndex = Math.toIntExact(findBlockIndex(target)); + if (blockIndex == meta.numBlocks) { return SeekStatus.END; } - index.seek(blockStartAddress + blockAddresses.get(state.blockIndex)); - loadFrame(); - + state.blockIndex = blockIndex; + if (state.blockIndex == loadedFrameIndex) { + resetFrame(); + } else { + index.seek(blockStartAddress + blockAddresses.get(state.blockIndex)); + loadFrame(); + } // TODO: should we support binary search here? int cmp; do { @@ -432,14 +436,6 @@ public ImpactsEnum impacts(int flags) throws IOException { @Override public BytesRef next() throws IOException { - if (state.blockIndex != loadedFrameIndex) { - long termIndexInBlock = state.termIndexInBlock; - index.seek(blockStartAddress + blockAddresses.get(state.blockIndex)); - loadFrame(); - while (termIndexInBlock-- > 0) { - scanNextTermInCurrentFrame(); - } - } if (state.termIndexInBlock >= state.numTermsInBlock) { if (++state.blockIndex >= meta.numBlocks) { return null; // exhausted @@ -448,6 +444,13 @@ public BytesRef next() throws IOException { index.seek(state.postingsFP + state.postingsBytes); } loadFrame(); + } else if (state.blockIndex != loadedFrameIndex) { + long termIndexInBlock = state.termIndexInBlock; + index.seek(blockStartAddress + blockAddresses.get(state.blockIndex)); + loadFrame(); + while (termIndexInBlock-- > 0) { + scanNextTermInCurrentFrame(); + } } scanNextTermInCurrentFrame(); return term.get(); @@ -465,11 +468,6 @@ private void decompressTerms(int compressedBytes, int originalBytes) throws IOEx private void loadFrame() throws IOException { state.termIndexInBlock = 0; - if (loadedFrameIndex == state.blockIndex) { - termsReader.setPosition(0); - index.seek(state.postingsFP); - return; - } state.numTermsInBlock = index.readVInt(); final long originalTermsBytes = index.readVLong(); final long termBytes = index.readVLong(); @@ -480,6 +478,13 @@ private void loadFrame() throws IOException { loadedFrameIndex = state.blockIndex; } + private void resetFrame() throws IOException { + assert loadedFrameIndex == state.blockIndex : loadedFrameIndex + " != " + state.blockIndex; + state.termIndexInBlock = 0; + termsReader.setPosition(0); + index.seek(state.postingsFP); + } + private void scanNextTermInCurrentFrame() throws IOException { assert loadedFrameIndex == state.blockIndex : loadedFrameIndex + " != " + state.blockIndex; term.setLength(termsReader.readVInt());