use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class ScriptFieldIT method testNativeScript.
public void testNativeScript() throws InterruptedException, ExecutionException {
indexRandom(true, client().prepareIndex("test", "type1", "1").setSource("text", "doc1"), client().prepareIndex("test", "type1", "2").setSource("text", "doc2"), client().prepareIndex("test", "type1", "3").setSource("text", "doc3"), client().prepareIndex("test", "type1", "4").setSource("text", "doc4"), client().prepareIndex("test", "type1", "5").setSource("text", "doc5"), client().prepareIndex("test", "type1", "6").setSource("text", "doc6"));
client().admin().indices().prepareFlush("test").execute().actionGet();
SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("int", new Script(ScriptType.INLINE, "native", "int", Collections.emptyMap())).addScriptField("float", new Script(ScriptType.INLINE, "native", "float", Collections.emptyMap())).addScriptField("double", new Script(ScriptType.INLINE, "native", "double", Collections.emptyMap())).addScriptField("long", new Script(ScriptType.INLINE, "native", "long", Collections.emptyMap())).execute().actionGet();
assertThat(sr.getHits().getHits().length, equalTo(6));
for (SearchHit hit : sr.getHits().getHits()) {
Object result = hit.getFields().get("int").getValues().get(0);
assertThat(result, equalTo((Object) intArray));
result = hit.getFields().get("long").getValues().get(0);
assertThat(result, equalTo((Object) longArray));
result = hit.getFields().get("float").getValues().get(0);
assertThat(result, equalTo((Object) floatArray));
result = hit.getFields().get("double").getValues().get(0);
assertThat(result, equalTo((Object) doubleArray));
}
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchCancellationIT method testCancellationOfScrollSearches.
public void testCancellationOfScrollSearches() throws Exception {
List<ScriptedBlockPlugin> plugins = initBlockFactory();
indexTestData();
logger.info("Executing search");
ListenableActionFuture<SearchResponse> searchResponse = client().prepareSearch("test").setScroll(TimeValue.timeValueSeconds(10)).setSize(5).setQuery(scriptQuery(new Script(ScriptType.INLINE, "native", NativeTestScriptedBlockFactory.TEST_NATIVE_BLOCK_SCRIPT, Collections.emptyMap()))).execute();
awaitForBlock(plugins);
cancelSearch(SearchAction.NAME);
disableBlocks(plugins);
SearchResponse response = ensureSearchWasCancelled(searchResponse);
if (response != null) {
// The response might not have failed on all shards - we need to clean scroll
logger.info("Cleaning scroll with id {}", response.getScrollId());
client().prepareClearScroll().addScrollId(response.getScrollId()).get();
}
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class SearchCancellationIT method testCancellationDuringQueryPhase.
public void testCancellationDuringQueryPhase() throws Exception {
List<ScriptedBlockPlugin> plugins = initBlockFactory();
indexTestData();
logger.info("Executing search");
ListenableActionFuture<SearchResponse> searchResponse = client().prepareSearch("test").setQuery(scriptQuery(new Script(ScriptType.INLINE, "native", NativeTestScriptedBlockFactory.TEST_NATIVE_BLOCK_SCRIPT, Collections.emptyMap()))).execute();
awaitForBlock(plugins);
cancelSearch(SearchAction.NAME);
disableBlocks(plugins);
logger.info("Segments {}", XContentHelper.toString(client().admin().indices().prepareSegments("test").get(), FORMAT_PARAMS));
ensureSearchWasCancelled(searchResponse);
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class StressSearchServiceReaperIT method testStressReaper.
// see issue #5165 - this test fails each time without the fix in pull #5170
public void testStressReaper() throws ExecutionException, InterruptedException {
int num = randomIntBetween(100, 150);
IndexRequestBuilder[] builders = new IndexRequestBuilder[num];
for (int i = 0; i < builders.length; i++) {
builders[i] = client().prepareIndex("test", "type", "" + i).setSource("f", English.intToEnglish(i));
}
createIndex("test");
indexRandom(true, builders);
final int iterations = scaledRandomIntBetween(500, 1000);
for (int i = 0; i < iterations; i++) {
SearchResponse searchResponse = client().prepareSearch("test").setQuery(matchAllQuery()).setSize(num).get();
assertNoFailures(searchResponse);
assertHitCount(searchResponse, num);
}
}
use of org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.
the class AggregationsIntegrationIT method testScroll.
public void testScroll() {
final int size = randomIntBetween(1, 4);
SearchResponse response = client().prepareSearch("index").setSize(size).setScroll(new TimeValue(500)).addAggregation(terms("f").field("f")).get();
assertSearchResponse(response);
Aggregations aggregations = response.getAggregations();
assertNotNull(aggregations);
Terms terms = aggregations.get("f");
assertEquals(Math.min(numDocs, 3L), terms.getBucketByKey("0").getDocCount());
int total = response.getHits().getHits().length;
while (response.getHits().getHits().length > 0) {
response = client().prepareSearchScroll(response.getScrollId()).setScroll(new TimeValue(500)).execute().actionGet();
assertNull(response.getAggregations());
total += response.getHits().getHits().length;
}
clearScroll(response.getScrollId());
assertEquals(numDocs, total);
}
Aggregations