Create Index APIedit
Create Index Requestedit
A CreateIndexRequest
requires an index
argument:
Index settingsedit
Each index created can have specific settings associated with it.
Index mappingsedit
An index may be created with mappings for its document types
request.mapping("_doc", "{\n" + " \"_doc\": {\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + " }\n" + "}", XContentType.JSON);
The mapping source can be provided in different ways in addition to the
String
example shown above:
Map<String, Object> jsonMap = new HashMap<>(); Map<String, Object> message = new HashMap<>(); message.put("type", "text"); Map<String, Object> properties = new HashMap<>(); properties.put("message", message); Map<String, Object> mapping = new HashMap<>(); mapping.put("properties", properties); jsonMap.put("_doc", mapping); request.mapping("_doc", jsonMap);
XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.startObject("_doc"); { builder.startObject("properties"); { builder.startObject("message"); { builder.field("type", "text"); } builder.endObject(); } builder.endObject(); } builder.endObject(); } builder.endObject(); request.mapping("_doc", builder);
Mapping source provided as an |
Index aliasesedit
Aliases can be set at index creation time
Providing the whole sourceedit
The whole source including all of its sections (mappings, settings and aliases) can also be provided:
request.source("{\n" + " \"settings\" : {\n" + " \"number_of_shards\" : 1,\n" + " \"number_of_replicas\" : 0\n" + " },\n" + " \"mappings\" : {\n" + " \"_doc\" : {\n" + " \"properties\" : {\n" + " \"message\" : { \"type\" : \"text\" }\n" + " }\n" + " }\n" + " },\n" + " \"aliases\" : {\n" + " \"twitter_alias\" : {}\n" + " }\n" + "}", XContentType.JSON);
Optional argumentsedit
The following arguments can optionally be provided:
Timeout to wait for the all the nodes to acknowledge the index creation as a |
|
Timeout to wait for the all the nodes to acknowledge the index creation as a |
Timeout to connect to the master node as a |
|
Timeout to connect to the master node as a |
Synchronous Executionedit
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
Asynchronous Executionedit
The asynchronous execution of a create index request requires both the CreateIndexRequest
instance and an ActionListener
instance to be passed to the asynchronous
method:
The asynchronous method does not block and returns immediately. Once it is
completed the ActionListener
is called back using the onResponse
method
if the execution successfully completed or using the onFailure
method if
it failed.
A typical listener for CreateIndexResponse
looks like:
Create Index Responseedit
The returned CreateIndexResponse
allows to retrieve information about the executed
operation as follows: