Search in sources :

Example 41 with CreateIndexResponse

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);
    }
}
Also used : IndexMapping(com.enonic.xp.repository.IndexMapping) IndexException(com.enonic.xp.repository.IndexException) UpdateIndexSettings(com.enonic.xp.repo.impl.index.UpdateIndexSettings) IndexSettings(com.enonic.xp.repository.IndexSettings) ElasticsearchException(org.elasticsearch.ElasticsearchException) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) IndexType(com.enonic.xp.index.IndexType) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) ImmutableOpenMap(org.elasticsearch.common.collect.ImmutableOpenMap) Map(java.util.Map)

Example 42 with CreateIndexResponse

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();
        }
    };
}
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 43 with CreateIndexResponse

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

Example 44 with CreateIndexResponse

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

Example 45 with CreateIndexResponse

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

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