IndexRequest
须要如下参数:json
IndexRequest request = new IndexRequest( "posts", "doc", "1"); String jsonString = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-30\"," + "\"message\":\"trying out Elasticsearch\"" + "}"; request.source(jsonString, XContentType.JSON);
posts
— 索引。doc
— 类型。1
— 文档ID。除了上面显示的String
示例以外,还能够以不一样的方式提供文档源:segmentfault
Map<String, Object> jsonMap = new HashMap<>(); jsonMap.put("user", "kimchy"); jsonMap.put("postDate", new Date()); jsonMap.put("message", "trying out Elasticsearch"); IndexRequest indexRequest = new IndexRequest("posts", "doc", "1") .source(jsonMap);
Map
提供,可自动转换为JSON格式。XContentBuilder builder = XContentFactory.jsonBuilder(); builder.startObject(); { builder.field("user", "kimchy"); builder.timeField("postDate", new Date()); builder.field("message", "trying out Elasticsearch"); } builder.endObject(); IndexRequest indexRequest = new IndexRequest("posts", "doc", "1") .source(builder);
XContentBuilder
对象提供,Elasticsearch内置辅助生成JSON内容。IndexRequest indexRequest = new IndexRequest("posts", "doc", "1") .source("user", "kimchy", "postDate", new Date(), "message", "trying out Elasticsearch");
Object
键值对提供,转换为JSON格式。能够选择提供如下参数:异步
request.routing("routing");
request.parent("parent");
request.timeout(TimeValue.timeValueSeconds(1)); request.timeout("1s");
TimeValue
的超时。String
的超时。request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); request.setRefreshPolicy("wait_for");
WriteRequest.RefreshPolicy
实例提供。String
提供。request.version(2);
request.versionType(VersionType.EXTERNAL);
request.opType(DocWriteRequest.OpType.CREATE); request.opType("create");
DocWriteRequest.OpType
值提供。String
提供的操做类型:能够为create
或update
(默认)。request.setPipeline("pipeline");
如下列方式执行IndexRequest
时,客户端在继续执行代码以前等待返回IndexResponse
:ide
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
执行IndexRequest
也能够以异步方式完成,以便客户端能够直接返回,用户须要经过将请求和侦听器传递给异步索引方法来指定响应或潜在故障的处理方式:post
client.indexAsync(request, RequestOptions.DEFAULT, listener);
IndexRequest
和执行完成时要使用的ActionListener
。异步方法不会阻塞并当即返回,一旦完成,若是执行成功完成,则使用onResponse
方法回调ActionListener
,若是失败则使用onFailure
方法。ui
index
的典型侦听器以下所示:code
listener = new ActionListener<IndexResponse>() { @Override public void onResponse(IndexResponse indexResponse) { } @Override public void onFailure(Exception e) { } };
onResponse
— 执行成功完成时调用。onFailure
— 当整个IndexRequest
失败时调用。返回的IndexResponse
容许检索有关已执行操做的信息,以下所示:对象
String index = indexResponse.getIndex(); String type = indexResponse.getType(); String id = indexResponse.getId(); long version = indexResponse.getVersion(); if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) { } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) { } ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo(); if (shardInfo.getTotal() != shardInfo.getSuccessful()) { } if (shardInfo.getFailed() > 0) { for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) { String reason = failure.reason(); } }
若是存在版本冲突,则抛出ElasticsearchException
:索引
IndexRequest request = new IndexRequest("posts", "doc", "1") .source("field", "value") .version(1); try { IndexResponse response = client.index(request, RequestOptions.DEFAULT); } catch(ElasticsearchException e) { if (e.status() == RestStatus.CONFLICT) { } }
若是将opType
设置为create
而且已存在具备相同索引、类型和ID的文档,则会发生相同的状况:ip
IndexRequest request = new IndexRequest("posts", "doc", "1") .source("field", "value") .opType(DocWriteRequest.OpType.CREATE); try { IndexResponse response = client.index(request, RequestOptions.DEFAULT); } catch(ElasticsearchException e) { if (e.status() == RestStatus.CONFLICT) { } }