- Java REST Client (deprecated): other versions:
- Overview
- Java Low Level REST Client
- Java High Level REST Client
- Getting started
- Document APIs
- Search APIs
- Miscellaneous APIs
- Indices APIs
- Analyze API
- Create Index API
- Delete Index API
- Indices Exists API
- Open Index API
- Close Index API
- Shrink Index API
- Split Index API
- Refresh API
- Flush API
- Flush Synced API
- Clear Cache API
- Force Merge API
- Rollover Index API
- Put Mapping API
- Get Mappings API
- Get Field Mappings API
- Index Aliases API
- Exists Alias API
- Get Alias API
- Update Indices Settings API
- Get Settings API
- Put Template API
- Validate Query API
- Get Templates API
- Get Index API
- Cluster APIs
- Ingest APIs
- Snapshot APIs
- Tasks APIs
- Script APIs
- Watcher APIs
- Using Java Builders
- Migration Guide
- License
Index API
editIndex API
editIndex Request
editAn IndexRequest
requires the following arguments:
Providing the document source
editThe document source can be provided in different ways in addition to the
String
example shown above:
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);
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);
Document source provided as an |
Optional arguments
editThe following arguments can optionally be provided:
Timeout to wait for primary shard to become available as a |
|
Timeout to wait for primary shard to become available as a |
request.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL); request.setRefreshPolicy("wait_for");
Operation type provided as an |
|
Operation type provided as a |
Synchronous Execution
editIndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
Asynchronous Execution
editThe asynchronous execution of an index request requires both the IndexRequest
instance and an ActionListener
instance to be passed to the asynchronous
method:
The asynchronous method does not block and returns immediately. Once it is
completed the ActionListener
is called back using the onResponse
method
if the execution successfully completed or using the onFailure
method if
it failed.
A typical listener for IndexResponse
looks like:
Index Response
editThe returned IndexResponse
allows to retrieve information about the executed
operation as follows:
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(); } }
Handle (if needed) the case where the document was created for the first time |
|
Handle (if needed) the case where the document was rewritten as it was already existing |
|
Handle the situation where number of successful shards is less than total shards |
|
Handle the potential failures |
If there is a version conflict, an ElasticsearchException
will
be thrown:
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) { } }
Same will happen in case opType
was set to create
and a document with
same index, type and id already existed:
On this page