private void updateMappings() {
    for (String entity : mappingGenerators.keySet()) {
      EntityMappingGenerator generator = mappingGenerators.get(entity);
      Map<String, Object> mapping = generator.generateMapping();
      if (mapping != null) {
        final String type = generator.forClass().getSimpleName().toLowerCase();
        ListenableActionFuture<PutMappingResponse> future =
            client
                .admin()
                .indices()
                .preparePutMapping("entities")
                .setType(type)
                .setSource(mapping)
                .execute();

        future.addListener(
            new ActionListener<PutMappingResponse>() {
              public void onResponse(PutMappingResponse putMappingResponse) {
                logger.info("Mapping for entity [{}] updated", type);
              }

              public void onFailure(Throwable throwable) {
                logger.error("Error updating mapping for entity {}", type, throwable);
              }
            });

        future.actionGet();
      }
    }
  }
Exemple #2
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;
  }