Creating Elasticsearch Indices via REST and Java API
Index creation in Elasticsearch can be performed via the REST API. When defining the request body, you can specify settings, mappings, and aliases. Note that the index wrapper inside settings is optional, allowing for a flatter configuration structure. For keyword fields, the ignore_above parameter defaults to 256.
Kibana Console
The following example creates a index named employee_data. It configures the number of shards and replicas, defines mappings for specific fields (including a multi-field for the bio), and assigns an alias.
PUT /employee_data
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"username": {
"type": "keyword"
},
"age": {
"type": "integer"
},
"bio": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword"
}
}
}
}
},
"aliases": {
"staff_directory": {}
}
}
cURL Command
The same operation can be executed via the command line using curl. Ensure the Content-Type header is set correctly.
curl -XPUT "http://localhost:9200/employee_data" -H 'Content-Type: application/json' -d '
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"username": { "type": "keyword" },
"age": { "type": "integer" },
"bio": {
"type": "text",
"fields": {
"keyword": { "type": "keyword" }
}
}
}
},
"aliases": {
"staff_directory": {}
}
}
'
To create an index programmatically in Java, use the CreateIndexRequest class. This allows for detailed configuration including settings, mappings, aliases, and timeout preferences.
Implementation Example
The code below demonstrates how to construct the request object, define settings using the Settings.Builder, and add mappings via a Map structure.
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.unit.TimeValue;
import java.util.HashMap;
import java.util.Map;
public boolean createEmployeeIndex(RestHighLevelClient client) throws Exception {
// Initialize the request for the index name "employee_data"
CreateIndexRequest request = new CreateIndexRequest("employee_data");
// Configure index settings
request.settings(Settings.builder()
.put("index.number_of_shards", 2)
.put("index.number_of_replicas", 1)
);
// Define mappings using a Map structure
Map<String, Object> usernameMapping = new HashMap<>();
usernameMapping.put("type", "keyword");
Map<String, Object> ageMapping = new HashMap<>();
ageMapping.put("type", "integer");
// Configure multi-field for "bio"
Map<String, Object> bioKeywordMapping = new HashMap<>();
bioKeywordMapping.put("type", "keyword");
Map<String, Object> bioFields = new HashMap<>();
bioFields.put("keyword", bioKeywordMapping);
Map<String, Object> bioMapping = new HashMap<>();
bioMapping.put("type", "text");
bioMapping.put("fields", bioFields);
// Assemble properties
Map<String, Object> properties = new HashMap<>();
properties.put("username", usernameMapping);
properties.put("age", ageMapping);
properties.put("bio", bioMapping);
Map<String, Object> mappingSource = new HashMap<>();
mappingSource.put("properties", properties);
request.mapping(mappingSource);
// Set alias
request.alias(new Alias("staff_directory"));
// Configure timeouts
request.setTimeout(TimeValue.timeValueMinutes(2));
request.setMasterTimeout(TimeValue.timeValueMinutes(1));
// Execute the request
CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT);
return response.isAcknowledged();
}