Search in sources :

Example 1 with InvalidIndexNameException

use of org.elasticsearch.indices.InvalidIndexNameException in project api-core by ca-cwds.

the class ElasticsearchDao method createIndex.

/**
 * Create an index and apply a mapping before blasting documents into it.
 *
 * @param index index name or alias
 * @param numShards number of shards
 * @param numReplicas number of replicas
 * @throws IOException on disconnect, hang, etc.
 * @deprecated call {@link #createIndexIfNeeded(String)} instead
 */
@Deprecated
private void createIndex(final String index, int numShards, int numReplicas) throws IOException {
    LOGGER.warn("CREATE ES INDEX {} with {} shards and {} replicas", index, numShards, numReplicas);
    try {
        final Settings indexSettings = Settings.builder().put("number_of_shards", numShards).put("number_of_replicas", numReplicas).build();
        CreateIndexRequest indexRequest = new CreateIndexRequest(index, indexSettings);
        getClient().admin().indices().create(indexRequest).actionGet();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        IOUtils.copy(this.getClass().getResourceAsStream(DEFAULT_PERSON_MAPPING), out);
        out.flush();
        final String mapping = out.toString();
        getClient().admin().indices().preparePutMapping(index).setType(getDefaultDocType()).setSource(mapping).get();
    } catch (InvalidIndexNameException e) {
        // NOSONAR
        LOGGER.warn("INDEX OR ALIAS ALREADY EXISTS! index/alias name={}, msg: {}", index, e.getDetailedMessage());
    // It's ok. Do not rethrow. This means that the "index" name is really an alias to
    // an existing index.
    }
}
Also used : InvalidIndexNameException(org.elasticsearch.indices.InvalidIndexNameException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) Settings(org.elasticsearch.common.settings.Settings)

Example 2 with InvalidIndexNameException

use of org.elasticsearch.indices.InvalidIndexNameException in project elasticsearch by elastic.

the class MetaDataCreateIndexServiceTests method validateIndexName.

private void validateIndexName(String indexName, String errorMessage) {
    InvalidIndexNameException e = expectThrows(InvalidIndexNameException.class, () -> MetaDataCreateIndexService.validateIndexName(indexName, ClusterState.builder(ClusterName.CLUSTER_NAME_SETTING.getDefault(Settings.EMPTY)).build()));
    assertThat(e.getMessage(), endsWith(errorMessage));
}
Also used : InvalidIndexNameException(org.elasticsearch.indices.InvalidIndexNameException)

Example 3 with InvalidIndexNameException

use of org.elasticsearch.indices.InvalidIndexNameException in project elasticsearch by elastic.

the class SharedClusterSnapshotRestoreIT method testRenameOnRestore.

public void testRenameOnRestore() throws Exception {
    Client client = client();
    logger.info("-->  creating repository");
    assertAcked(client.admin().cluster().preparePutRepository("test-repo").setType("fs").setSettings(Settings.builder().put("location", randomRepoPath())));
    createIndex("test-idx-1", "test-idx-2", "test-idx-3");
    ensureGreen();
    assertAcked(client.admin().indices().prepareAliases().addAlias("test-idx-1", "alias-1").addAlias("test-idx-2", "alias-2").addAlias("test-idx-3", "alias-3"));
    logger.info("--> indexing some data");
    for (int i = 0; i < 100; i++) {
        index("test-idx-1", "doc", Integer.toString(i), "foo", "bar" + i);
        index("test-idx-2", "doc", Integer.toString(i), "foo", "bar" + i);
    }
    refresh();
    assertThat(client.prepareSearch("test-idx-1").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    assertThat(client.prepareSearch("test-idx-2").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    logger.info("--> snapshot");
    CreateSnapshotResponse createSnapshotResponse = client.admin().cluster().prepareCreateSnapshot("test-repo", "test-snap").setWaitForCompletion(true).setIndices("test-idx-1", "test-idx-2").get();
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), greaterThan(0));
    assertThat(createSnapshotResponse.getSnapshotInfo().successfulShards(), equalTo(createSnapshotResponse.getSnapshotInfo().totalShards()));
    logger.info("--> restore indices with different names");
    RestoreSnapshotResponse restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+)").setRenameReplacement("$1-copy").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(client.prepareSearch("test-idx-1-copy").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    assertThat(client.prepareSearch("test-idx-2-copy").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    logger.info("--> close just restored indices");
    client.admin().indices().prepareClose("test-idx-1-copy", "test-idx-2-copy").get();
    logger.info("--> and try to restore these indices again");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+)").setRenameReplacement("$1-copy").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    assertThat(client.prepareSearch("test-idx-1-copy").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    assertThat(client.prepareSearch("test-idx-2-copy").setSize(0).get().getHits().getTotalHits(), equalTo(100L));
    logger.info("--> close indices");
    assertAcked(client.admin().indices().prepareClose("test-idx-1", "test-idx-2-copy"));
    logger.info("--> restore indices with different names");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+-2)").setRenameReplacement("$1-copy").setWaitForCompletion(true).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
    logger.info("--> delete indices");
    cluster().wipeIndices("test-idx-1", "test-idx-1-copy", "test-idx-2", "test-idx-2-copy");
    logger.info("--> try renaming indices using the same name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("(.+)").setRenameReplacement("same-name").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices using the same name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setRenamePattern("test-idx-2").setRenameReplacement("test-idx-1").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices using invalid index name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern(".+").setRenameReplacement("__WRONG__").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (InvalidIndexNameException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias name");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern(".+").setRenameReplacement("alias-3").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (InvalidIndexNameException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias of itself");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern("test-idx").setRenameReplacement("alias").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias of another restored index");
    try {
        client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1", "test-idx-2").setRenamePattern("test-idx-1").setRenameReplacement("alias-2").setWaitForCompletion(true).execute().actionGet();
        fail("Shouldn't be here");
    } catch (SnapshotRestoreException ex) {
    // Expected
    }
    logger.info("--> try renaming indices into existing alias of itself, but don't restore aliases ");
    restoreSnapshotResponse = client.admin().cluster().prepareRestoreSnapshot("test-repo", "test-snap").setIndices("test-idx-1").setRenamePattern("test-idx").setRenameReplacement("alias").setWaitForCompletion(true).setIncludeAliases(false).execute().actionGet();
    assertThat(restoreSnapshotResponse.getRestoreInfo().totalShards(), greaterThan(0));
}
Also used : CreateSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse) InvalidIndexNameException(org.elasticsearch.indices.InvalidIndexNameException) Client(org.elasticsearch.client.Client) RestoreSnapshotResponse(org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)

Aggregations

InvalidIndexNameException (org.elasticsearch.indices.InvalidIndexNameException)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 CreateSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse)1 RestoreSnapshotResponse (org.elasticsearch.action.admin.cluster.snapshots.restore.RestoreSnapshotResponse)1 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)1 Client (org.elasticsearch.client.Client)1 Settings (org.elasticsearch.common.settings.Settings)1