繁体   English   中英

将映射与 Elastic Search 的高级 REST JAVA 客户端异步放置 - 不推荐使用的错误

[英]Put mapping with Elastic Search's High level REST JAVA client asynchronously - deprecated error

我正在按照这个 Elastic Search 文档为我的 Elastic Search 索引创建一个名为“contacts”的映射。

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-put-mapping.html

运行我的代码结果

无法创建映射:验证失败:1:缺少映射类型;

这是我的代码。

public void createElasticMapping() throws IOException {
    RestHighLevelClient client = createHighLevelRestClient();

    PutMappingRequest request = new PutMappingRequest("contacts");

    ArrayList<Field> fields = new ArrayList<Field>();
    fields.add(new Field("list_id", "integer"));
    fields.add(new Field("contact_id", "integer"));

    Map<String, Object> properties = new HashMap<>();

    for (Field fieldToAdd : fields) {
        Map<String, Object> fieldData = new HashMap<>();
        fieldData.put("type", fieldToAdd.type);
        properties.put(fieldToAdd.name, fieldData);
    }

    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("properties", properties);
    request.source(jsonMap);

    @SuppressWarnings("deprecation")
    org.elasticsearch.action.support.master.AcknowledgedResponse putMappingResponse = client.indices()
            .putMapping(request, RequestOptions.DEFAULT);
    System.out.print(putMappingResponse);

    client.close();
}

这很奇怪,因为我正在关注文档的示例。 我使用的是 Java 客户端的 7.6.0 版。


更新。 我将 Java 客户端降级到 7.5.2 版,因为这是我的 Elastic Search 部署版本。 put mapping 命令现在可以工作了。 但是,我无法使异步调用正常工作。 如果我取消对该调用的注释,Eclipse 会告诉我该函数已被弃用,我应该使用新函数。 但是,新方法看起来与不推荐使用的方法相同(相同的参数,相同的名称)。 有什么不同? 我如何告诉 Eclipse 使用新版本?

已弃用。 此方法使用旧的请求对象,该对象仍然引用类型,这是一个已弃用的功能。 应该使用 putMappingAsync(PutMappingRequest, RequestOptions, ActionListener) 方法来代替,它接受一个新的请求对象。

只有同步的。

    @POST
@Path("/mapping")
public void createElasticMapping() throws IOException {
    RestHighLevelClient client = createHighLevelRestClient();

    PutMappingRequest request = new PutMappingRequest("contacts");

    ArrayList<Field> fields = new ArrayList<Field>();
    fields.add(new Field("list_id", "integer"));
    fields.add(new Field("contact_id", "integer"));

    Map<String, Object> properties = new HashMap<>();

    for (Field fieldToAdd : fields) {
        Map<String, Object> fieldData = new HashMap<>();
        fieldData.put("type", fieldToAdd.type);
        properties.put(fieldToAdd.name, fieldData);
    }

    Map<String, Object> jsonMap = new HashMap<>();
    jsonMap.put("properties", properties);
    request.source(jsonMap);

    org.elasticsearch.action.support.master.AcknowledgedResponse putMappingResponse = client.indices()
            .putMapping(request, RequestOptions.DEFAULT);
    System.out.print(putMappingResponse);

// 这里是不起作用的异步调用。 // client.indices().putMappingAsync(request, RequestOptions.DEFAULT, listener);

    client.close();
}

我认为你导入了错误的类。 有两个类“PutMappingRequest”位于不同的包中:

  1. org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest
  2. org.elasticsearch.client.indices.PutMappingRequest

您应该使用第二个来避免异常。 它由 Java 高级 REST 客户端提供。 要修复异常“缺少映射类型” ,您只需更改导入语句,Eclipse 将使用正确的类进行编译。 您无需降级 Elasticsearch 版本。

如果您查看 Java 客户端源代码,您将看到使用不同输入参数定义的两个方法putMapping(...) 它们是重载方法。 只有一个被弃用:

/**
 * Updates the mappings on an index using the Put Mapping API.
 * ...
 *
 * @deprecated This method uses an old request object which still refers to types, a deprecated feature. The method
 * {@link #putMapping(PutMappingRequest, RequestOptions)} should be used instead, which accepts a new request object.
 */
@Deprecated
public AcknowledgedResponse putMapping(
    org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest putMappingRequest,
    RequestOptions options) throws IOException {
    ...
}

public AcknowledgedResponse putMapping(
    PutMappingRequest putMappingRequest,
    RequestOptions options) throws IOException {
    ...
}

不确定我是否理解异步调用不起作用的意思。 下面是一个使用示例:

client
    .indices()
    .putMappingAsync(
        request,
        RequestOptions.DEFAULT,
        new ActionListener<>() {
          @Override
          public void onResponse(AcknowledgedResponse r) {
            // TODO handle response here
          }

          @Override
          public void onFailure(Exception e) {
            // TODO handle failure here
          }
        });

也可以看看:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM