/*
 * Licensed to Elastic Search and Shay Banon under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. Elastic Search licenses this
 * file to you 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 org.elasticsearch.test.integration.search.scan;

import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.Client;
import org.elasticsearch.common.collect.Sets;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.test.integration.AbstractNodesTests;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import java.util.Set;

import static org.elasticsearch.index.query.xcontent.QueryBuilders.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

public class SearchScanTests extends AbstractNodesTests {

    private Client client;

    @BeforeClass public void createNodes() throws Exception {
        startNode("node1");
        startNode("node2");
        client = getClient();
    }

    @AfterClass public void closeNodes() {
        client.close();
        closeAllNodes();
    }

    protected Client getClient() {
        return client("node1");
    }

    @Test public void testSimpleScroll() throws Exception {
        try {
            client.admin().indices().prepareDelete("test").execute().actionGet();
        } catch (Exception e) {
            // ignore
        }
        client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 3)).execute().actionGet();
        client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();

        Set<String> ids = Sets.newHashSet();
        Set<String> expectedIds = Sets.newHashSet();
        for (int i = 0; i < 100; i++) {
            String id = Integer.toString(i);
            expectedIds.add(id);
            client.prepareIndex("test", "type1", id).setSource("field", i).execute().actionGet();
        }

        client.admin().indices().prepareRefresh().execute().actionGet();

        SearchResponse searchResponse = client.prepareSearch()
                .setSearchType(SearchType.SCAN)
                .setQuery(matchAllQuery())
                .setSize(7)
                .setScroll(TimeValue.timeValueMinutes(2))
                .execute().actionGet();

        assertThat(searchResponse.hits().totalHits(), equalTo(100l));

        // start scrolling, until we get not results
        while (true) {
            searchResponse = client.prepareSearchScroll(searchResponse.scrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
            assertThat(searchResponse.failedShards(), equalTo(0));
            for (SearchHit hit : searchResponse.hits()) {
                assertThat(hit.id() + "should not exists in the result set", ids.contains(hit.id()), equalTo(false));
                ids.add(hit.id());
            }
            if (searchResponse.hits().totalHits() == 0) {
                break;
            }
        }

        assertThat(expectedIds, equalTo(ids));
    }
}
@@ -2016,16 +2016,17 @@
 leScroll
+1
 () throw
@@ -3820,32 +3820,1937 @@
    %7D%0A        %7D%0A%0A
+        assertThat(expectedIds, equalTo(ids));%0A    %7D%0A%0A    @Test public void testSimpleScroll2() throws Exception %7B%0A        try %7B%0A            client.admin().indices().prepareDelete(%22test%22).execute().actionGet();%0A        %7D catch (Exception e) %7B%0A            // ignore%0A        %7D%0A        client.admin().indices().prepareCreate(%22test%22).setSettings(ImmutableSettings.settingsBuilder().put(%22index.number_of_shards%22, 1)).execute().actionGet();%0A        client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();%0A%0A        Set%3CString%3E ids = Sets.newHashSet();%0A        Set%3CString%3E expectedIds = Sets.newHashSet();%0A        for (int i = 0; i %3C 100; i++) %7B%0A            String id = Integer.toString(i);%0A            expectedIds.add(id);%0A            client.prepareIndex(%22test%22, %22type1%22, id).setSource(%22field%22, i).execute().actionGet();%0A        %7D%0A%0A        client.admin().indices().prepareRefresh().execute().actionGet();%0A%0A        SearchResponse searchResponse = client.prepareSearch()%0A                .setSearchType(SearchType.SCAN)%0A                .setQuery(matchAllQuery())%0A                .setSize(10)%0A                .setScroll(TimeValue.timeValueMinutes(2))%0A                .execute().actionGet();%0A%0A        assertThat(searchResponse.hits().totalHits(), equalTo(100l));%0A%0A        // start scrolling, until we get not results%0A        while (true) %7B%0A            searchResponse = client.prepareSearchScroll(searchResponse.scrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();%0A            assertThat(searchResponse.failedShards(), equalTo(0));%0A            for (SearchHit hit : searchResponse.hits()) %7B%0A                assertThat(hit.id() + %22should not exists in the result set%22, ids.contains(hit.id()), equalTo(false));%0A                ids.add(hit.id());%0A            %7D%0A            if (searchResponse.hits().totalHits() == 0) %7B%0A                break;%0A            %7D%0A        %7D%0A%0A
         assertTh

add another test to scan