* @return {@code true} if an error occurs during the creation.
*/
private boolean createIndex() {
for (String type : TYPES) {
try {
CreateIndexRequestBuilder createIndex = client().admin().indices().prepareCreate(indexName(type));
URL mappingUrl = getClass().getClassLoader().getResource("META-INF/elasticsearch/index/" + type + ".json");
ObjectMapper jsonMapper = new ObjectMapper();
JsonNode indexConfig = jsonMapper.readTree(mappingUrl);
JsonNode indexSettings = indexConfig.get("settings");
if (indexSettings != null && indexSettings.isObject()) {
createIndex.setSettings(jsonMapper.writeValueAsString(indexSettings));
}
JsonNode mappings = indexConfig.get("mappings");
if (mappings != null && mappings.isObject()) {
for (Iterator<Map.Entry<String, JsonNode>> i = mappings.fields(); i.hasNext(); ) {
Map.Entry<String, JsonNode> field = i.next();
ObjectNode mapping = jsonMapper.createObjectNode();
mapping.put(field.getKey(), field.getValue());
createIndex.addMapping(field.getKey(), jsonMapper.writeValueAsString(mapping));
}
}
boolean ack = createIndex.execute().actionGet().acknowledged();
if (!ack) {
log.error("Cannot create index " + indexName(type));
return false;
}