Search in sources :

Example 46 with CreateIndexResponse

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;
}
Also used : CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 47 with CreateIndexResponse

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;
}
Also used : IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) Settings(org.elasticsearch.common.settings.Settings)

Example 48 with CreateIndexResponse

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();
        }
    };
}
Also used : DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) TransportClient(org.elasticsearch.client.transport.TransportClient) GetRequest(org.elasticsearch.action.get.GetRequest) CreateIndexRequestBuilder(org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IOException(java.io.IOException) DeleteIndexRequestBuilder(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) IndexNotFoundException(org.elasticsearch.index.IndexNotFoundException) IOException(java.io.IOException)

Example 49 with CreateIndexResponse

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);
    }
}
Also used : IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 50 with CreateIndexResponse

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);
    }
}
Also used : BusinessException(org.meveo.admin.exception.BusinessException) CustomEntityTemplate(org.meveo.model.customEntities.CustomEntityTemplate) CustomFieldTemplate(org.meveo.model.crm.CustomFieldTemplate) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) IOException(java.io.IOException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) JpaAmpNewTx(org.meveo.jpa.JpaAmpNewTx) TransactionAttribute(javax.ejb.TransactionAttribute)

Aggregations

CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)56 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)26 IOException (java.io.IOException)19 Settings (org.elasticsearch.common.settings.Settings)13 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)12 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)9 ElasticsearchException (org.elasticsearch.ElasticsearchException)7 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)5 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)5 IndexNotFoundException (org.elasticsearch.index.IndexNotFoundException)5 Alias (org.elasticsearch.action.admin.indices.alias.Alias)4 DeleteIndexResponse (org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse)4 GetIndexRequest (org.elasticsearch.action.admin.indices.get.GetIndexRequest)4 ClusterState (org.elasticsearch.cluster.ClusterState)4 IndicesExistsRequest (org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest)3 GetIndexResponse (org.elasticsearch.action.admin.indices.get.GetIndexResponse)3 PutMappingResponse (org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse)3 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)3 RelationName (io.crate.metadata.RelationName)2 HashMap (java.util.HashMap)2