Skip to content

Commit cc39eb7

Browse files
committed
add a narrowing search scan test
1 parent 64bf849 commit cc39eb7

File tree

3 files changed

+244
-133
lines changed

3 files changed

+244
-133
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/client/action/search/SearchRequestBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ public SearchRequestBuilder setHighlighterOrder(String order) {
479479

480480
/**
481481
* Sets the source of the request as a json string. Note, settings anything other
482-
* than the search type will cause this source to be overridden, consifer using
482+
* than the search type will cause this source to be overridden, consider using
483483
* {@link #setExtraSource(String)}.
484484
*/
485485
public SearchRequestBuilder setSource(String source) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
/*
2+
* Licensed to Elastic Search and Shay Banon under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. Elastic Search licenses this
6+
* file to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.test.integration.search.scan;
21+
22+
import org.elasticsearch.action.search.SearchResponse;
23+
import org.elasticsearch.action.search.SearchType;
24+
import org.elasticsearch.client.Client;
25+
import org.elasticsearch.common.collect.Sets;
26+
import org.elasticsearch.common.settings.ImmutableSettings;
27+
import org.elasticsearch.common.unit.TimeValue;
28+
import org.elasticsearch.search.SearchHit;
29+
import org.elasticsearch.test.integration.AbstractNodesTests;
30+
import org.testng.annotations.AfterClass;
31+
import org.testng.annotations.BeforeClass;
32+
import org.testng.annotations.Test;
33+
34+
import java.util.Set;
35+
36+
import static org.elasticsearch.index.query.QueryBuilders.*;
37+
import static org.hamcrest.MatcherAssert.*;
38+
import static org.hamcrest.Matchers.*;
39+
40+
public class SearchScanScrollingTests extends AbstractNodesTests {
41+
42+
private Client client;
43+
44+
@BeforeClass public void createNodes() throws Exception {
45+
startNode("node1");
46+
startNode("node2");
47+
client = getClient();
48+
}
49+
50+
@AfterClass public void closeNodes() {
51+
client.close();
52+
closeAllNodes();
53+
}
54+
55+
protected Client getClient() {
56+
return client("node1");
57+
}
58+
59+
@Test public void shard1docs100size3() throws Exception {
60+
testScroll(1, 100, 3);
61+
}
62+
63+
@Test public void shard1docs100size7() throws Exception {
64+
testScroll(1, 100, 7);
65+
}
66+
67+
@Test public void shard1docs100size13() throws Exception {
68+
testScroll(1, 100, 13);
69+
}
70+
71+
@Test public void shard1docs100size24() throws Exception {
72+
testScroll(1, 100, 24);
73+
}
74+
75+
@Test public void shard1docs100size45() throws Exception {
76+
testScroll(1, 100, 45);
77+
}
78+
79+
@Test public void shard1docs100size63() throws Exception {
80+
testScroll(1, 100, 63);
81+
}
82+
83+
@Test public void shard1docs100size89() throws Exception {
84+
testScroll(1, 100, 89);
85+
}
86+
87+
@Test public void shard1docs100size99() throws Exception {
88+
testScroll(1, 100, 99);
89+
}
90+
91+
@Test public void shard1docs100size100() throws Exception {
92+
testScroll(1, 100, 100);
93+
}
94+
95+
@Test public void shard1docs100size101() throws Exception {
96+
testScroll(1, 100, 101);
97+
}
98+
99+
@Test public void shard1docs100size120() throws Exception {
100+
testScroll(1, 100, 120);
101+
}
102+
103+
@Test public void shard3docs100size3() throws Exception {
104+
testScroll(3, 100, 3);
105+
}
106+
107+
@Test public void shard3docs100size7() throws Exception {
108+
testScroll(3, 100, 7);
109+
}
110+
111+
@Test public void shard3docs100size13() throws Exception {
112+
testScroll(3, 100, 13);
113+
}
114+
115+
@Test public void shard3docs100size24() throws Exception {
116+
testScroll(3, 100, 24);
117+
}
118+
119+
@Test public void shard3docs100size45() throws Exception {
120+
testScroll(3, 100, 45);
121+
}
122+
123+
@Test public void shard3docs100size63() throws Exception {
124+
testScroll(3, 100, 63);
125+
}
126+
127+
@Test public void shard3docs100size89() throws Exception {
128+
testScroll(3, 100, 89);
129+
}
130+
131+
@Test public void shard3docs100size120() throws Exception {
132+
testScroll(3, 100, 120);
133+
}
134+
135+
@Test public void shard3docs100size3Unbalanced() throws Exception {
136+
testScroll(3, 100, 3, true);
137+
}
138+
139+
@Test public void shard3docs100size7Unbalanced() throws Exception {
140+
testScroll(3, 100, 7, true);
141+
}
142+
143+
@Test public void shard3docs100size13Unbalanced() throws Exception {
144+
testScroll(3, 100, 13, true);
145+
}
146+
147+
@Test public void shard3docs100size24Unbalanced() throws Exception {
148+
testScroll(3, 100, 24, true);
149+
}
150+
151+
@Test public void shard3docs100size45Unbalanced() throws Exception {
152+
testScroll(3, 100, 45, true);
153+
}
154+
155+
@Test public void shard3docs100size63Unbalanced() throws Exception {
156+
testScroll(3, 100, 63, true);
157+
}
158+
159+
@Test public void shard3docs100size89Unbalanced() throws Exception {
160+
testScroll(3, 100, 89, true);
161+
}
162+
163+
@Test public void shard3docs100size120Unbalanced() throws Exception {
164+
testScroll(3, 100, 120);
165+
}
166+
167+
private void testScroll(int numberOfShards, long numberOfDocs, int size) throws Exception {
168+
testScroll(numberOfShards, numberOfDocs, size, false);
169+
}
170+
171+
private void testScroll(int numberOfShards, long numberOfDocs, int size, boolean unbalanced) throws Exception {
172+
try {
173+
client.admin().indices().prepareDelete("test").execute().actionGet();
174+
} catch (Exception e) {
175+
// ignore
176+
}
177+
client.admin().indices().prepareCreate("test").setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", numberOfShards)).execute().actionGet();
178+
client.admin().cluster().prepareHealth().setWaitForGreenStatus().execute().actionGet();
179+
180+
Set<String> ids = Sets.newHashSet();
181+
Set<String> expectedIds = Sets.newHashSet();
182+
for (int i = 0; i < numberOfDocs; i++) {
183+
String id = Integer.toString(i);
184+
expectedIds.add(id);
185+
String routing = null;
186+
if (unbalanced) {
187+
if (i < (numberOfDocs * 0.6)) {
188+
routing = "0";
189+
} else if (i < (numberOfDocs * 0.9)) {
190+
routing = "1";
191+
} else {
192+
routing = "2";
193+
}
194+
}
195+
client.prepareIndex("test", "type1", id).setRouting(routing).setSource("field", i).execute().actionGet();
196+
}
197+
198+
client.admin().indices().prepareRefresh().execute().actionGet();
199+
200+
SearchResponse searchResponse = client.prepareSearch()
201+
.setSearchType(SearchType.SCAN)
202+
.setQuery(matchAllQuery())
203+
.setSize(size)
204+
.setScroll(TimeValue.timeValueMinutes(2))
205+
.execute().actionGet();
206+
207+
assertThat(searchResponse.hits().totalHits(), equalTo(numberOfDocs));
208+
209+
// start scrolling, until we get not results
210+
while (true) {
211+
searchResponse = client.prepareSearchScroll(searchResponse.scrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
212+
assertThat(searchResponse.hits().totalHits(), equalTo(numberOfDocs));
213+
assertThat(searchResponse.failedShards(), equalTo(0));
214+
for (SearchHit hit : searchResponse.hits()) {
215+
assertThat(hit.id() + "should not exists in the result set", ids.contains(hit.id()), equalTo(false));
216+
ids.add(hit.id());
217+
}
218+
if (searchResponse.hits().hits().length == 0) {
219+
break;
220+
}
221+
}
222+
223+
assertThat(expectedIds, equalTo(ids));
224+
}
225+
}

0 commit comments

Comments
 (0)