Index API
索引請求
IndexRequest
需要以下參數:
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
示例之外,還可以以不同的方式提供文檔源:
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");
- Parent值。
request.timeout(TimeValue.timeValueSeconds(1));
request.timeout("1s");
- 等待主碎片作爲
TimeValue
的可用超時。 - 等待主碎片以字符串形式提供的超時。
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
:
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
異步執行
執行IndexRequest
也可以以異步方式完成,以便客戶端可以直接返回,用戶需要通過將請求和偵聽器傳遞給異步索引方法來指定響應或潛在故障的處理方式:
client.indexAsync(request, RequestOptions.DEFAULT, listener);
- 要執行的
IndexRequest
和執行完成時要使用的ActionListener
。
異步方法不會阻塞並立即返回,一旦完成,如果執行成功完成,則使用onResponse
方法回調ActionListener
,如果失敗則使用onFailure
方法。
index
的典型偵聽器如下所示:
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的文檔,則會發生相同的情況:
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) {
}
}
- 引發的異常表示返回了版本衝突錯誤。