簡體   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