目录
1.为了访问索引信息,使用adminClient
IndicesAdminClient indicesAdminClient = client.admin().indices();
2.创建索引
client.admin().indices().prepareCreate("twitter").get();
3. 设置索引参数
client.admin().indices().prepareCreate("twitter")
.setSettings(Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2)
)
.get();
4.PUT MAPPING
client.admin().indices().prepareCreate("twitter")
.addMapping("_doc", "message", "type=text")
.get();
Creates an index called twitter
Add a _doc type with a field called message that has the datatype text.
关于addMapping方法有很多种形式.可以使用 XContentBuilder
或者 Map
参数.具体的请参考使用版本的javadoc来选择最适合你的方法。
PUT mapping也可以用于更新已经创建的索引。如:
client.admin().indices().preparePutMapping("twitter")
.setType("_doc")
.setSource("{
" +
" "properties": {
" +
" "name": {
" +
" "type": "text"
" +
" }
" +
" }
" +
"}", XContentType.JSON)
.get();
// You can also provide the type in the source document
client.admin().indices().preparePutMapping("twitter")
.setType("_doc")
.setSource("{
" +
" "_doc":{
" +
" "properties": {
" +
" "name": {
" +
" "type": "text"
" +
" }
" +
" }
" +
" }
" +
"}", XContentType.JSON)
.get();
Puts a mapping on existing index called twitter
Adds a new field name to the mapping
The type can be also provided within the source
5. Refresh刷新
// 1.刷新所有索引
client.admin().indices().prepareRefresh().get();
//2.刷新一个索引
client.admin().indices()
.prepareRefresh("twitter")
.get();
//3. 刷新多个索引
client.admin().indices()
.prepareRefresh("twitter", "company")
.get();
6.获取已存在的索引信息Settings
GetSettingsResponse response = client.admin().indices()
.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);
}
Get settings for indices company and employee
Iterate over results
Index name
Settings for the given index
Number of shards for this index
Number of replicas for this index
7.更新索引Settings
client.admin().indices().prepareUpdateSettings("twitter")
.setSettings(Settings.builder()
.put("index.number_of_replicas", 0)
)
.get();