use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project xp by enonic.
the class IndexServiceInternalImpl method createIndex.
@Override
public void createIndex(final com.enonic.xp.repo.impl.index.CreateIndexRequest request) {
final String indexName = request.getIndexName();
final IndexSettings indexSettings = request.getIndexSettings();
LOG.info("creating index {}", indexName);
CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
createIndexRequest.settings(indexSettings.getAsString());
if (request.getMappings() != null) {
for (Map.Entry<IndexType, IndexMapping> mappingEntry : request.getMappings().entrySet()) {
createIndexRequest.mapping(mappingEntry.getKey().isDynamicTypes() ? ES_DEFAULT_INDEX_TYPE_NAME : mappingEntry.getKey().getName(), mappingEntry.getValue().getAsString());
}
}
try {
final CreateIndexResponse createIndexResponse = client.admin().indices().create(createIndexRequest).actionGet(CREATE_INDEX_TIMEOUT);
LOG.info("Index {} created with status {}", indexName, createIndexResponse.isAcknowledged());
} catch (ElasticsearchException e) {
throw new IndexException("Failed to create index: " + indexName, e);
}
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project incubator-gobblin by apache.
the class TransportWriterVariant method getTestClient.
@Override
public TestClient getTestClient(Config config) throws IOException {
final ElasticsearchTransportClientWriter transportClientWriter = new ElasticsearchTransportClientWriter(config);
final TransportClient transportClient = transportClientWriter.getTransportClient();
return new TestClient() {
@Override
public GetResponse get(GetRequest getRequest) throws IOException {
try {
return transportClient.get(getRequest).get();
} catch (Exception e) {
throw new IOException(e);
}
}
@Override
public void recreateIndex(String indexName) throws IOException {
DeleteIndexRequestBuilder dirBuilder = transportClient.admin().indices().prepareDelete(indexName);
try {
DeleteIndexResponse diResponse = dirBuilder.execute().actionGet();
} catch (IndexNotFoundException ie) {
System.out.println("Index not found... that's ok");
}
CreateIndexRequestBuilder cirBuilder = transportClient.admin().indices().prepareCreate(indexName);
CreateIndexResponse ciResponse = cirBuilder.execute().actionGet();
Assert.assertTrue(ciResponse.isAcknowledged(), "Create index succeeeded");
}
@Override
public void close() throws IOException {
transportClientWriter.close();
}
};
}
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 java-apply by javachengwc.
the class EsServiceImpl method createMapping.
// 创建索引映射
public void createMapping(String collectName, String indexType, Map<String, Map<String, String>> fieldSetting) throws Exception {
logger.info("EsServiceImpl createMapping start,collectName={},indexType={}", collectName, indexType);
CreateIndexRequestBuilder cib = esClient.admin().indices().prepareCreate(collectName);
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startObject(// 设置定义字段
"properties");
// .endObject()
for (String field : fieldSetting.keySet()) {
mapping.startObject(field);
Map<String, String> setting = fieldSetting.get(field);
for (Map.Entry<String, String> entry : setting.entrySet()) {
mapping.field(entry.getKey(), entry.getValue());
}
mapping.endObject();
}
mapping.endObject();
mapping.endObject();
cib.addMapping(indexType, mapping);
CreateIndexResponse response = cib.execute().get();
logger.info("EsServiceImpl createMapping end,collectName={},indexType={}", collectName, indexType);
}
Aggregations