Search in sources :

Example 1 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class ContextAndHeaderTransportIT method testThatGeoShapeQueryGetRequestContainsContextAndHeaders.

public void testThatGeoShapeQueryGetRequestContainsContextAndHeaders() throws Exception {
    transportClient().prepareIndex(lookupIndex, "type", "1").setSource(jsonBuilder().startObject().field("name", "Munich Suburban Area").startObject("location").field("type", "polygon").startArray("coordinates").startArray().startArray().value(11.34).value(48.25).endArray().startArray().value(11.68).value(48.25).endArray().startArray().value(11.65).value(48.06).endArray().startArray().value(11.37).value(48.13).endArray().startArray().value(11.34).value(48.25).endArray().endArray().endArray().endObject().endObject()).get();
    // second document
    transportClient().prepareIndex(queryIndex, "type", "1").setSource(jsonBuilder().startObject().field("name", "Munich Center").startObject("location").field("type", "point").startArray("coordinates").value(11.57).value(48.13).endArray().endObject().endObject()).get();
    transportClient().admin().indices().prepareRefresh(lookupIndex, queryIndex).get();
    GeoShapeQueryBuilder queryBuilder = QueryBuilders.geoShapeQuery("location", "1", "type").indexedShapeIndex(lookupIndex).indexedShapePath("location");
    SearchResponse searchResponse = transportClient().prepareSearch(queryIndex).setQuery(queryBuilder).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 1);
    assertThat(requests, hasSize(greaterThan(0)));
    assertGetRequestsContainHeaders();
}
Also used : GeoShapeQueryBuilder(org.elasticsearch.index.query.GeoShapeQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class ContextAndHeaderTransportIT method testThatTermsLookupGetRequestContainsContextAndHeaders.

public void testThatTermsLookupGetRequestContainsContextAndHeaders() throws Exception {
    transportClient().prepareIndex(lookupIndex, "type", "1").setSource(jsonBuilder().startObject().array("followers", "foo", "bar", "baz").endObject()).get();
    transportClient().prepareIndex(queryIndex, "type", "1").setSource(jsonBuilder().startObject().field("username", "foo").endObject()).get();
    transportClient().admin().indices().prepareRefresh(queryIndex, lookupIndex).get();
    TermsLookup termsLookup = new TermsLookup(lookupIndex, "type", "1", "followers");
    TermsQueryBuilder termsLookupFilterBuilder = QueryBuilders.termsLookupQuery("username", termsLookup);
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchAllQuery()).must(termsLookupFilterBuilder);
    SearchResponse searchResponse = transportClient().prepareSearch(queryIndex).setQuery(queryBuilder).get();
    assertNoFailures(searchResponse);
    assertHitCount(searchResponse, 1);
    assertGetRequestsContainHeaders();
}
Also used : BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) TermsQueryBuilder(org.elasticsearch.index.query.TermsQueryBuilder) TermsLookup(org.elasticsearch.indices.TermsLookup) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class CreateIndexIT method testCreateAndDeleteIndexConcurrently.

public void testCreateAndDeleteIndexConcurrently() throws InterruptedException {
    createIndex("test");
    final AtomicInteger indexVersion = new AtomicInteger(0);
    final Object indexVersionLock = new Object();
    final CountDownLatch latch = new CountDownLatch(1);
    int numDocs = randomIntBetween(1, 10);
    for (int i = 0; i < numDocs; i++) {
        client().prepareIndex("test", "test").setSource("index_version", indexVersion.get()).get();
    }
    synchronized (indexVersionLock) {
        // not necessarily needed here but for completeness we lock here too
        indexVersion.incrementAndGet();
    }
    client().admin().indices().prepareDelete("test").execute(new // this happens async!!!
    ActionListener<DeleteIndexResponse>() {

        @Override
        public void onResponse(DeleteIndexResponse deleteIndexResponse) {
            Thread thread = new Thread() {

                @Override
                public void run() {
                    try {
                        // recreate that index
                        client().prepareIndex("test", "test").setSource("index_version", indexVersion.get()).get();
                        synchronized (indexVersionLock) {
                            // we sync here since we have to ensure that all indexing operations below for a given ID are done before
                            // we increment the index version otherwise a doc that is in-flight could make it into an index that it
                            // was supposed to be deleted for and our assertion fail...
                            indexVersion.incrementAndGet();
                        }
                        // from here on all docs with index_version == 0|1 must be gone!!!! only 2 are ok;
                        assertAcked(client().admin().indices().prepareDelete("test").get());
                    } finally {
                        latch.countDown();
                    }
                }
            };
            thread.start();
        }

        @Override
        public void onFailure(Exception e) {
            throw new RuntimeException(e);
        }
    });
    numDocs = randomIntBetween(100, 200);
    for (int i = 0; i < numDocs; i++) {
        try {
            synchronized (indexVersionLock) {
                client().prepareIndex("test", "test").setSource("index_version", indexVersion.get()).setTimeout(TimeValue.timeValueSeconds(10)).get();
            }
        } catch (IndexNotFoundException inf) {
        // fine
        } catch (UnavailableShardsException ex) {
            assertEquals(ex.getCause().getClass(), IndexNotFoundException.class);
        // fine we run into a delete index while retrying
        }
    }
    latch.await();
    refresh();
    // we only really assert that we never reuse segments of old indices or anything like this here and that nothing fails with
    // crazy exceptions
    SearchResponse expected = client().prepareSearch("test").setIndicesOptions(IndicesOptions.lenientExpandOpen()).setQuery(new RangeQueryBuilder("index_version").from(indexVersion.get(), true)).get();
    SearchResponse all = client().prepareSearch("test").setIndicesOptions(IndicesOptions.lenientExpandOpen()).get();
    assertEquals(expected + " vs. " + all, expected.getHits().getTotalHits(), all.getHits().getTotalHits());
    logger.info("total: {}", expected.getHits().getTotalHits());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) RangeQueryBuilder(org.elasticsearch.index.query.RangeQueryBuilder) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) SearchResponse(org.elasticsearch.action.search.SearchResponse) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) UnavailableShardsException(org.elasticsearch.action.UnavailableShardsException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException)

Example 4 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class RejectionActionIT method testSimulatedSearchRejectionLoad.

public void testSimulatedSearchRejectionLoad() throws Throwable {
    for (int i = 0; i < 10; i++) {
        client().prepareIndex("test", "type", Integer.toString(i)).setSource("field", "1").get();
    }
    int numberOfAsyncOps = randomIntBetween(200, 700);
    final CountDownLatch latch = new CountDownLatch(numberOfAsyncOps);
    final CopyOnWriteArrayList<Object> responses = new CopyOnWriteArrayList<>();
    for (int i = 0; i < numberOfAsyncOps; i++) {
        client().prepareSearch("test").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(QueryBuilders.matchQuery("field", "1")).execute(new ActionListener<SearchResponse>() {

            @Override
            public void onResponse(SearchResponse searchResponse) {
                responses.add(searchResponse);
                latch.countDown();
            }

            @Override
            public void onFailure(Exception e) {
                responses.add(e);
                latch.countDown();
            }
        });
    }
    latch.await();
    // validate all responses
    for (Object response : responses) {
        if (response instanceof SearchResponse) {
            SearchResponse searchResponse = (SearchResponse) response;
            for (ShardSearchFailure failure : searchResponse.getShardFailures()) {
                assertTrue("got unexpected reason..." + failure.reason(), failure.reason().toLowerCase(Locale.ENGLISH).contains("rejected"));
            }
        } else {
            Exception t = (Exception) response;
            Throwable unwrap = ExceptionsHelper.unwrapCause(t);
            if (unwrap instanceof SearchPhaseExecutionException) {
                SearchPhaseExecutionException e = (SearchPhaseExecutionException) unwrap;
                for (ShardSearchFailure failure : e.shardFailures()) {
                    assertTrue("got unexpected reason..." + failure.reason(), failure.reason().toLowerCase(Locale.ENGLISH).contains("rejected"));
                }
            } else if ((unwrap instanceof EsRejectedExecutionException) == false) {
                throw new AssertionError("unexpected failure", (Throwable) response);
            }
        }
    }
    assertThat(responses.size(), equalTo(numberOfAsyncOps));
}
Also used : SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) CountDownLatch(java.util.concurrent.CountDownLatch) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException) SearchResponse(org.elasticsearch.action.search.SearchResponse) ShardSearchFailure(org.elasticsearch.action.search.ShardSearchFailure) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) EsRejectedExecutionException(org.elasticsearch.common.util.concurrent.EsRejectedExecutionException)

Example 5 with SearchResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse in project elasticsearch by elastic.

the class IndexRecoveryIT method testDisconnectsWhileRecovering.

public void testDisconnectsWhileRecovering() throws Exception {
    final String indexName = "test";
    final Settings nodeSettings = Settings.builder().put(RecoverySettings.INDICES_RECOVERY_RETRY_DELAY_NETWORK_SETTING.getKey(), "100ms").put(RecoverySettings.INDICES_RECOVERY_INTERNAL_ACTION_TIMEOUT_SETTING.getKey(), "1s").put(MockFSDirectoryService.RANDOM_PREVENT_DOUBLE_WRITE_SETTING.getKey(), // restarted recoveries will delete temp files and write them again
    false).build();
    // start a master node
    internalCluster().startNode(nodeSettings);
    final String blueNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "blue").put(nodeSettings).build());
    final String redNodeName = internalCluster().startNode(Settings.builder().put("node.attr.color", "red").put(nodeSettings).build());
    ClusterHealthResponse response = client().admin().cluster().prepareHealth().setWaitForNodes(">=3").get();
    assertThat(response.isTimedOut(), is(false));
    client().admin().indices().prepareCreate(indexName).setSettings(Settings.builder().put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + "color", "blue").put(IndexMetaData.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 0)).get();
    List<IndexRequestBuilder> requests = new ArrayList<>();
    int numDocs = scaledRandomIntBetween(25, 250);
    for (int i = 0; i < numDocs; i++) {
        requests.add(client().prepareIndex(indexName, "type").setSource("{}", XContentType.JSON));
    }
    indexRandom(true, requests);
    ensureSearchable(indexName);
    ClusterStateResponse stateResponse = client().admin().cluster().prepareState().get();
    final String blueNodeId = internalCluster().getInstance(ClusterService.class, blueNodeName).localNode().getId();
    assertFalse(stateResponse.getState().getRoutingNodes().node(blueNodeId).isEmpty());
    SearchResponse searchResponse = client().prepareSearch(indexName).get();
    assertHitCount(searchResponse, numDocs);
    String[] recoveryActions = new String[] { PeerRecoverySourceService.Actions.START_RECOVERY, PeerRecoveryTargetService.Actions.FILES_INFO, PeerRecoveryTargetService.Actions.FILE_CHUNK, PeerRecoveryTargetService.Actions.CLEAN_FILES, //RecoveryTarget.Actions.TRANSLOG_OPS, <-- may not be sent if already flushed
    PeerRecoveryTargetService.Actions.PREPARE_TRANSLOG, PeerRecoveryTargetService.Actions.FINALIZE };
    final String recoveryActionToBlock = randomFrom(recoveryActions);
    final boolean dropRequests = randomBoolean();
    logger.info("--> will {} between blue & red on [{}]", dropRequests ? "drop requests" : "break connection", recoveryActionToBlock);
    MockTransportService blueMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, blueNodeName);
    MockTransportService redMockTransportService = (MockTransportService) internalCluster().getInstance(TransportService.class, redNodeName);
    TransportService redTransportService = internalCluster().getInstance(TransportService.class, redNodeName);
    TransportService blueTransportService = internalCluster().getInstance(TransportService.class, blueNodeName);
    final CountDownLatch requestBlocked = new CountDownLatch(1);
    blueMockTransportService.addDelegate(redTransportService, new RecoveryActionBlocker(dropRequests, recoveryActionToBlock, blueMockTransportService.original(), requestBlocked));
    redMockTransportService.addDelegate(blueTransportService, new RecoveryActionBlocker(dropRequests, recoveryActionToBlock, redMockTransportService.original(), requestBlocked));
    logger.info("--> starting recovery from blue to red");
    client().admin().indices().prepareUpdateSettings(indexName).setSettings(Settings.builder().put(IndexMetaData.INDEX_ROUTING_INCLUDE_GROUP_SETTING.getKey() + "color", "red,blue").put(IndexMetaData.SETTING_NUMBER_OF_REPLICAS, 1)).get();
    requestBlocked.await();
    logger.info("--> stopping to block recovery");
    blueMockTransportService.clearAllRules();
    redMockTransportService.clearAllRules();
    ensureGreen();
    searchResponse = client(redNodeName).prepareSearch(indexName).setPreference("_local").get();
    assertHitCount(searchResponse, numDocs);
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) MockTransportService(org.elasticsearch.test.transport.MockTransportService) ClusterStateResponse(org.elasticsearch.action.admin.cluster.state.ClusterStateResponse) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) SearchResponse(org.elasticsearch.action.search.SearchResponse) IndexRequestBuilder(org.elasticsearch.action.index.IndexRequestBuilder) MockTransportService(org.elasticsearch.test.transport.MockTransportService) TransportService(org.elasticsearch.transport.TransportService) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

SearchResponse (org.elasticsearch.action.search.SearchResponse)1720 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)976 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)275 SearchHit (org.elasticsearch.search.SearchHit)227 Script (org.elasticsearch.script.Script)225 ArrayList (java.util.ArrayList)217 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)210 Matchers.containsString (org.hamcrest.Matchers.containsString)156 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)143 HashMap (java.util.HashMap)139 Bucket (org.elasticsearch.search.aggregations.bucket.histogram.Histogram.Bucket)133 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)132 Bucket (org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket)95 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)95 SearchHits (org.elasticsearch.search.SearchHits)91 Sum (org.elasticsearch.search.aggregations.metrics.sum.Sum)84 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)82 Test (org.junit.Test)74 Map (java.util.Map)72 GeoPoint (org.elasticsearch.common.geo.GeoPoint)70