elasticsearch-java api之索引(index)的各种操作

17 篇文章 3 订阅

1、创建索引:

1)简单索引——没有指定mapping

public static boolean createIndex(String indexName) {
		IndicesAdminClient indicesAdminClient = transportClient.admin()
				.indices();
		CreateIndexResponse response = indicesAdminClient.prepareCreate(
				indexName).get();
		return response.isAcknowledged();
	}
由于es是无模式的数据库,所以可以不指定mapping,按照插入数据的类型根据默认条件去索引。

2)复杂索引:

/**
	 * 创建复杂索引(类型/mapping)
	 * 
	 * @param indexName
	 *            索引名
	 * @param indexType
	 *            索引类型名
	 * @param mappingSource
	 *            索引mapping
	 */
	public static boolean createIndex(String indexName, String indexType,
			String mappingSource) {

		if (isExistsIndex(indexName)) {
			return false;
		}
		// setting
		Settings settings = Settings.builder().put("index.number_of_shards", 3)
				.put("index.number_of_replicas", 2).build();
		// mapping

		IndicesAdminClient indicesAdminClient = transportClient.admin()
				.indices();
		CreateIndexResponse response = indicesAdminClient
				.prepareCreate(indexName).setSettings(settings)// setting
				.addMapping(indexType, mappingSource)// type,mapping
				.get();
		return response.isAcknowledged();
	}

指定索引的setting和mapping,其中mappingSource是一个json数据字符串。

2、设置索引类型和mapping:

/**
	 * 设置映射 在指定索引上一次性创建或修改一到多个索引的映射,指定的索引必须存在否则报错。 可以结合创建简单索引,然后在设置映射。
	 * 
	 * @param index
	 * @param type
	 * @return
	 */
	public static boolean putIndexMapping(String index, String type) {
		// mapping
		XContentBuilder mappingBuilder;
		try {
			mappingBuilder = XContentFactory.jsonBuilder().startObject()
					.startObject(type).startObject("properties")
					.startObject("name").field("type", "string")
					.field("store", "yes").endObject().startObject("sex")
					.field("type", "string").field("store", "yes").endObject()
					.startObject("college").field("type", "string")
					.field("store", "yes").endObject().startObject("age")
					.field("type", "long").field("store", "yes").endObject()
					.startObject("school").field("type", "string")
					.field("store", "yes").field("index", "not_analyzed")
					.endObject().endObject().endObject().endObject();
		} catch (Exception e) {
			return false;
		}
		IndicesAdminClient indicesAdminClient = transportClient.admin()
				.indices();
		PutMappingResponse response = indicesAdminClient
				.preparePutMapping(index).setType(type)
				.setSource(mappingBuilder).get();
		return response.isAcknowledged();
	}
一般来说,可以先通过简单索引创建一个索引,然后在设置该索引的类型和mapping。

3、判断索引、索引类型是否存在:

/**
	 * 判断指定的索引名是否存在
	 * 
	 * @param indexName
	 *            索引名
	 * @return 存在:true; 不存在:false;
	 */
	public static boolean isExistsIndex(String indexName) {
		IndicesExistsResponse response = transportClient
				.admin()
				.indices()
				.exists(new IndicesExistsRequest()
						.indices(new String[] { indexName })).actionGet();
		return response.isExists();
	}

	/**
	 * 判断指定的索引的类型是否存在
	 * 
	 * @param indexName
	 *            索引名
	 * @param indexType
	 *            索引类型
	 * @return 存在:true; 不存在:false;
	 */
	public static boolean isExistsType(String indexName, String indexType) {
		TypesExistsResponse response = transportClient
				.admin()
				.indices()
				.typesExists(
						new TypesExistsRequest(new String[] { indexName },
								indexType)).actionGet();
		return response.isExists();
	}
4、删除索引:

/**
	 * 删除索引
	 * 
	 * @param indexName
	 *            索引名
	 * @return
	 */
	public static boolean deleteIndex(String indexName) {
		IndicesAdminClient indicesAdminClient = transportClient.admin()
				.indices();
		DeleteIndexResponse response = indicesAdminClient
				.prepareDelete(indexName).execute().actionGet();
		return response.isAcknowledged();
	}

5、打开、关闭索引:

/**
	 * 关闭索引
	 * 
	 * @param index
	 * @return
	 */
	public static boolean closeIndex(String index) {
		IndicesAdminClient indicesAdminClient = transportClient.admin()
				.indices();
		CloseIndexResponse response = indicesAdminClient.prepareClose(index)
				.get();
		return response.isAcknowledged();
	}

	/**
	 * 打开索引
	 * 
	 * @param index
	 * @return
	 */
	public static boolean openIndex(String index) {
		IndicesAdminClient indicesAdminClient = transportClient.admin()
				.indices();
		OpenIndexResponse response = indicesAdminClient.prepareOpen(index)
				.get();
		return response.isAcknowledged();
	}

6、统计索引:

/**
	 * 索引统计
	 * 
	 * @param client
	 * @param index
	 */
	public static void indexStats(String index) {
		IndicesAdminClient indicesAdminClient = transportClient.admin()
				.indices();
		IndicesStatsResponse response = indicesAdminClient.prepareStats(index)
				.all().get();
		ShardStats[] shardStatsArray = response.getShards();
		for (ShardStats shardStats : shardStatsArray) {
			logger.info("shardStats {}", shardStats.toString());
		}
		Map<String, IndexStats> indexStatsMap = response.getIndices();
		for (String key : indexStatsMap.keySet()) {
			logger.info("indexStats {}", indexStatsMap.get(key));
		}
		CommonStats commonStats = response.getTotal();
		logger.info("total commonStats {}", commonStats.toString());
		commonStats = response.getPrimaries();
		logger.info("primaries commonStats {}", commonStats.toString());
	}

7、其他:

给索引设置别名等等。










评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

赶路人儿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值