Search in sources :

Example 26 with CreateIndexResponse

use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project meveo by meveo-org.

the class ElasticSearchIndexPopulationService method createCETIndex.

/**
 * Update Elastic Search model with custom entity template definitions - depending on the configuration might create new index for each CET
 *
 * @param cet Custom entity template
 * @throws BusinessException business exception
 */
public void createCETIndex(CustomEntityTemplate cet) throws BusinessException {
    boolean storeAsTable = cet.getSqlStorageConfiguration() != null && cet.getSqlStorageConfiguration().isStoreAsTable();
    Class<? extends ISearchable> instanceClass = storeAsTable ? CustomTableRecord.class : CustomEntityInstance.class;
    ESIndexNameAndType indexAndType = addToIndexAndTypeCache(instanceClass, cet.getCode());
    // Not interested in storing and indexing this entity in Elastic Search
    if (indexAndType == null) {
        log.warn("No matching index found for CET {}", cet);
        return;
    }
    String indexName = indexAndType.getIndexName();
    String modelJson = esConfiguration.getCetIndexConfiguration(cet);
    if (modelJson == null) {
        log.warn("No matching index mapping found for CET {}", cet);
        return;
    }
    // Check if index is not defined yet, and define it if thats the case
    boolean indexExists = false;
    GetIndexRequest getIndexRequest = new GetIndexRequest();
    getIndexRequest.indices(indexName);
    try {
        GetIndexResponse response = esConnection.getClient().indices().get(getIndexRequest, RequestOptions.DEFAULT);
        indexExists = response.indices().length != 0;
    } catch (IOException e) {
        throw new BusinessException("Failed to get index " + indexName + " information in Elastic Search.", e);
    } catch (ElasticsearchStatusException e) {
        if (!(e.status() == RestStatus.NOT_FOUND && e.getMessage().contains("type=index_not_found_exception"))) {
            throw new BusinessException("Failed to find index " + indexName + " in Elastic Search.", e);
        }
    // index was not found, so continue on
    }
    if (!indexExists) {
        // Replace index name (already contains provider prefix) in index configuration json - this already contain provider prefix
        modelJson = modelJson.replaceAll(INDEX_INDEX_NAME_PLACEHOLDER, indexName);
        // Strip the actual index configuration by removing index name and use that index name in index creation (might contain non-alias name e.g. demo_custom_v1 vs
        // demo_custom as alias)
        int firstPos = modelJson.indexOf('"');
        String realIndexName = modelJson.substring(firstPos + 1, modelJson.indexOf('"', firstPos + 1));
        modelJson = modelJson.substring(modelJson.indexOf("{", realIndexName.length()), modelJson.lastIndexOf('}'));
        log.debug("Creating index {}/{} for Custom entity template: {} with model {}", realIndexName, indexName, cet.getCode(), modelJson);
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(realIndexName);
        createIndexRequest.source(modelJson, XContentType.JSON);
        try {
            @SuppressWarnings("unused") CreateIndexResponse createResponse = esConnection.getClient().indices().create(createIndexRequest, RequestOptions.DEFAULT);
        } catch (IOException e) {
            throw new BusinessException("Failed to create index " + realIndexName + "/" + indexName + " in Elastic Search.", e);
        }
    } else {
        log.debug("Index {} creation for Custom entity template: {} will be skipped as such index already exists", indexName, cet.getCode());
    }
}
Also used : IOException(java.io.IOException) ElasticsearchStatusException(org.elasticsearch.ElasticsearchStatusException) BusinessException(org.meveo.admin.exception.BusinessException) GetIndexResponse(org.elasticsearch.action.admin.indices.get.GetIndexResponse) GetIndexRequest(org.elasticsearch.action.admin.indices.get.GetIndexRequest) CreateIndexRequest(org.elasticsearch.action.admin.indices.create.CreateIndexRequest) CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 27 with CreateIndexResponse

use of org.elasticsearch.action.admin.indices.create.CreateIndexResponse in project elasticsearch-suggest-plugin by spinscale.

the class AbstractSuggestTest method createIndexWithProductsMapping.

private void createIndexWithProductsMapping(String indexName) throws IOException {
    String settingsData = IOUtils.toString(this.getClass().getResourceAsStream("/product.json"));
    CreateIndexResponse createIndexResponse = client().admin().indices().prepareCreate(indexName).setSource(settingsData).execute().actionGet();
    assertThat(createIndexResponse.isAcknowledged(), is(true));
    client().admin().cluster().prepareHealth(indexName).setWaitForGreenStatus().execute().actionGet();
}
Also used : CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse)

Example 28 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 29 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 30 with CreateIndexResponse

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

the class ActiveShardsObserverIT method testCreateIndexNoActiveShardsNoWaiting.

public void testCreateIndexNoActiveShardsNoWaiting() throws Exception {
    Settings.Builder settingsBuilder = Settings.builder().put(indexSettings()).put(INDEX_NUMBER_OF_SHARDS_SETTING.getKey(), randomIntBetween(1, 5)).put(INDEX_NUMBER_OF_REPLICAS_SETTING.getKey(), 0);
    if (internalCluster().getNodeNames().length > 0) {
        String exclude = String.join(",", internalCluster().getNodeNames());
        settingsBuilder.put("index.routing.allocation.exclude._name", exclude);
    }
    Settings settings = settingsBuilder.build();
    CreateIndexResponse response = prepareCreate("test-idx").setSettings(settings).setWaitForActiveShards(ActiveShardCount.NONE).get();
    assertTrue(response.isAcknowledged());
}
Also used : CreateIndexResponse(org.elasticsearch.action.admin.indices.create.CreateIndexResponse) Settings(org.elasticsearch.common.settings.Settings)

Aggregations

CreateIndexResponse (org.elasticsearch.action.admin.indices.create.CreateIndexResponse)49 CreateIndexRequest (org.elasticsearch.action.admin.indices.create.CreateIndexRequest)24 IOException (java.io.IOException)16 Settings (org.elasticsearch.common.settings.Settings)12 CreateIndexRequestBuilder (org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder)8 ElasticsearchException (org.elasticsearch.ElasticsearchException)7 ClusterHealthResponse (org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse)5 GetIndexRequest (org.elasticsearch.action.admin.indices.get.GetIndexRequest)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 ClusterState (org.elasticsearch.cluster.ClusterState)4 ResourceAlreadyExistsException (org.elasticsearch.ResourceAlreadyExistsException)3 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 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)3 RelationName (io.crate.metadata.RelationName)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2