簡體   English   中英

用於 JAVA 的 createIndex elasticsearch 高級 rest 客戶端使設置不正確

[英]createIndex elasticsearch high level rest client for JAVA makes settings incorrectly

我正在使用elasticsearch 文檔中的代碼來創建索引。 我應該能夠將 Postman 中的索引配置 object 粘貼到我的 Java 代碼中。

request.source("{\n" +
        "    \"settings\" : {\n" +
        "        \"number_of_shards\" : 1,\n" +
        "        \"number_of_replicas\" : 0\n" +
        "    },\n" +
        "    \"mappings\" : {\n" +
        "        \"properties\" : {\n" +
        "            \"message\" : { \"type\" : \"text\" }\n" +
        "        }\n" +
        "    },\n" +
        "    \"aliases\" : {\n" +
        "        \"twitter_alias\" : {}\n" +
        "    }\n" +
        "}", XContentType.JSON); 

當我執行 GET /index_name 時,我看到一個包含兩個映射部分的奇怪結構的索引。 為什么是這樣? 我希望有一個映射和一個設置部分。

{
    "contacts_4_3t88f9nabk": {
        "aliases": {},
        "mappings": {
            "properties": {
                "aliases": {
                    "properties": {
                        "twitter_alias": {
                            "type": "object"
                        }
                    }
                },
                "mappings": {
                    "properties": {
                        "properties": {
                            "properties": {
                                "message": {
                                    "properties": {
                                        "type": {
                                            "type": "text",
                                            "fields": {
                                                "keyword": {
                                                    "type": "keyword",
                                                    "ignore_above": 256
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                },
                "settings": {
                    "properties": {
                        "number_of_replicas": {
                            "type": "long"
                        },
                        "number_of_shards": {
                            "type": "long"
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1589442095340",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "othIq5Q2Sgy4eZ3xkxkneg",
                "version": {
                    "created": "7060199"
                },
                "provided_name": "contacts_4_3t88f9nabk"
            }
        }
    }
}

我需要使用 CreateIndexRequest object 而不是 IndexRequest。

您可以嘗試下面的代碼塊通過 Java 高級 rest 客戶端以兩種(同步和異步)方式創建索引:

//Synchronous call
  public String createIndex(String indexName) throws IOException {
    CreateIndexRequest request = buildIndexRequest(indexName);
    CreateIndexResponse indexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
    return indexResponse.index();
  }

  //Asynchronous call
  public void createIndexAsync(String indexName) {
    CreateIndexRequest request = buildIndexRequest(indexName);

    ActionListener listener = new ActionListener<IndexResponse>() {
      @Override
      public void onResponse(IndexResponse indexResponse) {
        log.info("Index: {} has been created successfully", indexResponse.getIndex());
      }

      @Override
      public void onFailure(Exception e) {
        log.error("Exception occurred while creating the index: {}", indexName, e);
      }
    };

    restHighLevelClient.indices().createAsync(request, RequestOptions.DEFAULT, listener);
  }

  private CreateIndexRequest buildIndexRequest(String indexName) {
    CreateIndexRequest request = new CreateIndexRequest(indexName);

    //We can specify shards and replicas like below
    request.settings(Settings.builder()
        .put("index.number_of_shards", 1)
        .put("index.number_of_replicas", 2)
    );

    Map<String, Object> mapping = new HashMap<>();
    mapping.put("properties", buildProperties());
    request.mapping(mapping);
    return request;
  }

  //We can define field mapping as below
  private Map<String, Object> buildProperties() {
    Map<String, Object> textType = new HashMap<>();
    textType.put("type", "text");

    Map<String, Object> longType = new HashMap<>();
    longType.put("type", "long");

    Map<String, Object> dateType = new HashMap<>();
    dateType.put("type", "date");

    Map<String, Object> properties = new HashMap<>();
    properties.put("documentId", textType);
    properties.put("documentNumber", longType);
    properties.put("documentCreated", dateType);
    return properties;
  }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM