Java Code Examples for org.elasticsearch.action.admin.indices.create.CreateIndexRequest#settings()

The following examples show how to use org.elasticsearch.action.admin.indices.create.CreateIndexRequest#settings() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexServiceImpl.java    From microservices-platform with Apache License 2.0 6 votes vote down vote up
@Override
public boolean create(IndexDto indexDto) throws IOException {
    CreateIndexRequest request = new CreateIndexRequest(indexDto.getIndexName());
    request.settings(Settings.builder()
            .put("index.number_of_shards", indexDto.getNumberOfShards())
            .put("index.number_of_replicas", indexDto.getNumberOfReplicas())
    );
    if (StrUtil.isNotEmpty(indexDto.getType()) && StrUtil.isNotEmpty(indexDto.getMappingsSource())) {
        //mappings
        request.mapping(indexDto.getType(), indexDto.getMappingsSource(), XContentType.JSON);
    }
    CreateIndexResponse response = elasticsearchRestTemplate.getClient()
            .indices()
            .create(request, RequestOptions.DEFAULT);
    return response.isAcknowledged();
}
 
Example 2
Source File: ElasticSearchDAOV5.java    From conductor with Apache License 2.0 6 votes vote down vote up
private void addIndex(String indexName) {
    try {
        elasticSearchClient.admin()
                .indices()
                .prepareGetIndex()
                .addIndices(indexName)
                .execute()
                .actionGet();
    } catch (IndexNotFoundException infe) {
        try {

            CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
            createIndexRequest.settings(Settings.builder()
                    .put("index.number_of_shards", config.getElasticSearchIndexShardCount())
                    .put("index.number_of_replicas", config.getElasticSearchIndexReplicationCount())
            );

            elasticSearchClient.admin()
                    .indices()
                    .create(createIndexRequest)
                    .actionGet();
        } catch (ResourceAlreadyExistsException done) {
            // no-op
        }
    }
}
 
Example 3
Source File: ElasticSearchDAOV6.java    From conductor with Apache License 2.0 6 votes vote down vote up
private void createIndex(String indexName) {
    try {
        elasticSearchClient.admin().indices().prepareGetIndex().addIndices(indexName).execute().actionGet();
    } catch (IndexNotFoundException infe) {
        try {
            CreateIndexRequest createIndexRequest = new CreateIndexRequest(indexName);
            createIndexRequest.settings(Settings.builder()
                    .put("index.number_of_shards", config.getElasticSearchIndexShardCount())
                    .put("index.number_of_replicas", config.getElasticSearchIndexReplicationCount())
            );

            elasticSearchClient.admin()
                    .indices()
                    .create(createIndexRequest)
                    .actionGet();
        } catch (ResourceAlreadyExistsException done) {
            LOGGER.error("Failed to update log index name: {}", indexName, done);
        }
    }
}
 
Example 4
Source File: ElasticsearchClientRest.java    From c2mon with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public boolean createIndex(IndexMetadata indexMetadata, String mapping) {
  CreateIndexRequest request = new CreateIndexRequest(indexMetadata.getName());

  request.settings(Settings.builder()
      .put("index.number_of_shards", properties.getShardsPerIndex())
      .put("index.number_of_replicas", properties.getReplicasPerShard())
  );

  if (properties.isAutoTemplateMapping()) {
    request.mapping(TYPE, mapping, XContentType.JSON);
  }

  try {
    CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
    return createIndexResponse.isAcknowledged();
  } catch (IOException e) {
    log.error("Error creating '{}' index on Elasticsearch.", indexMetadata.getName(), e);
  }

  return false;
}
 
Example 5
Source File: ElasticsearchConfiguration.java    From datashare with GNU Affero General Public License v3.0 6 votes vote down vote up
public static boolean createIndex(RestHighLevelClient client, String indexName, String indexType) {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName);
    try {
        if (!client.indices().exists(request)) {
            LOGGER.info("index {} does not exist, creating one", indexName);
            CreateIndexRequest createReq = new CreateIndexRequest(indexName);
            createReq.settings(getResourceContent(SETTINGS_RESOURCE_NAME), JSON);
            createReq.mapping(indexType, getResourceContent(MAPPING_RESOURCE_NAME), JSON);
            client.indices().create(createReq);
            return true;
        }
    } catch (IOException e) {
        throw new ConfigurationException(e);
    }
    return false;
}
 
Example 6
Source File: ElasticSearchSink.java    From pulsar with Apache License 2.0 6 votes vote down vote up
private void createIndexIfNeeded() throws IOException {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(elasticSearchConfig.getIndexName());
    boolean exists = getClient().indices().exists(request);

    if (!exists) {
        CreateIndexRequest cireq = new CreateIndexRequest(elasticSearchConfig.getIndexName());

        cireq.settings(Settings.builder()
           .put("index.number_of_shards", elasticSearchConfig.getIndexNumberOfShards())
           .put("index.number_of_replicas", elasticSearchConfig.getIndexNumberOfReplicas()));

        CreateIndexResponse ciresp = getClient().indices().create(cireq);
        if (!ciresp.isAcknowledged() || !ciresp.isShardsAcknowledged()) {
            throw new RuntimeException("Unable to create index.");
        }
    }
}
 
Example 7
Source File: TestUtils.java    From foxtrot with Apache License 2.0 6 votes vote down vote up
public static ElasticsearchConnection initESConnection(MockElasticsearchServer elasticsearchServer) {
    CreateIndexRequest createRequest = new CreateIndexRequest(TableMapStore.TABLE_META_INDEX);
    Settings indexSettings = Settings.builder()
            .put("number_of_replicas", 0)
            .build();
    createRequest.settings(indexSettings);
    elasticsearchServer.getClient()
            .admin()
            .indices()
            .create(createRequest)
            .actionGet();
    elasticsearchServer.getClient()
            .admin()
            .cluster()
            .prepareHealth()
            .setWaitForGreenStatus()
            .execute()
            .actionGet();
    ElasticsearchConnection elasticsearchConnection = Mockito.mock(ElasticsearchConnection.class);
    when(elasticsearchConnection.getClient()).thenReturn(elasticsearchServer.getClient());
    ElasticsearchUtils.initializeMappings(elasticsearchServer.getClient());
    return elasticsearchConnection;
}
 
Example 8
Source File: ElasticsearchRule.java    From datashare with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {
    GetIndexRequest request = new GetIndexRequest();
    request.indices(indexName);
    if (! client.indices().exists(request)) {
        CreateIndexRequest createReq = new CreateIndexRequest(indexName);
        byte[] settings = toByteArray(getClass().getClassLoader().getResourceAsStream(SETTINGS_RESOURCE_NAME));
        createReq.settings(new String(settings), JSON);
        byte[] mapping = toByteArray(getClass().getClassLoader().getResourceAsStream(MAPPING_RESOURCE_NAME));
        createReq.mapping("doc", new String(mapping), JSON);
        client.indices().create(createReq);
    }
}
 
Example 9
Source File: ElasticSearchService.java    From pybbs with GNU Affero General Public License v3.0 5 votes vote down vote up
public boolean createIndex(String type, XContentBuilder mappingBuilder) {
    try {
        if (this.instance() == null) return false;
        CreateIndexRequest request = new CreateIndexRequest(name);
        request.settings(Settings.builder().put("index.number_of_shards", 1).put("index.number_of_shards", 5));
        if (mappingBuilder != null) request.mapping(type, mappingBuilder);
        CreateIndexResponse response = this.client.indices().create(request, RequestOptions.DEFAULT);
        return response.isAcknowledged();
    } catch (IOException e) {
        log.error(e.getMessage());
        return false;
    }
}
 
Example 10
Source File: ElasticSearchClient.java    From skywalking with Apache License 2.0 5 votes vote down vote up
public boolean createIndex(String indexName, Map<String, Object> settings,
                           Map<String, Object> mapping) throws IOException {
    indexName = formatIndexName(indexName);
    CreateIndexRequest request = new CreateIndexRequest(indexName);
    Gson gson = new Gson();
    request.settings(gson.toJson(settings), XContentType.JSON);
    request.mapping(TYPE, gson.toJson(mapping), XContentType.JSON);
    CreateIndexResponse response = client.indices().create(request);
    log.debug("create {} index finished, isAcknowledged: {}", indexName, response.isAcknowledged());
    return response.isAcknowledged();
}
 
Example 11
Source File: RandomCreateIndexGenerator.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a random {@link CreateIndexRequest}. Randomizes the index name, the aliases,
 * mappings and settings associated with the index.
 */
public static CreateIndexRequest randomCreateIndexRequest() throws IOException {
    String index = randomAlphaOfLength(5);
    CreateIndexRequest request = new CreateIndexRequest(index);
    randomAliases(request);
    if (randomBoolean()) {
        String type = randomAlphaOfLength(5);
        request.mapping(type, randomMapping(type));
    }
    if (randomBoolean()) {
        request.settings(randomIndexSettings());
    }
    return request;
}
 
Example 12
Source File: EsAggregationSearchTest.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
 * 创建索引
 *
 * @throws IOException
 */
private static void createIndex() throws IOException {

    // 类型
    String type = "_doc";
    String index = "student";
    // 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");

    Map<String, Object> date2 = new HashMap<>();
    //设置类型
    date2.put("type", "date");
    date2.put("format", "yyyy-MM-dd HH:mm:ss.SSS");
    Map<String, Object> jsonMap2 = new HashMap<>();
    Map<String, Object> properties = new HashMap<>();
    //设置字段message信息
    properties.put("uid", lon);
    properties.put("grade", lon);
    properties.put("class", lon);
    properties.put("age", lon);
    properties.put("name", keyword);
    properties.put("createtm", date);
    properties.put("updatetm", date2);
    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();
    }

}
 
Example 13
Source File: EsUtil.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
 * @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;

}
 
Example 14
Source File: EsHighLevelRestTest1.java    From java-study with Apache License 2.0 4 votes vote down vote up
/**
 * 创建索引
 * 
 * @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();
	}

}