Search in sources :

Example 6 with SearchResponse

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));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 7 with SearchResponse

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();
    }
}
Also used : AbstractSearchScript(org.elasticsearch.script.AbstractSearchScript) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 8 with SearchResponse

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);
}
Also used : AbstractSearchScript(org.elasticsearch.script.AbstractSearchScript) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 9 with 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);
    }
}
Also used : IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 10 with SearchResponse

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);
}
Also used : Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) TimeValue(org.elasticsearch.common.unit.TimeValue) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1451 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)239 Script (org.elasticsearch.script.Script)223 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)208 Matchers.containsString (org.hamcrest.Matchers.containsString)156 ArrayList (java.util.ArrayList)143 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)131 SearchHit (org.elasticsearch.search.SearchHit)112 HashMap (java.util.HashMap)102 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)93 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)78 GeoPoint (org.elasticsearch.common.geo.GeoPoint)70 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)69 SearchHits (org.elasticsearch.search.SearchHits)60 AggregationBuilders.dateHistogram (org.elasticsearch.search.aggregations.AggregationBuilders.dateHistogram)60 DateTime (org.joda.time.DateTime)59 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)58 Range (org.elasticsearch.search.aggregations.bucket.range.Range)50