Elasticsearch 7 Java High Level REST Client 创建索引,完整例子请参考最后的Test用例
CreateIndexRequest request = new CreateIndexRequest("twitter");
Each index created can have specific settings associated with it.
request.settings(Settings.builder() .put("index.number_of_shards", 3) .put("index.number_of_replicas", 2) );
request.mapping( "{\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + "}", XContentType.JSON);
The mapping source can be provided in different ways in addition to the String
example shown above:
Mapmessage = new HashMap<>(); message.put("type", "text"); Map properties = new HashMap<>(); properties.put("message", message); Map mapping = new HashMap<>(); mapping.put("properties", properties); request.mapping(mapping);
XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.startObject("properties"); { builder.startObject("message"); { builder.field("type", "text"); } builder.endObject(); } builder.endObject(); } builder.endObject(); request.mapping(builder);
Aliases can be set at index creation time
request.alias(new Alias("twitter_alias").filter(QueryBuilders.termQuery("user", "kimchy")));
The whole source including all of its sections (mappings, settings and aliases) can also be provided:
request.source("{\n" + " \"settings\" : {\n" + " \"number_of_shards\" : 1,\n" + " \"number_of_replicas\" : 0\n" + " },\n" + " \"mappings\" : {\n" + " \"properties\" : {\n" + " \"message\" : { \"type\" : \"text\" }\n" + " }\n" + " },\n" + " \"aliases\" : {\n" + " \"twitter_alias\" : {}\n" + " }\n" + "}", XContentType.JSON);
The following arguments can optionally be provided:
request.setTimeout(TimeValue.timeValueMinutes(2)); request.setMasterTimeout(TimeValue.timeValueMinutes(1)); request.waitForActiveShards(ActiveShardCount.from(2)); request.waitForActiveShards(ActiveShardCount.DEFAULT);
When executing a CreateIndexRequest
in the following manner, the client waits for the CreateIndexResponse
to be returned before continuing with code execution:
CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT);
Synchronous calls may throw an IOException
in case of either failing to parse the REST response in the high-level REST client, the request times out or similar cases where there is no response coming back from the server.
In cases where the server returns a 4xx
or 5xx
error code, the high-level client tries to parse the response body error details instead and then throws a generic ElasticsearchException
and adds the original ResponseException
as a suppressed exception to it.
@Test public void testCreateIndex() throws IOException { CreateIndexRequest request = new CreateIndexRequest("twitter_xx"); request.settings(Settings.builder().put("index.number_of_shards", 3).put("index.number_of_replicas", 2)); String json = "{\r\n" + " \"properties\" : {\r\n" + " \"author\" : {\r\n" + " \"type\" : \"text\",\r\n" + " \"fields\" : {\r\n" + " \"keyword\" : {\r\n" + " \"type\" : \"keyword\",\r\n" + " \"ignore_above\" : 256\r\n" + " }\r\n" + " }\r\n" + " },\r\n" + " \"comments\" : {\r\n" + " \"type\": \"nested\", \r\n" + " \"properties\" : {\r\n" + " \"age\" : {\r\n" + " \"type\" : \"long\"\r\n" + " },\r\n" + " \"comment\" : {\r\n" + " \"type\" : \"text\",\r\n" + " \"fields\" : {\r\n" + " \"keyword\" : {\r\n" + " \"type\" : \"keyword\",\r\n" + " \"ignore_above\" : 256\r\n" + " }\r\n" + " }\r\n" + " },\r\n" + " \"date\" : {\r\n" + " \"type\" : \"long\"\r\n" + " },\r\n" + " \"name\" : {\r\n" + " \"type\" : \"text\",\r\n" + " \"fields\" : {\r\n" + " \"keyword\" : {\r\n" + " \"type\" : \"keyword\",\r\n" + " \"ignore_above\" : 256\r\n" + " }\r\n" + " }\r\n" + " },\r\n" + " \"stars\" : {\r\n" + " \"type\" : \"long\"\r\n" + " }\r\n" + " }\r\n" + " },\r\n" + " \"keyword\" : {\r\n" + " \"type\" : \"text\",\r\n" + " \"fields\" : {\r\n" + " \"keyword\" : {\r\n" + " \"type\" : \"keyword\",\r\n" + " \"ignore_above\" : 256\r\n" + " }\r\n" + " }\r\n" + " },\r\n" + " \"title\" : {\r\n" + " \"type\" : \"text\",\r\n" + " \"fields\" : {\r\n" + " \"keyword\" : {\r\n" + " \"type\" : \"keyword\",\r\n" + " \"ignore_above\" : 256\r\n" + " }\r\n" + " }\r\n" + " }\r\n" + " }\r\n" + " }"; request.mapping(json, XContentType.JSON); CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); System.out.println("-----------createIndexResponse-----------" + createIndexResponse.toString());
}
完整代码可参考:https://github.com/hsn999/Elasticsearch_7_springboot_demo