Search in sources :

Example 71 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class SimpleRoutingIT method testRequiredRoutingCrudApis.

public void testRequiredRoutingCrudApis() throws Exception {
    client().admin().indices().prepareCreate("test").addAlias(new Alias("alias")).addMapping("type1", XContentFactory.jsonBuilder().startObject().startObject("type1").startObject("_routing").field("required", true).endObject().endObject().endObject()).execute().actionGet();
    ensureGreen();
    logger.info("--> indexing with id [1], and routing [0]");
    client().prepareIndex(indexOrAlias(), "type1", "1").setRouting("0").setSource("field", "value1").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    logger.info("--> verifying get with no routing, should fail");
    logger.info("--> indexing with id [1], with no routing, should fail");
    try {
        client().prepareIndex(indexOrAlias(), "type1", "1").setSource("field", "value1").get();
        fail("index with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    logger.info("--> verifying get with routing, should find");
    for (int i = 0; i < 5; i++) {
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
    }
    logger.info("--> deleting with no routing, should fail");
    try {
        client().prepareDelete(indexOrAlias(), "type1", "1").get();
        fail("delete with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail("get with missing routing when routing is required should fail");
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(true));
    }
    try {
        client().prepareUpdate(indexOrAlias(), "type1", "1").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").execute().actionGet();
        fail("update with missing routing when routing is required should fail");
    } catch (ElasticsearchException e) {
        assertThat(e.unwrapCause(), instanceOf(RoutingMissingException.class));
    }
    client().prepareUpdate(indexOrAlias(), "type1", "1").setRouting("0").setDoc(Requests.INDEX_CONTENT_TYPE, "field", "value2").get();
    client().admin().indices().prepareRefresh().execute().actionGet();
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail();
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        GetResponse getResponse = client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet();
        assertThat(getResponse.isExists(), equalTo(true));
        assertThat(getResponse.getSourceAsMap().get("field"), equalTo("value2"));
    }
    client().prepareDelete(indexOrAlias(), "type1", "1").setRouting("0").setRefreshPolicy(RefreshPolicy.IMMEDIATE).get();
    for (int i = 0; i < 5; i++) {
        try {
            client().prepareGet(indexOrAlias(), "type1", "1").execute().actionGet().isExists();
            fail();
        } catch (RoutingMissingException e) {
            assertThat(e.status(), equalTo(RestStatus.BAD_REQUEST));
            assertThat(e.getMessage(), equalTo("routing is required for [test]/[type1]/[1]"));
        }
        assertThat(client().prepareGet(indexOrAlias(), "type1", "1").setRouting("0").execute().actionGet().isExists(), equalTo(false));
    }
}
Also used : Alias(org.elasticsearch.action.admin.indices.alias.Alias) ElasticsearchException(org.elasticsearch.ElasticsearchException) GetResponse(org.elasticsearch.action.get.GetResponse) MultiGetResponse(org.elasticsearch.action.get.MultiGetResponse) RoutingMissingException(org.elasticsearch.action.RoutingMissingException)

Example 72 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class DoubleTermsIT method testSingleValuedFieldOrderedByNonMetricsOrMultiBucketSubAggregation.

public void testSingleValuedFieldOrderedByNonMetricsOrMultiBucketSubAggregation() throws Exception {
    for (String index : Arrays.asList("idx", "idx_unmapped")) {
        try {
            client().prepareSearch(index).setTypes("type").addAggregation(terms("terms").field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("num_tags", true)).subAggregation(terms("num_tags").field("num_tags").collectMode(randomFrom(SubAggCollectionMode.values())))).execute().actionGet();
            fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation which is not of a metrics type");
        } catch (ElasticsearchException e) {
        // expected
        }
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 73 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class StringTermsIT method testSingleValuedFieldOrderedByMissingSubAggregation.

public void testSingleValuedFieldOrderedByMissingSubAggregation() throws Exception {
    for (String index : Arrays.asList("idx", "idx_unmapped")) {
        try {
            client().prepareSearch(index).setTypes("type").addAggregation(terms("terms").executionHint(randomExecutionHint()).field(SINGLE_VALUED_FIELD_NAME).collectMode(randomFrom(SubAggCollectionMode.values())).order(Terms.Order.aggregation("avg_i", true))).execute().actionGet();
            fail("Expected search to fail when trying to sort terms aggregation by sug-aggregation that doesn't exist");
        } catch (ElasticsearchException e) {
        // expected
        }
    }
}
Also used : Matchers.containsString(org.hamcrest.Matchers.containsString) ElasticsearchException(org.elasticsearch.ElasticsearchException)

Example 74 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project elasticsearch by elastic.

the class SearchWithRandomIOExceptionsIT method testRandomDirectoryIOExceptions.

public void testRandomDirectoryIOExceptions() throws IOException, InterruptedException, ExecutionException {
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("test").field("type", "keyword").endObject().endObject().endObject().endObject().string();
    final double exceptionRate;
    final double exceptionOnOpenRate;
    if (frequently()) {
        if (randomBoolean()) {
            if (randomBoolean()) {
                exceptionOnOpenRate = 1.0 / between(5, 100);
                exceptionRate = 0.0d;
            } else {
                exceptionRate = 1.0 / between(5, 100);
                exceptionOnOpenRate = 0.0d;
            }
        } else {
            exceptionOnOpenRate = 1.0 / between(5, 100);
            exceptionRate = 1.0 / between(5, 100);
        }
    } else {
        // rarely no exception
        exceptionRate = 0d;
        exceptionOnOpenRate = 0d;
    }
    final boolean createIndexWithoutErrors = randomBoolean();
    int numInitialDocs = 0;
    if (createIndexWithoutErrors) {
        Settings.Builder settings = Settings.builder().put("index.number_of_replicas", numberOfReplicas());
        logger.info("creating index: [test] using settings: [{}]", settings.build().getAsMap());
        client().admin().indices().prepareCreate("test").setSettings(settings).addMapping("type", mapping, XContentType.JSON).execute().actionGet();
        numInitialDocs = between(10, 100);
        ensureGreen();
        for (int i = 0; i < numInitialDocs; i++) {
            client().prepareIndex("test", "type", "init" + i).setSource("test", "init").get();
        }
        client().admin().indices().prepareRefresh("test").execute().get();
        client().admin().indices().prepareFlush("test").execute().get();
        client().admin().indices().prepareClose("test").execute().get();
        client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(MockFSDirectoryService.RANDOM_IO_EXCEPTION_RATE_SETTING.getKey(), exceptionRate).put(MockFSDirectoryService.RANDOM_IO_EXCEPTION_RATE_ON_OPEN_SETTING.getKey(), exceptionOnOpenRate));
        client().admin().indices().prepareOpen("test").execute().get();
    } else {
        Settings.Builder settings = Settings.builder().put("index.number_of_replicas", randomIntBetween(0, 1)).put(MockFSIndexStore.INDEX_CHECK_INDEX_ON_CLOSE_SETTING.getKey(), false).put(MockFSDirectoryService.RANDOM_IO_EXCEPTION_RATE_SETTING.getKey(), exceptionRate).put(MockFSDirectoryService.RANDOM_IO_EXCEPTION_RATE_ON_OPEN_SETTING.getKey(), // we cannot expect that the index will be valid
        exceptionOnOpenRate);
        logger.info("creating index: [test] using settings: [{}]", settings.build().getAsMap());
        client().admin().indices().prepareCreate("test").setSettings(settings).addMapping("type", mapping, XContentType.JSON).execute().actionGet();
    }
    ClusterHealthResponse clusterHealthResponse = client().admin().cluster().health(Requests.clusterHealthRequest().waitForYellowStatus().timeout(TimeValue.timeValueSeconds(5))).get();
    final int numDocs;
    final boolean expectAllShardsFailed;
    if (clusterHealthResponse.isTimedOut()) {
        /* some seeds just won't let you create the index at all and we enter a ping-pong mode
             * trying one node after another etc. that is ok but we need to make sure we don't wait
             * forever when indexing documents so we set numDocs = 1 and expecte all shards to fail
             * when we search below.*/
        logger.info("ClusterHealth timed out - only index one doc and expect searches to fail");
        numDocs = 1;
        expectAllShardsFailed = true;
    } else {
        numDocs = between(10, 100);
        expectAllShardsFailed = false;
    }
    int numCreated = 0;
    boolean[] added = new boolean[numDocs];
    for (int i = 0; i < numDocs; i++) {
        added[i] = false;
        try {
            IndexResponse indexResponse = client().prepareIndex("test", "type", Integer.toString(i)).setTimeout(TimeValue.timeValueSeconds(1)).setSource("test", English.intToEnglish(i)).get();
            if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) {
                numCreated++;
                added[i] = true;
            }
        } catch (ElasticsearchException ex) {
        }
    }
    ESIntegTestCase.NumShards numShards = getNumShards("test");
    logger.info("Start Refresh");
    // don't assert on failures here
    final RefreshResponse refreshResponse = client().admin().indices().prepareRefresh("test").execute().get();
    final boolean refreshFailed = refreshResponse.getShardFailures().length != 0 || refreshResponse.getFailedShards() != 0;
    logger.info("Refresh failed [{}] numShardsFailed: [{}], shardFailuresLength: [{}], successfulShards: [{}], totalShards: [{}] ", refreshFailed, refreshResponse.getFailedShards(), refreshResponse.getShardFailures().length, refreshResponse.getSuccessfulShards(), refreshResponse.getTotalShards());
    final int numSearches = scaledRandomIntBetween(10, 20);
    // we don't check anything here really just making sure we don't leave any open files or a broken index behind.
    for (int i = 0; i < numSearches; i++) {
        try {
            int docToQuery = between(0, numDocs - 1);
            int expectedResults = added[docToQuery] ? 1 : 0;
            logger.info("Searching for [test:{}]", English.intToEnglish(docToQuery));
            SearchResponse searchResponse = client().prepareSearch().setTypes("type").setQuery(QueryBuilders.matchQuery("test", English.intToEnglish(docToQuery))).setSize(expectedResults).get();
            logger.info("Successful shards: [{}]  numShards: [{}]", searchResponse.getSuccessfulShards(), numShards.numPrimaries);
            if (searchResponse.getSuccessfulShards() == numShards.numPrimaries && !refreshFailed) {
                assertResultsAndLogOnFailure(expectedResults, searchResponse);
            }
            // check match all
            searchResponse = client().prepareSearch().setTypes("type").setQuery(QueryBuilders.matchAllQuery()).setSize(numCreated + numInitialDocs).addSort("_uid", SortOrder.ASC).get();
            logger.info("Match all Successful shards: [{}]  numShards: [{}]", searchResponse.getSuccessfulShards(), numShards.numPrimaries);
            if (searchResponse.getSuccessfulShards() == numShards.numPrimaries && !refreshFailed) {
                assertResultsAndLogOnFailure(numCreated + numInitialDocs, searchResponse);
            }
        } catch (SearchPhaseExecutionException ex) {
            logger.info("SearchPhaseException: [{}]", ex.getMessage());
            // if a scheduled refresh or flush fails all shards we see all shards failed here
            if (!(expectAllShardsFailed || refreshResponse.getSuccessfulShards() == 0 || ex.getMessage().contains("all shards failed"))) {
                throw ex;
            }
        }
    }
    if (createIndexWithoutErrors) {
        // check the index still contains the records that we indexed without errors
        client().admin().indices().prepareClose("test").execute().get();
        client().admin().indices().prepareUpdateSettings("test").setSettings(Settings.builder().put(MockFSDirectoryService.RANDOM_IO_EXCEPTION_RATE_SETTING.getKey(), 0).put(MockFSDirectoryService.RANDOM_IO_EXCEPTION_RATE_ON_OPEN_SETTING.getKey(), 0));
        client().admin().indices().prepareOpen("test").execute().get();
        ensureGreen();
        SearchResponse searchResponse = client().prepareSearch().setTypes("type").setQuery(QueryBuilders.matchQuery("test", "init")).get();
        assertNoFailures(searchResponse);
        assertHitCount(searchResponse, numInitialDocs);
    }
}
Also used : ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) SearchPhaseExecutionException(org.elasticsearch.action.search.SearchPhaseExecutionException) ElasticsearchException(org.elasticsearch.ElasticsearchException) SearchResponse(org.elasticsearch.action.search.SearchResponse) RefreshResponse(org.elasticsearch.action.admin.indices.refresh.RefreshResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) ESIntegTestCase(org.elasticsearch.test.ESIntegTestCase) Settings(org.elasticsearch.common.settings.Settings)

Example 75 with ElasticsearchException

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.ElasticsearchException in project graylog2-server by Graylog2.

the class Indices method indexCreationDate.

@Nullable
public DateTime indexCreationDate(String index) {
    final GetIndexRequest indexRequest = c.admin().indices().prepareGetIndex().addFeatures(GetIndexRequest.Feature.SETTINGS).addIndices(index).request();
    try {
        final GetIndexResponse response = c.admin().indices().getIndex(indexRequest).actionGet();
        final Settings settings = response.settings().get(index);
        if (settings == null) {
            return null;
        }
        return new DateTime(settings.getAsLong("index.creation_date", 0L), DateTimeZone.UTC);
    } catch (ElasticsearchException e) {
        LOG.warn("Unable to read creation_date for index " + index, e.getRootCause());
        return null;
    }
}
Also used : GetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) ElasticsearchException(org.elasticsearch.ElasticsearchException) Settings(org.elasticsearch.common.settings.Settings) DateTime(org.joda.time.DateTime) Nullable(javax.annotation.Nullable)

Aggregations

ElasticsearchException (org.elasticsearch.ElasticsearchException)310 IOException (java.io.IOException)128 Settings (org.elasticsearch.common.settings.Settings)32 HashMap (java.util.HashMap)31 ArrayList (java.util.ArrayList)30 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)30 ClusterState (org.elasticsearch.cluster.ClusterState)29 Matchers.containsString (org.hamcrest.Matchers.containsString)25 List (java.util.List)23 Map (java.util.Map)22 AtomicReference (java.util.concurrent.atomic.AtomicReference)20 ParameterizedMessage (org.apache.logging.log4j.message.ParameterizedMessage)18 XContentParser (org.elasticsearch.common.xcontent.XContentParser)17 Path (java.nio.file.Path)16 Test (org.junit.Test)16 ActionListener (org.elasticsearch.action.ActionListener)15 Collections (java.util.Collections)14 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)14 Version (org.elasticsearch.Version)14 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)13