简体   繁体   English

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

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

I am following this Elastic Search documentation to create a mapping for my Elastic Search index named "contacts".我正在按照这个 Elastic Search 文档为我的 Elastic Search 索引创建一个名为“contacts”的映射。

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

Running my code results in运行我的代码结果

failure to create mapping: Validation Failed: 1: mapping type is missing;无法创建映射:验证失败:1:缺少映射类型;

Here is my code.这是我的代码。

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();
}

This is odd because I am following the documentation's example.这很奇怪,因为我正在关注文档的示例。 I am using version 7.6.0 of the Java client.我使用的是 Java 客户端的 7.6.0 版。


Update.更新。 I downgraded to version 7.5.2 of the Java client because this is the version of my Elastic Search deployment.我将 Java 客户端降级到 7.5.2 版,因为这是我的 Elastic Search 部署版本。 The put mapping command now works. put mapping 命令现在可以工作了。 However, I cannot get the asynchronous call to work.但是,我无法使异步调用正常工作。 If I uncomment that call, Eclipse tells me that this function is deprecated and that I should use the new one.如果我取消对该调用的注释,Eclipse 会告诉我该函数已被弃用,我应该使用新函数。 However, the new method looks identical to the deprecated method (same parameters, same name).但是,新方法看起来与不推荐使用的方法相同(相同的参数,相同的名称)。 What's the difference?有什么不同? And how do I tell Eclipse to use the new version?我如何告诉 Eclipse 使用新版本?

Deprecated.已弃用。 This method uses an old request object which still refers to types, a deprecated feature.此方法使用旧的请求对象,该对象仍然引用类型,这是一个已弃用的功能。 The method putMappingAsync(PutMappingRequest, RequestOptions, ActionListener) should be used instead,which accepts a new request object.应该使用 putMappingAsync(PutMappingRequest, RequestOptions, ActionListener) 方法来代替,它接受一个新的请求对象。

Only the synchronous one.只有同步的。

    @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);

// here is the asynchronous call that does not work. // 这里是不起作用的异步调用。 // client.indices().putMappingAsync(request, RequestOptions.DEFAULT, listener); // client.indices().putMappingAsync(request, RequestOptions.DEFAULT, listener);

    client.close();
}

I think you imported the wrong class.我认为你导入了错误的类。 There are two classes "PutMappingRequest" located in different packages:有两个类“PutMappingRequest”位于不同的包中:

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

You should use the 2nd one to avoid the exception.您应该使用第二个来避免异常。 It is provided by the Java High Level REST Client.它由 Java 高级 REST 客户端提供。 To fix the exception "mapping type is missing" , you just need to change your import statement and Eclipse will compile with the correct class.要修复异常“缺少映射类型” ,您只需更改导入语句,Eclipse 将使用正确的类进行编译。 You don't need to downgrade your Elasticsearch version.您无需降级 Elasticsearch 版本。

If you check the Java client source code, you will see two methods putMapping(...) defined with different input parameters.如果您查看 Java 客户端源代码,您将看到使用不同输入参数定义的两个方法putMapping(...) They are overloaded methods.它们是重载方法。 Only one is deprecated:只有一个被弃用:

/**
 * 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 {
    ...
}

Not sure I understood what you mean by the asynchronous call does not work.不确定我是否理解异步调用不起作用的意思。 Here's an example of usage:下面是一个使用示例:

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
          }
        });

See also:也可以看看:

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

相关问题 Spring 数据弹性搜索与 Java 高级 REST 客户端 - Spring Data Elastic Search vs Java High Level REST Client 为什么在弹性搜索中引入 Java 高级 REST 客户端? - Why Java High Level REST Client got introduced in Elastic search? 为什么 filterQuery 在用于 JAVA 的 Elastic Search 的高级 REST 客户端中不起作用? - Why does filterQuery not work in Elastic Search's high level REST client for JAVA? 弹性搜索 rest 高级客户端出现以下错误 - Getting below error in elastic search rest high level client 弹性搜索:将客户端传输到高级其余客户端 - Elastic Search: Transport Client to High Level Rest Client 如何通过 Java 高级 rest 客户端在 Elastic Search 中使用多个字段进行搜索 - How to search using multiple fields in Elastic Search through Java high level rest client Elastic Search 7 高级客户端使用映射创建索引 - Elastic Search 7 High level Client Create Index with mapping 如何使用 JAVA 高级 REST 客户端创建弹性搜索索引? - How can I create an elastic search index with the JAVA high level REST client? 如何通过 Java 高级 Rest 客户端访问安全弹性搜索 - How to hit Secure Elastic Search through Java High Level Rest Client 如何使用 Elastic 的 High Level Rest Client 获取所有索引? - How to get all indices with Elastic's High Level Rest Client?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM