use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchScrollIT method testSimpleScrollQueryThenFetchSmallSizeUnevenDistribution.
public void testSimpleScrollQueryThenFetchSmallSizeUnevenDistribution() throws Exception {
client().admin().indices().prepareCreate("test").setSettings(Settings.builder().put("index.number_of_shards", 3)).execute().actionGet();
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
for (int i = 0; i < 100; i++) {
String routing = "0";
if (i > 90) {
routing = "1";
} else if (i > 60) {
routing = "2";
}
client().prepareIndex("test", "type1", Integer.toString(i)).setSource("field", i).setRouting(routing).execute().actionGet();
}
client().admin().indices().prepareRefresh().execute().actionGet();
SearchResponse searchResponse = client().prepareSearch().setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(matchAllQuery()).setSize(3).setScroll(TimeValue.timeValueMinutes(2)).addSort("field", SortOrder.ASC).execute().actionGet();
try {
long counter = 0;
assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
assertThat(searchResponse.getHits().getHits().length, equalTo(3));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
}
for (int i = 0; i < 32; i++) {
searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
assertThat(searchResponse.getHits().getHits().length, equalTo(3));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
}
}
// and now, the last one is one
searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
assertThat(searchResponse.getHits().getHits().length, equalTo(1));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
}
// a the last is zero
searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(2)).execute().actionGet();
assertThat(searchResponse.getHits().getTotalHits(), equalTo(100L));
assertThat(searchResponse.getHits().getHits().length, equalTo(0));
for (SearchHit hit : searchResponse.getHits()) {
assertThat(((Number) hit.getSortValues()[0]).longValue(), equalTo(counter++));
}
} finally {
clearScroll(searchResponse.getScrollId());
}
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchScrollIT method testThatNonExistingScrollIdReturnsCorrectException.
public void testThatNonExistingScrollIdReturnsCorrectException() throws Exception {
client().prepareIndex("index", "type", "1").setSource("field", "value").execute().get();
refresh();
SearchResponse searchResponse = client().prepareSearch("index").setSize(1).setScroll("1m").get();
assertThat(searchResponse.getScrollId(), is(notNullValue()));
ClearScrollResponse clearScrollResponse = client().prepareClearScroll().addScrollId(searchResponse.getScrollId()).get();
assertThat(clearScrollResponse.isSucceeded(), is(true));
assertThrows(internalCluster().transportClient().prepareSearchScroll(searchResponse.getScrollId()), RestStatus.NOT_FOUND);
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchScrollWithFailingNodesIT method testScanScrollWithShardExceptions.
public void testScanScrollWithShardExceptions() throws Exception {
internalCluster().startNode();
internalCluster().startNode();
assertAcked(prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put(ShardsLimitAllocationDecider.INDEX_TOTAL_SHARDS_PER_NODE_SETTING.getKey(), 1)));
List<IndexRequestBuilder> writes = new ArrayList<>();
for (int i = 0; i < 100; i++) {
writes.add(client().prepareIndex("test", "type1").setSource(jsonBuilder().startObject().field("field", i).endObject()));
}
indexRandom(false, writes);
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(10).setScroll(TimeValue.timeValueMinutes(1)).get();
assertAllSuccessful(searchResponse);
long numHits = 0;
do {
numHits += searchResponse.getHits().getHits().length;
searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(1)).get();
assertAllSuccessful(searchResponse);
} while (searchResponse.getHits().getHits().length > 0);
assertThat(numHits, equalTo(100L));
clearScroll("_all");
internalCluster().stopRandomNonMasterNode();
searchResponse = client().prepareSearch().setQuery(matchAllQuery()).setSize(10).setScroll(TimeValue.timeValueMinutes(1)).get();
assertThat(searchResponse.getSuccessfulShards(), lessThan(searchResponse.getTotalShards()));
numHits = 0;
int numberOfSuccessfulShards = searchResponse.getSuccessfulShards();
do {
numHits += searchResponse.getHits().getHits().length;
searchResponse = client().prepareSearchScroll(searchResponse.getScrollId()).setScroll(TimeValue.timeValueMinutes(1)).get();
assertThat(searchResponse.getSuccessfulShards(), equalTo(numberOfSuccessfulShards));
} while (searchResponse.getHits().getHits().length > 0);
assertThat(numHits, greaterThan(0L));
clearScroll(searchResponse.getScrollId());
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchQueryIT method testBoolQueryMinShouldMatchBiggerThanNumberOfShouldClauses.
public void testBoolQueryMinShouldMatchBiggerThanNumberOfShouldClauses() throws IOException {
createIndex("test");
client().prepareIndex("test", "type1", "1").setSource("field1", new String[] { "value1", "value2", "value3" }).get();
client().prepareIndex("test", "type1", "2").setSource("field2", "value1").get();
refresh();
BoolQueryBuilder boolQuery = boolQuery().must(termQuery("field1", "value1")).should(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(3));
SearchResponse searchResponse = client().prepareSearch().setQuery(boolQuery).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("1"));
boolQuery = boolQuery().must(termQuery("field1", "value1")).should(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(1)).minimumShouldMatch(2);
searchResponse = client().prepareSearch().setQuery(boolQuery).get();
assertHitCount(searchResponse, 0L);
boolQuery = boolQuery().should(termQuery("field1", "value1")).should(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(3)).minimumShouldMatch(1);
searchResponse = client().prepareSearch().setQuery(boolQuery).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("1"));
boolQuery = boolQuery().must(termQuery("field1", "value1")).must(boolQuery().should(termQuery("field1", "value1")).should(termQuery("field1", "value2")).minimumShouldMatch(3));
searchResponse = client().prepareSearch().setQuery(boolQuery).get();
assertHitCount(searchResponse, 0L);
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchQueryIT method testSpecialRangeSyntaxInQueryString.
public void testSpecialRangeSyntaxInQueryString() {
createIndex("test");
client().prepareIndex("test", "type1", "1").setSource("str", "kimchy", "date", "2012-02-01", "num", 12).get();
client().prepareIndex("test", "type1", "2").setSource("str", "shay", "date", "2012-02-05", "num", 20).get();
refresh();
SearchResponse searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:>19")).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("2"));
searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:>20")).get();
assertHitCount(searchResponse, 0L);
searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:>=20")).get();
assertHitCount(searchResponse, 1L);
assertFirstHit(searchResponse, hasId("2"));
searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:>11")).get();
assertHitCount(searchResponse, 2L);
searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:<20")).get();
assertHitCount(searchResponse, 1L);
searchResponse = client().prepareSearch().setQuery(queryStringQuery("num:<=20")).get();
assertHitCount(searchResponse, 2L);
searchResponse = client().prepareSearch().setQuery(queryStringQuery("+num:>11 +num:<20")).get();
assertHitCount(searchResponse, 1L);
}
Aggregations