/*
 * Copyright 2017-2021 B2i Healthcare Pte Ltd, http://b2i.sg
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.b2international.index;

import java.io.IOException;
import java.util.Iterator;

import com.b2international.index.query.Query;
import com.google.common.base.Strings;

/**
 * @since 6.0
 * @param <T> - the type of scrolled documents
 */
public final class ScrollingIterator<T> implements Iterator<Hits<T>> {

	private final Searcher searcher;
	private final Query<T> query;
	
	private Hits<T> hits;
	private boolean done;

	public ScrollingIterator(Searcher searcher, Query<T> query) {
		this.searcher = searcher;
		this.query = query;
		
		// ensure we have defined scroll keep alive value
		if (Strings.isNullOrEmpty(query.getScrollKeepAlive())) {
			query.setScrollKeepAlive(Query.DEFAULT_SCROLL_KEEP_ALIVE);
		}
	}
	
	@Override
	public boolean hasNext() {
		
		if (done) {
			return false;
		}
		
		try {
			if (hits == null) {
				hits = searcher.search(query);
			} else {
				hits = searcher.scroll(new Scroll<>(query.getSelection(), query.getFields(), hits.getScrollId(), query.getScrollKeepAlive()));
			}
		} catch (IOException e) {
			throw new IndexException("Failed to load next page of scrolled documents", e);
		}
		
		if (!hits.iterator().hasNext()) {
			searcher.cancelScroll(hits.getScrollId());
			hits = null;
			done = true;
			return false;
		}
		
		return true;
	}

	@Override
	public Hits<T> next() {
		return hits;
	}

}

@@ -763,65 +763,49 @@
 ry;%0A
-import com.google.common.base.Strings;%0A%0A/**%0A * @since 6.0
+%0A/**%0A * @since 6.0 (as ScrollingIterator)
 %0A * 
@@ -825,30 +825,27 @@
 the type of 
-scroll
+pag
 ed documents
@@ -869,24 +869,26 @@
  class S
-crolling
+earchAfter
 Iterator
@@ -1046,24 +1046,26 @@
 public S
-crolling
+earchAfter
 Iterator
@@ -1156,188 +1156,8 @@
 ry;%0A
-%09%09%0A%09%09// ensure we have defined scroll keep alive value%0A%09%09if (Strings.isNullOrEmpty(query.getScrollKeepAlive())) %7B%0A%09%09%09query.setScrollKeepAlive(Query.DEFAULT_SCROLL_KEEP_ALIVE);%0A%09%09%7D%0A
 %09%7D%0A%09
@@ -1248,18 +1248,49 @@
 ry %7B%0A%09%09%09
-if
+%0A%09%09%09final Query%3CT%3E queryForPage =
  (hits =
@@ -1301,192 +1301,130 @@
 ll) 
-%7B
 %0A%09%09%09%09
-hits = searcher.search(query);%0A%09%09%09%7D else %7B%0A%09%09%09%09hits = searcher.scroll(new Scroll%3C%3E(query.getSelection(), query.getFields(), hits.getScrollId(), query.getScrollKeepAlive())
+? query %0A%09%09%09%09: query.withSearchAfter(hits.getSearchAfter()).build();%0A%09%09%09%0A%09%09%09hits = searcher.search(queryForPage
 );%0A%09%09%09
-%7D
 %0A%09%09%7D
@@ -1509,17 +1509,8 @@
  of 
-scrolled 
 docu
@@ -1568,54 +1568,8 @@
 ) %7B%0A
-%09%09%09searcher.cancelScroll(hits.getScrollId());%0A
 %09%09%09h

Use searchAfter instead of scrolling in Hits iterator