Indices Administration
editIndices Administration
editTo access indices Java API, you need to call indices()
method from an AdminClient
:
IndicesAdminClient indicesAdminClient = client.admin().indices();
In the rest of this guide, we will use client.admin().indices()
.
Create Index
editUsing an IndicesAdminClient
, you can create an index with all default settings and no mapping:
client.admin().indices().prepareCreate("twitter").get();
Index Settings
editEach index created can have specific settings associated with it.
Put Mapping
editYou can add mappings for a new type at index creation time:
client.admin().indices().prepareCreate("twitter") .addMapping("tweet", "message", "type=text") .get();
Creates an index called |
|
Add a |
There are several variants of the above addMapping
method, some taking an
XContentBuilder
or a Map
with the mapping definition as arguments. Make sure
to check the javadocs to pick the simplest one for your use case.
The PUT mapping API also allows to specify the mapping of a type after index creation. In this case you can provide the mapping as a String similar to the Rest API syntax:
client.admin().indices().preparePutMapping("twitter") .setType("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" + " \"user\":{\n" + " \"properties\": {\n" + " \"name\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + " }\n" + "}", XContentType.JSON) .get();
Puts a mapping on existing index called |
|
Adds a |
|
This |
|
type can be also provided within the source |
You can use the same API to update an existing mapping:
Refresh
editThe refresh API allows to explicitly refresh one or more index:
Get Settings
editThe get settings API allows to retrieve settings of index/indices:
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); }
Update Indices Settings
editYou can change index settings by calling: