use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project meveo by meveo-org.
the class ElasticSearchIndexPopulationService method createCETIndex.
/**
* Update Elastic Search model with custom entity template definitions - depending on the configuration might create new index for each CET
*
* @param cet Custom entity template
* @throws BusinessException business exception
*/
public void createCETIndex(CustomEntityTemplate cet) throws BusinessException {
boolean storeAsTable = cet.getSqlStorageConfiguration() != null && cet.getSqlStorageConfiguration().isStoreAsTable();
Class<? extends ISearchable> instanceClass = storeAsTable ? CustomTableRecord.class : CustomEntityInstance.class;
ESIndexNameAndType indexAndType = addToIndexAndTypeCache(instanceClass, cet.getCode());
// Not interested in storing and indexing this entity in Elastic Search
if (indexAndType == null) {
log.warn("No matching index found for CET {}", cet);
return;
}
String indexName = indexAndType.getIndexName();
String modelJson = esConfiguration.getCetIndexConfiguration(cet);
if (modelJson == null) {
log.warn("No matching index mapping found for CET {}", cet);
return;
}
// Check if index is not defined yet, and define it if thats the case
boolean indexExists = false;
GetIndexRequest getIndexRequest = new GetIndexRequest();
getIndexRequest.indices(indexName);
try {
GetIndexResponse response = esConnection.getClient().indices().get(getIndexRequest, RequestOptions.DEFAULT);
indexExists = response.indices().length != 0;
} catch (IOException e) {
throw new BusinessException("Failed to get index " + indexName + " information in Elastic Search.", e);
} catch (ElasticsearchStatusException e) {
if (!(e.status() == RestStatus.NOT_FOUND && e.getMessage().contains("type=index_not_found_exception"))) {
throw new BusinessException("Failed to find index " + indexName + " in Elastic Search.", e);
}
// index was not found, so continue on
}
if (!indexExists) {
// Replace index name (already contains provider prefix) in index configuration json - this already contain provider prefix
modelJson = modelJson.replaceAll(INDEX_INDEX_NAME_PLACEHOLDER, indexName);
// Strip the actual index configuration by removing index name and use that index name in index creation (might contain non-alias name e.g. demo_custom_v1 vs
// demo_custom as alias)
int firstPos = modelJson.indexOf('"');
String realIndexName = modelJson.substring(firstPos + 1, modelJson.indexOf('"', firstPos + 1));
modelJson = modelJson.substring(modelJson.indexOf("{", realIndexName.length()), modelJson.lastIndexOf('}'));
log.debug("Creating index {}/{} for Custom entity template: {} with model {}", realIndexName, indexName, cet.getCode(), modelJson);
CreateIndexRequest createIndexRequest = new CreateIndexRequest(realIndexName);
createIndexRequest.source(modelJson, XContentType.JSON);
try {
@SuppressWarnings("unused") CreateIndexResponse createResponse = esConnection.getClient().indices().create(createIndexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new BusinessException("Failed to create index " + realIndexName + "/" + indexName + " in Elastic Search.", e);
}
} else {
log.debug("Index {} creation for Custom entity template: {} will be skipped as such index already exists", indexName, cet.getCode());
}
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project elasticsearch-suggest-plugin by spinscale.
the class AbstractSuggestTest method createIndexWithProductsMapping.
private void createIndexWithProductsMapping(String indexName) throws IOException {
String settingsData = IOUtils.toString(this.getClass().getResourceAsStream("/product.json"));
CreateIndexResponse createIndexResponse = client().admin().indices().prepareCreate(indexName).setSource(settingsData).execute().actionGet();
assertThat(createIndexResponse.isAcknowledged(), is(true));
client().admin().cluster().prepareHealth(indexName).setWaitForGreenStatus().execute().actionGet();
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project pancm_project by xuwujing.
the class EsHighLevelRestTest1 method createIndex.
/**
* 创建索引
*
* @throws IOException
*/
private static void createIndex() throws IOException {
// 类型
String type = "_doc";
String index = "test1";
// setting 的值
Map<String, Object> setmapping = new HashMap<>();
// 分区数、副本数、缓存刷新时间
setmapping.put("number_of_shards", 10);
setmapping.put("number_of_replicas", 1);
setmapping.put("refresh_interval", "5s");
Map<String, Object> keyword = new HashMap<>();
// 设置类型
keyword.put("type", "keyword");
Map<String, Object> lon = new HashMap<>();
// 设置类型
lon.put("type", "long");
Map<String, Object> date = new HashMap<>();
// 设置类型
date.put("type", "date");
date.put("format", "yyyy-MM-dd HH:mm:ss");
Map<String, Object> jsonMap2 = new HashMap<>();
Map<String, Object> properties = new HashMap<>();
// 设置字段message信息
properties.put("uid", lon);
properties.put("phone", lon);
properties.put("msgcode", lon);
properties.put("message", keyword);
properties.put("sendtime", date);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
jsonMap2.put(type, mapping);
GetIndexRequest getRequest = new GetIndexRequest();
getRequest.indices(index);
getRequest.types(type);
getRequest.local(false);
getRequest.humanReadable(true);
boolean exists2 = client.indices().exists(getRequest, RequestOptions.DEFAULT);
// 如果存在就不创建了
if (exists2) {
System.out.println(index + "索引库已经存在!");
return;
}
// 开始创建库
CreateIndexRequest request = new CreateIndexRequest(index);
try {
// 加载数据类型
request.settings(setmapping);
// 设置mapping参数
request.mapping(type, jsonMap2);
// 设置别名
request.alias(new Alias("pancm_alias"));
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
boolean falg = createIndexResponse.isAcknowledged();
if (falg) {
System.out.println("创建索引库:" + index + "成功!");
}
} catch (IOException e) {
e.printStackTrace();
}
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project pancm_project by xuwujing.
the class IpHandler method creatIndex.
/**
* @return boolean
* @Author pancm
* @Description //创建索引库(指定Mpping类型)
* @Date 2019/3/21
* @Param [esBasicModelConfig]
*/
public static boolean creatIndex(EsBasicModelConfig esBasicModelConfig) throws IOException {
boolean falg = true;
Objects.requireNonNull(esBasicModelConfig, "esBasicModelConfig is not null");
String type = Objects.requireNonNull(esBasicModelConfig.getType(), "type is not null");
String index = Objects.requireNonNull(esBasicModelConfig.getIndex(), "index is not null");
if (exitsIndex(index)) {
logger.warn("索引库{}已经存在!无需在进行创建!", index);
return true;
}
String mapping = esBasicModelConfig.getMappings();
Map<String, Object> setting = esBasicModelConfig.getSettings();
String alias = esBasicModelConfig.getAlias();
// 开始创建库
CreateIndexRequest request = new CreateIndexRequest(index);
try {
if (Objects.nonNull(mapping)) {
// 加载数据类型
request.mapping(type, mapping);
}
if (Objects.nonNull(setting)) {
// 分片数
request.settings(setting);
}
if (Objects.nonNull(alias)) {
// 别名
request.alias(new Alias(alias));
}
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
falg = createIndexResponse.isAcknowledged();
} catch (IOException e) {
throw e;
} finally {
if (isAutoClose) {
close();
}
}
return falg;
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project elasticsearch by elastic.
the class ActiveShardsObserverIT method testCreateIndexNoActiveShardsNoWaiting.
public void testCreateIndexNoActiveShardsNoWaiting() throws Exception {
Settings.Builder settingsBuilder = Settings.builder().put(indexSettings()).put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), randomIntBetween(1, 5)).put(INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0);
if (internalCluster().getNodeNames().length > 0) {
String exclude = String.join(",", internalCluster().getNodeNames());
settingsBuilder.put("index.routing.allocation.exclude._name", exclude);
}
Settings settings = settingsBuilder.build();
CreateIndexResponse response = prepareCreate("test-idx").setSettings(settings).setWaitForActiveShards(ActiveShardCount.NONE).get();
assertTrue(response.isAcknowledged());
}
Aggregations