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();
}
/**
* 关闭索引
*
* @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();
}
/**
* 索引统计
*
* @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 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());
}
给索引设置别名等等。