Skip to content

Instantly share code, notes, and snippets.

@karussell
Created February 23, 2011 16:05
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save karussell/840604 to your computer and use it in GitHub Desktop.
@Test public void testSimpleScroll3() throws Exception {
try {
client.admin().indices().prepareDelete("test1").execute().actionGet();
client.admin().indices().prepareDelete("test2").execute().actionGet();
client.admin().indices().prepareDelete("unrelatedindex").execute().actionGet();
} catch (Exception e) {
// ignore
}
client.admin().indices().prepareCreate("test1").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 2))
.execute().actionGet();
client.admin().indices().prepareCreate("test2").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 2))
.execute().actionGet();
client.admin().indices().prepareCreate("unrelatedindex").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 2))
.execute().actionGet();
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
Set<String> ids = Sets.newHashSet();
Set<String> expectedIds = Sets.newHashSet();
Set<String> expectedIds2 = Sets.newHashSet();
for (int i = 0; i < 3; i++) {
String id = Integer.toString(i);
expectedIds.add(id);
client.prepareIndex("test1", "type1", id).setSource("field", i).execute().actionGet();
}
for (int i = 0; i < 2; i++) {
String id = Integer.toString(i);
expectedIds2.add(id);
client.prepareIndex("test2", "type1", id).setSource("field", i).execute().actionGet();
}
client.admin().indices().prepareRefresh().execute().actionGet();
SearchResponse searchResponse = client.prepareSearch("test1")
.setSearchType(SearchType.SCAN)
.setQuery(matchAllQuery())
.setSize(10)
.setScroll(TimeValue.timeValueMinutes(30))
.execute().actionGet();
assertThat(searchResponse.hits().totalHits(), equalTo(3l));
// start scrolling, until we get not results
while (true) {
searchResponse = client.prepareSearchScroll(searchResponse.scrollId()).setScroll(TimeValue.timeValueMinutes(30)).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));
// feed something into a 3rd index via bulk API
BulkRequestBuilder brb = client.prepareBulk();
for (int i = 5; i < 10; i++) {
String id = Integer.toString(i);
expectedIds2.add(id);
XContentBuilder source = JsonXContent.unCachedContentBuilder().startObject();
source.field("field", i);
source.endObject();
brb.add(Requests.indexRequest("unrelatedindex").type("type1").id(id).source(source));
}
brb.execute().actionGet();
ids.clear();
searchResponse = client.prepareSearch("test2")
.setSearchType(SearchType.SCAN)
.setQuery(matchAllQuery())
.setSize(10)
.setScroll(TimeValue.timeValueMinutes(30))
.execute().actionGet();
assertThat(searchResponse.hits().totalHits(), equalTo(2l));
// start scrolling, until we get not results
while (true) {
searchResponse = client.prepareSearchScroll(searchResponse.scrollId()).setScroll(TimeValue.timeValueMinutes(30)).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(expectedIds2, equalTo(ids));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment