use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project incubator-inlong by apache.
the class ElasticsearchService method createIndex.
protected boolean createIndex(String index) throws IOException {
if (existsIndex(index)) {
return true;
}
CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
createIndexRequest.settings(Settings.builder().put("index.number_of_shards", esConfig.getShardsNum()).put("index.number_of_replicas", esConfig.getReplicaNum()));
createIndexRequest.mapping("_doc", generateBuilder());
CreateIndexResponse response = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
boolean res = response.isAcknowledged();
if (res) {
LOG.info("success creating index {}", index);
} else {
LOG.info("fail to create index {}", index);
}
return res;
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project olive by ClareTung.
the class IndexService2 method createIndex.
/**
* 创建索引
*/
public Object createIndex() {
Object result = "";
try {
// 创建 Mapping
XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().field("dynamic", true).startObject("properties").startObject("name").field("type", "text").startObject("fields").startObject("keyword").field("type", "keyword").endObject().endObject().endObject().startObject("address").field("type", "text").startObject("fields").startObject("keyword").field("type", "keyword").endObject().endObject().endObject().startObject("remark").field("type", "text").startObject("fields").startObject("keyword").field("type", "keyword").endObject().endObject().endObject().startObject("age").field("type", "integer").endObject().startObject("salary").field("type", "float").endObject().startObject("birthDate").field("type", "date").field("format", "yyyy-MM-dd").endObject().startObject("createTime").field("type", "date").endObject().endObject().endObject();
// 创建索引配置信息,配置
Settings settings = Settings.builder().put("index.number_of_shards", 1).put("index.number_of_replicas", 0).build();
// 新建创建索引请求对象,然后设置索引类型(ES 7.0 将不存在索引类型)和 mapping 与 index 配置
CreateIndexRequest request = new CreateIndexRequest("es-start-user", settings);
request.mapping("doc", mapping);
// RestHighLevelClient 执行创建索引
CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
// 判断是否创建成功
boolean isCreated = createIndexResponse.isAcknowledged();
log.info("是否创建成功:{}", isCreated);
// 根据具体业务逻辑返回不同结果,这里为了方便直接将结果返回
result = isCreated;
} catch (IOException e) {
log.error("", e);
}
return result;
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project 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 springboot-templet-start by thedestiny.
the class IEsServiceImpl method createIndexRequest.
@Override
public void createIndexRequest(String index) {
CreateIndexRequest createIndexRequest = new CreateIndexRequest(index).settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 0));
try {
CreateIndexResponse response = client.indices().create(createIndexRequest, COMMON_OPTIONS);
log.info(" 所有节点确认响应 : {}", response.isAcknowledged());
log.info(" 所有分片的复制未超时 :{}", response.isShardsAcknowledged());
} catch (IOException e) {
log.error("创建索引库【{}】失败", index, e);
}
}
use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project meveo by meveo-org.
the class ElasticSearchIndexPopulationService method createIndexes.
/**
* Recreate indexes for a <b>current provider</b>. Index names are prefixed by provider code (removed spaces and lowercase).
*
* @throws BusinessException Failure to create index in ES exception.
*/
@JpaAmpNewTx
@TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
public void createIndexes() throws BusinessException {
String indexPrefix = currentUser.getProviderCode() == null ? "null" : BaseEntity.cleanUpAndLowercaseCodeOrId(currentUser.getProviderCode());
log.debug("Creating Elastic Search indexes with prefix {}", indexPrefix);
repopulateIndexAndTypeCache(false);
RestHighLevelClient client = esConnection.getClient();
// Create indexes
CreateIndexRequest createIndexRequest = null;
for (Entry<String, String> model : esConfiguration.getDataModel().entrySet()) {
String indexName = model.getKey().replace(INDEX_PROVIDER_PLACEHOLDER, indexPrefix);
String modelJson = model.getValue().replace(INDEX_PROVIDER_PLACEHOLDER, indexPrefix);
log.debug("Creating index for entity: {} with model {}", indexName, modelJson);
createIndexRequest = new CreateIndexRequest(indexName);
createIndexRequest.source(modelJson, XContentType.JSON);
try {
@SuppressWarnings("unused") CreateIndexResponse createResponse = client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
} catch (IOException e) {
throw new BusinessException("Failed to create index " + indexName + " in Elastic Search.", e);
}
}
log.trace("Creating Elastic Search mappings for CETs with prefix {}", indexPrefix);
// Create mappings for custom entity templates
List<CustomEntityTemplate> cets = customEntityTemplateService.listNoCache();
for (CustomEntityTemplate cet : cets) {
createCETIndex(cet);
}
log.trace("Updating Elastic Search mappings for CFTs with prefix {}", indexPrefix);
// Update model mapping with custom fields
List<CustomFieldTemplate> cfts = customFieldTemplateService.getCFTForIndex();
for (CustomFieldTemplate cft : cfts) {
updateCFMapping(cft);
}
}
Aggregations