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) { } }