@@ -112,6 +112,20 @@ public int advance(int target) throws IOException {
112112 target = timestamps .advance (target );
113113 }
114114 if (target <= upTo ) {
115+ if (target > primaryFieldUpTo ) {
116+ if (target > primaryFieldSkipper .maxDocID (0 )) {
117+ primaryFieldSkipper .advance (target );
118+ }
119+ for (int level = 0 ; level < primaryFieldSkipper .numLevels (); level ++) {
120+ if (primaryFieldSkipper .minValue (level ) == primaryFieldSkipper .maxValue (level )) {
121+ primaryFieldUpTo = primaryFieldSkipper .maxDocID (level );
122+ match = Match .MAYBE_ONE_PRIMARY_SORT ;
123+ } else {
124+ break ;
125+ }
126+ }
127+ }
128+
115129 return doc = target ;
116130 }
117131 break ;
@@ -175,6 +189,16 @@ public boolean matches() throws IOException {
175189 final long value = approximation .timestamps .longValue ();
176190 yield value >= minTimestamp && value <= maxTimestamp ;
177191 }
192+ case MAYBE_ONE_PRIMARY_SORT -> {
193+ final long value = approximation .timestamps .longValue ();
194+ if (value < minTimestamp ) {
195+ approximation .match = Match .NO ;
196+ approximation .upTo = approximation .primaryFieldUpTo ;
197+ yield false ;
198+ } else {
199+ yield value <= maxTimestamp ;
200+ }
201+ }
178202 case NO_AND_SKIP , NO -> throw new IllegalStateException ("Unpositioned approximation" );
179203 };
180204 }
@@ -199,6 +223,8 @@ enum Match {
199223 NO_AND_SKIP ,
200224 /** Document values need to be checked to verify matches */
201225 MAYBE ,
226+ /** Same as {@link #MAYBE}, but only if there is one primary sort value in current skip entry */
227+ MAYBE_ONE_PRIMARY_SORT ,
202228 /** All docs in the range match */
203229 YES ;
204230 }
0 commit comments