【elasticsearch7.x】创建索引、获取索引配置信息

package com.es.fixData.get;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.elasticsearch.ResourceAlreadyExistsException;
import org.elasticsearch.action.admin.indices.alias.get.GetAliasesRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsRequest;
import org.elasticsearch.action.admin.indices.settings.get.GetSettingsResponse;
import org.elasticsearch.client.GetAliasesResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.Requests;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.GetMappingsRequest;
import org.elasticsearch.client.indices.GetMappingsResponse;
import org.elasticsearch.cluster.metadata.AliasMetaData;
import org.elasticsearch.cluster.metadata.MappingMetaData;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import com.alibaba.fastjson.JSONObject;
import com.es.util.ESClient;

public class CreateIndex {
	public static void main(String[] args) throws Exception {
		// 创建测试索引
		createTest();

		// 给指定索引添加字段
		addField("test_wd");

		// 获取索引配置信息
		getIndexSettings();
	}

	private static void getIndexSettings() {

		RestHighLevelClient client = ESClient.getES7Client();

		try {
			GetAliasesRequest request = new GetAliasesRequest();
			GetAliasesResponse getAliasesResponse = client.indices().getAlias(request, RequestOptions.DEFAULT);
			Map<String, Set<AliasMetaData>> map = getAliasesResponse.getAliases();
			// 获取es7全部索引集合
			Set<String> indices = map.keySet();
			// 遍历索引
			for (String index : indices) {
//				System.out.println(index);
				// 获取索引setting
				GetSettingsRequest getSettings = new GetSettingsRequest().indices(index);
				GetSettingsResponse getSettingsResponse = client.indices().getSettings(getSettings,
						RequestOptions.DEFAULT);
				String indexShards = getSettingsResponse.getSetting(index, "index.number_of_shards");// 索引分片
				String indexReplicas = getSettingsResponse.getSetting(index, "index.number_of_replicas");// 索引副本

				// 获取索引mappings
				GetMappingsRequest getMappings = new GetMappingsRequest().indices(index);
				GetMappingsResponse getMappingResponse = client.indices().getMapping(getMappings,
						RequestOptions.DEFAULT);
				Map<String, MappingMetaData> allMappings = getMappingResponse.mappings();
				MappingMetaData indexMapping = allMappings.get(index);
				Map<String, Object> mapping = indexMapping.sourceAsMap();

				JSONObject json = new JSONObject(mapping);
				json.put("number_of_shards", indexShards);
				json.put("number_of_replicas", indexReplicas);

				JSONObject endJson = new JSONObject();
				endJson.put(index, json);
				System.out.println(endJson.toJSONString());
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	private static void createTest() {
		String wd = "test_wd";// 索引名称
		try {
			// 添加索引:带routing
			createIndex(wd, 6, 1, true);// 创建索引

			List<Map<String, String>> zdList = new ArrayList<Map<String, String>>();
			zdList.add(createFieldMap("company", "text", null)); // 公司主体
			zdList.add(createFieldMap("companyType", "keyword", null)); // keyword 不分词
			zdList.add(createFieldMap("updateTime", "long", null)); // 时间类型
			zdList.add(createFieldMap("description", "text", "false")); // 不索引该字段(不能用于检索)

			createType(wd, "_doc", zdList);// 添加mapping字段
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 创建索引
	 * 
	 * @param index    索引
	 * @param shards   分片数量
	 * @param replicas 副本数量
	 * @param routing  是否添加路由
	 * @throws IOException
	 */

	private static void createIndex(String index, int shards, int replicas, Boolean routing) throws IOException {
		if (shards < 6) {
			shards = 6;
		}
		if (replicas < 1) {
			replicas = 1;
		}
		RestHighLevelClient client = ESClient.getES7Client();

		// number_of_shards:数据分片数,默认6
		// number_of_replicas:数据备份数,只有1台机器设置为0
		Settings setting = Settings.builder().put("number_of_shards", shards).put("number_of_replicas", replicas)
				.build();
		CreateIndexRequest indexRequest = new CreateIndexRequest(index, setting);
		try {
			client.indices().create(indexRequest, RequestOptions.DEFAULT);
			if (routing) {
				setIndexRouting(index);
			}
		} catch (ResourceAlreadyExistsException e) {
			System.out.println("【Exception 此库已存在!!! 】");
		}

	}

	/**
	 * 给索引设置路由
	 * 
	 * @param indexName 索引名称
	 */
	public static void setIndexRouting(String indexName) {
		if (indexName == null)
			return;
		try {
			XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("_doc")
					.startObject("_routing").field("required", true).endObject().endObject().endObject();

			PutMappingRequest mapping = Requests.putMappingRequest(indexName).type("_doc").source(builder);
			ESClient.getES7Client().indices().putMapping(mapping, RequestOptions.DEFAULT);
		} catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	/**
	 * 添加mapping字段
	 * 
	 * @param indexName 索引名称
	 * @param typeName  默认_doc
	 * @param zdList    组装好的 字段集合
	 * @throws IOException
	 */
	private static void createType(String indexName, String typeName, List<Map<String, String>> zdList)
			throws IOException {
		if (indexName == null || typeName == null)
			return;
		if (zdList == null || zdList.size() == 0)
			return;
		XContentBuilder builder = null;
		try {
			builder = XContentFactory.jsonBuilder().startObject().startObject(typeName).startObject("properties");
			for (Map<String, String> map : zdList) {
				String code = (String) map.get("fieldCode");
				String fieldType = (String) map.get("fieldType");
				String storeType = (String) map.get("storeType");
				if (fieldType.equals("date")) {
					fieldType = "long";
				}
				builder.startObject(code);
				builder.field("type", fieldType);

				if (storeType != null && storeType.length() > 0) {
					builder.field("index", storeType);
				}
				builder.endObject();
			}
			builder.endObject();// start几个,end几个
			builder.endObject();
			builder.endObject();
		} catch (IOException e) {
			e.printStackTrace();
		}
		if (builder == null)
			return;

		PutMappingRequest mappingRequest = Requests.putMappingRequest(indexName).type(typeName).source(builder);
		ESClient.getES7Client().indices().putMapping(mappingRequest, RequestOptions.DEFAULT);
//		ESClient.getES7Client().close();
	}

	/**
	 * 组装mapping字段
	 * 
	 * @param fieldCode 字段名称
	 * @param fieldType 字段类型
	 * @param storeType 是否索引
	 * @return
	 */
	public static Map<String, String> createFieldMap(String fieldCode, String fieldType, String storeType) {
		Map<String, String> zdMap = new HashMap<String, String>();
		zdMap.put("fieldCode", fieldCode);// 字段名称
		zdMap.put("fieldType", fieldType);// 字段类型
		if (storeType != null) {
			zdMap.put("storeType", storeType);
		}
		return zdMap;
	}

	/**
	 * 给指定索引添加单个字段字段
	 * 
	 * @param index 索引
	 * @throws Exception
	 */
	private static void addField(String index) throws Exception {
		List<Map<String, String>> zdList = new ArrayList<Map<String, String>>();
		zdList.add(createFieldMap("eventType", "text", null));

		createType(index, "_doc", zdList);
	}

}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值