public void testAliasEmptyName() throws Exception {
    PutIndexTemplateRequestBuilder putIndexTemplateRequestBuilder =
        client()
            .admin()
            .indices()
            .preparePutTemplate("template_1")
            .setTemplate("te*")
            .addAlias(new Alias("  ").indexRouting("1,2,3"));

    IllegalArgumentException e =
        expectThrows(IllegalArgumentException.class, () -> putIndexTemplateRequestBuilder.get());
    assertThat(e.getMessage(), equalTo("alias name is required"));
  }
  public void testAliasWithMultipleIndexRoutings() throws Exception {
    PutIndexTemplateRequestBuilder putIndexTemplateRequestBuilder =
        client()
            .admin()
            .indices()
            .preparePutTemplate("template_1")
            .setTemplate("te*")
            .addAlias(new Alias("alias").indexRouting("1,2,3"));

    IllegalArgumentException e =
        expectThrows(IllegalArgumentException.class, () -> putIndexTemplateRequestBuilder.get());
    assertThat(
        e.getMessage(),
        equalTo("alias [alias] has several index routing values associated with it"));
  }
  public void testAliasEmptyName() throws Exception {
    PutIndexTemplateRequestBuilder putIndexTemplateRequestBuilder =
        client()
            .admin()
            .indices()
            .preparePutTemplate("template_1")
            .setTemplate("te*")
            .addAlias(new Alias("  ").indexRouting("1,2,3"));

    try {
      putIndexTemplateRequestBuilder.get();
      fail("put template should have failed due to alias with empty name");
    } catch (IllegalArgumentException e) {
      assertThat(e.getMessage(), equalTo("alias name is required"));
    }
  }
  public void testAliasWithMultipleIndexRoutings() throws Exception {
    PutIndexTemplateRequestBuilder putIndexTemplateRequestBuilder =
        client()
            .admin()
            .indices()
            .preparePutTemplate("template_1")
            .setTemplate("te*")
            .addAlias(new Alias("alias").indexRouting("1,2,3"));

    try {
      putIndexTemplateRequestBuilder.get();
      fail("put template should have failed due to alias with multiple index routings");
    } catch (IllegalArgumentException e) {
      assertThat(
          e.getMessage(),
          equalTo("alias [alias] has several index routing values associated with it"));
    }
  }
  public void testAliasInvalidFilterInvalidJson() throws Exception {
    // invalid json: put index template fails
    PutIndexTemplateRequestBuilder putIndexTemplateRequestBuilder =
        client()
            .admin()
            .indices()
            .preparePutTemplate("template_1")
            .setTemplate("te*")
            .addAlias(new Alias("invalid_alias").filter("abcde"));

    IllegalArgumentException e =
        expectThrows(IllegalArgumentException.class, () -> putIndexTemplateRequestBuilder.get());
    assertThat(e.getMessage(), equalTo("failed to parse filter for alias [invalid_alias]"));

    GetIndexTemplatesResponse response =
        client().admin().indices().prepareGetTemplates("template_1").get();
    assertThat(response.getIndexTemplates().size(), equalTo(0));
  }
Example #6
0
  public AsyncFuture<Void> configure() {
    final IndicesAdminClient indices = client.admin().indices();

    log.info("[{}] updating template for {}", templateName, index.template());

    final PutIndexTemplateRequestBuilder put = indices.preparePutTemplate(templateName);

    put.setSettings(settings);
    put.setTemplate(index.template());

    for (final Map.Entry<String, Map<String, Object>> mapping : mappings.entrySet()) {
      put.addMapping(mapping.getKey(), mapping.getValue());
    }

    final ResolvableFuture<Void> future = async.future();

    final ListenableActionFuture<PutIndexTemplateResponse> target = put.execute();

    target.addListener(
        new ActionListener<PutIndexTemplateResponse>() {
          @Override
          public void onResponse(final PutIndexTemplateResponse response) {
            if (!response.isAcknowledged()) {
              future.fail(new Exception("request not acknowledged"));
              return;
            }

            future.resolve(null);
          }

          @Override
          public void onFailure(Throwable e) {
            future.fail(e);
          }
        });

    future.onCancelled(() -> target.cancel(false));
    return future;
  }