Search in sources :

Example 36 with CreateIndexResponse

use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project uavstack by uavorg.

the class ESClient method creatIndex.

/**
 * creatIndex
 *
 * @param index
 * @return
 */
public boolean creatIndex(String index) {
    CreateIndexRequest cir = new CreateIndexRequest(index);
    CreateIndexResponse resp = client.admin().indices().create(cir).actionGet();
    if (resp.isAcknowledged()) {
        return true;
    }
    return false;
}
Also used : CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 37 with CreateIndexResponse

use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project syncope by apache.

the class ElasticsearchReindex method doExecute.

@Override
protected String doExecute(final boolean dryRun) throws JobExecutionException {
    if (!dryRun) {
        try {
            LOG.debug("Start rebuild index {}", AuthContextUtils.getDomain().toLowerCase());
            IndicesExistsResponse existsIndexResponse = client.admin().indices().exists(new IndicesExistsRequest(AuthContextUtils.getDomain().toLowerCase())).get();
            if (existsIndexResponse.isExists()) {
                DeleteIndexResponse deleteIndexResponse = client.admin().indices().delete(new DeleteIndexRequest(AuthContextUtils.getDomain().toLowerCase())).get();
                LOG.debug("Successfully removed {}: {}", AuthContextUtils.getDomain().toLowerCase(), deleteIndexResponse);
            }
            XContentBuilder settings = XContentFactory.jsonBuilder().startObject().startObject("analysis").startObject("analyzer").startObject("string_lowercase").field("type", "custom").field("tokenizer", "standard").field("filter").startArray().value("lowercase").endArray().endObject().endObject().endObject().endObject();
            XContentBuilder mapping = XContentFactory.jsonBuilder().startObject().startArray("dynamic_templates").startObject().startObject("strings").field("match_mapping_type", "string").startObject("mapping").field("type", "keyword").field("analyzer", "string_lowercase").endObject().endObject().endObject().endArray().endObject();
            CreateIndexResponse createIndexResponse = client.admin().indices().create(new CreateIndexRequest(AuthContextUtils.getDomain().toLowerCase()).settings(settings).mapping(AnyTypeKind.USER.name(), mapping).mapping(AnyTypeKind.GROUP.name(), mapping).mapping(AnyTypeKind.ANY_OBJECT.name(), mapping)).get();
            LOG.debug("Successfully created {}: {}", AuthContextUtils.getDomain().toLowerCase(), createIndexResponse);
            LOG.debug("Indexing users...");
            for (int page = 1; page <= (userDAO.count() / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
                for (User user : userDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE)) {
                    IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), AnyTypeKind.USER.name(), user.getKey()).setSource(elasticsearchUtils.builder(user)).get();
                    LOG.debug("Index successfully created for {}: {}", user, response);
                }
            }
            LOG.debug("Indexing groups...");
            for (int page = 1; page <= (groupDAO.count() / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
                for (Group group : groupDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE)) {
                    IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), AnyTypeKind.GROUP.name(), group.getKey()).setSource(elasticsearchUtils.builder(group)).get();
                    LOG.debug("Index successfully created for {}: {}", group, response);
                }
            }
            LOG.debug("Indexing any objects...");
            for (int page = 1; page <= (anyObjectDAO.count() / AnyDAO.DEFAULT_PAGE_SIZE) + 1; page++) {
                for (AnyObject anyObject : anyObjectDAO.findAll(page, AnyDAO.DEFAULT_PAGE_SIZE)) {
                    IndexResponse response = client.prepareIndex(AuthContextUtils.getDomain().toLowerCase(), AnyTypeKind.ANY_OBJECT.name(), anyObject.getKey()).setSource(elasticsearchUtils.builder(anyObject)).get();
                    LOG.debug("Index successfully created for {}: {}", anyObject, response);
                }
            }
            LOG.debug("Rebuild index {} successfully completed", AuthContextUtils.getDomain().toLowerCase());
        } catch (Exception e) {
            throw new JobExecutionException("While rebuilding index " + AuthContextUtils.getDomain().toLowerCase(), e);
        }
    }
    return "SUCCESS";
}
Also used : Group(org.apache.syncope.core.persistence.api.entity.group.Group) User(org.apache.syncope.core.persistence.api.entity.user.User) DeleteIndexRequest(org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest) JobExecutionException(org.quartz.JobExecutionException) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) AnyObject(org.apache.syncope.core.persistence.api.entity.anyobject.AnyObject) JobExecutionException(org.quartz.JobExecutionException) DeleteIndexResponse(org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) IndexResponse(org.elasticsearch.action.index.IndexResponse) IndicesExistsResponse(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) IndicesExistsRequest(org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 38 with CreateIndexResponse

use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project incubator-skywalking by apache.

the class ElasticSearchClient method createIndex.

public boolean createIndex(String indexName, String indexType, Settings settings, XContentBuilder mappingBuilder) {
    if (!ready) {
        throw new ElasticSearchClientNotReadyException();
    }
    IndicesAdminClient adminClient = client.admin().indices();
    indexName = formatIndexName(indexName);
    CreateIndexResponse response = adminClient.prepareCreate(indexName).setSettings(settings).addMapping(indexType, mappingBuilder).get();
    logger.info("create {} index with type of {} finished, isAcknowledged: {}", indexName, indexType, response.isAcknowledged());
    return response.isShardsAcked();
}
Also used : IndicesAdminClient(org.elasticsearch.client.IndicesAdminClient) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 39 with CreateIndexResponse

use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project blog-example by my-dlq.

the class IndexService 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("mydlq-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 40 with CreateIndexResponse

use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project elasticsearch-learning-to-rank by o19s.

the class BaseIntegrationTest method createStore.

public void createStore(String name) throws Exception {
    assert IndexFeatureStore.isIndexStore(name);
    CreateIndexResponse resp = client().execute(CreateIndexAction.INSTANCE, IndexFeatureStore.buildIndexRequest(name)).get();
    assertTrue(resp.isAcknowledged());
}
Also used : CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

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