0

我正在使用elasticsearch 文档中的这段代码来创建索引。我应该能够将 Postman 的索引配置对象粘贴到我的 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"
            }
        }
    }
}
4

2 回答 2

0

我需要使用 CreateIndexRequest 对象而不是 IndexRequest。

于 2020-05-14T07:54:47.467 回答
0

您可以尝试下面的代码块通过 Java 高级休息客户端以两种(同步和异步)方式创建索引:

//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;
  }
于 2020-06-24T09:24:42.740 回答