管理索引接口
创建索引
管理索引接口 可以通过默认设置和无设置来创建索引。
client.admin().indices().prepareCreate("twitter").get();
设置映射
设置映射接口可以在创建索引的时候添加一个新的type:
client.admin().indices()
//创建一个名字叫twitter的索引
.prepareCreate("twitter")
//同时会增加一个名为tweet的type
.addMapping("\"tweet\": {\n" +
" \"properties\": {\n" +
" \"message\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}")
.get();
设置映射接口还允许想现有的索引中添加新字段:
client.admin().indices()
//修改已经存在的名为twitter的索引
.preparePutMapping("twitter")
//创建一个名为user的type
.setType("user")
//user具有预定义的类型
.setSource("{\n" +
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON)
.get();
// You can also provide the type in the source document
client.admin().indices().preparePutMapping("twitter")
.setType("user")
.setSource("{\n" +
//type也可以在source中指定
" \"user\":{\n" +
" \"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON)
.get();
也可以修改已经存在的映射:
client.admin().indices().preparePutMapping("twitter")
.setType("user")
.setSource("{\n" +
" \"properties\": {\n" +
//新字段user_name
" \"user_name\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
"}", XContentType.JSON)
.get();
显式刷新
刷新接口允许显式地刷新一个或多个索引:
//刷新所有索引
client.admin().indices().prepareRefresh().get();
//刷新一个索引
client.admin().indices()
.prepareRefresh("twitter")
.get();
//刷新多个索引
client.admin().indices()
.prepareRefresh("twitter", "company")
.get();
获取设置
获取映射接口允许查询索引的设置:
GetSettingsResponse response = client.admin().indices()
//获取company和employee的设置
.prepareGetSettings("company", "employee").get();
//遍历结果
for (ObjectObjectCursor<String, Settings> cursor : response.getIndexToSettings()) {
//索引名
String index = cursor.key;
//索引的设置
Settings settings = cursor.value;
//索引的分片数
Integer shards = settings.getAsInt("index.number_of_shards", null);
//索引的副本数
Integer replicas = settings.getAsInt("index.number_of_replicas", null);
}