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

The following examples show how to use org.elasticsearch.action.admin.indices.create.CreateIndexRequest#mapping() . 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: ElasticsearchNamespaceStore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void init() {
	boolean indexExistsAlready = clientProvider.getClient()
			.admin()
			.indices()
			.exists(new IndicesExistsRequest(index))
			.actionGet()
			.isExists();

	if (!indexExistsAlready) {
		CreateIndexRequest request = new CreateIndexRequest(index);
		request.mapping(ELASTICSEARCH_TYPE, MAPPING, XContentType.JSON);
		clientProvider.getClient().admin().indices().create(request).actionGet();
	}

}
 
Example 3
Source File: ElasticsearchDataStructure.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void init() {

	boolean indexExistsAlready = clientProvider.getClient()
			.admin()
			.indices()
			.exists(new IndicesExistsRequest(index))
			.actionGet()
			.isExists();

	if (!indexExistsAlready) {
		CreateIndexRequest request = new CreateIndexRequest(index);
		request.mapping(ELASTICSEARCH_TYPE, MAPPING, XContentType.JSON);
		clientProvider.getClient().admin().indices().create(request).actionGet();
	}

	refreshIndex();

}
 
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: TransportIndexAction.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected void doExecute(final Task task, final IndexRequest request, final ActionListener<IndexResponse> listener) {
    // if we don't have a master, we don't have metadata, that's fine, let it find a master using create index API
    ClusterState state = clusterService.state();
    if (autoCreateIndex.shouldAutoCreate(request.index(), state)) {
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(request);
        createIndexRequest.index(request.index());
        createIndexRequest.mapping(request.type());
        createIndexRequest.cause("auto(index api)");
        createIndexRequest.masterNodeTimeout(request.timeout());
        createIndexAction.execute(task, createIndexRequest, new ActionListener<CreateIndexResponse>() {
            @Override
            public void onResponse(CreateIndexResponse result) {
                innerExecute(task, request, listener);
            }

            @Override
            public void onFailure(Throwable e) {
                if (ExceptionsHelper.unwrapCause(e) instanceof IndexAlreadyExistsException) {
                    // we have the index, do it
                    try {
                        innerExecute(task, request, listener);
                    } catch (Throwable e1) {
                        listener.onFailure(e1);
                    }
                } else {
                    listener.onFailure(e);
                }
            }
        });
    } else {
        innerExecute(task, request, listener);
    }
}
 
Example 7
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 8
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 9
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 10
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 11
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 12
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 13
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();
	}

}