diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SortedNumericDocValuesSyntheticFieldLoader.java b/server/src/main/java/org/elasticsearch/index/mapper/SortedNumericDocValuesSyntheticFieldLoader.java index 98ceeb7c7f387..b06d5bac9da89 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SortedNumericDocValuesSyntheticFieldLoader.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SortedNumericDocValuesSyntheticFieldLoader.java @@ -188,7 +188,7 @@ private class SingletonDocValuesLoader implements DocValuesLoader, Values { private final int[] docIdsInLeaf; private final long[] values; private final boolean[] hasValue; - private int idx = 0; + private int idx = -1; private SingletonDocValuesLoader(int[] docIdsInLeaf, long[] values, boolean[] hasValue) { this.docIdsInLeaf = docIdsInLeaf; @@ -198,11 +198,12 @@ private SingletonDocValuesLoader(int[] docIdsInLeaf, long[] values, boolean[] ha @Override public boolean advanceToDoc(int docId) throws IOException { - int index = Arrays.binarySearch(docIdsInLeaf, idx, docIdsInLeaf.length, docId); - if (index < 0) { - throw new IllegalArgumentException(docId + " is not expected to be called"); + idx++; + if (docIdsInLeaf[idx] != docId) { + throw new IllegalArgumentException( + "expected to be called with [" + docIdsInLeaf[idx] + "] but was called with " + docId + " instead" + ); } - idx = index; return hasValue[idx]; } diff --git a/server/src/main/java/org/elasticsearch/index/mapper/SortedSetDocValuesSyntheticFieldLoaderLayer.java b/server/src/main/java/org/elasticsearch/index/mapper/SortedSetDocValuesSyntheticFieldLoaderLayer.java index 395c8f33527c1..68781830ffe8f 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/SortedSetDocValuesSyntheticFieldLoaderLayer.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/SortedSetDocValuesSyntheticFieldLoaderLayer.java @@ -185,7 +185,7 @@ private static class SingletonDocValuesLoader implements DocValuesLoader, DocVal private final int[] uniqueOrds; private final BytesRef[] converted; - private int idx = 0; + private int idx = -1; private SingletonDocValuesLoader(int[] docIdsInLeaf, int[] ords, int[] uniqueOrds, BytesRef[] converted) { this.docIdsInLeaf = docIdsInLeaf; @@ -196,11 +196,12 @@ private SingletonDocValuesLoader(int[] docIdsInLeaf, int[] ords, int[] uniqueOrd @Override public boolean advanceToDoc(int docId) throws IOException { - int index = Arrays.binarySearch(docIdsInLeaf, idx, docIdsInLeaf.length, docId); - if (index < 0) { - throw new IllegalArgumentException(docId + " is not expected to be called"); + idx++; + if (docIdsInLeaf[idx] != docId) { + throw new IllegalArgumentException( + "expected to be called with [" + docIdsInLeaf[idx] + "] but was called with " + docId + " instead" + ); } - idx = index; return ords[idx] >= 0; }