createIndex elasticsearch клиент остального высокого уровня для JAVA неправильно выполняет настройки
Я использую этот код из документации 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"
}
}
}
}
2 ответа
Мне нужно было использовать объект CreateIndexRequest, а не IndexRequest.
Вы можете попробовать следующий блок кода для создания индекса обоими способами (синхронным и асинхронным) через клиент отдыха высокого уровня 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;
}