elasticsearch-java api之索引(index)的各種操作
阿新 • • 發佈:2019-01-27
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:
一般來說,可以先通過簡單索引建立一個索引,然後在設定該索引的型別和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(); }
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、其他:
給索引設定別名等等。