Get API
提供以下可选参数:
String[] includes = Strings.EMPTY_ARRAY;
String[] excludes = new String[]{“message”};
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
request.fetchSourceContext(fetchSourceContext);
Configure source exclusion for specific fields
```
request.routing("routing"); //Routing value
request.parent("parent"); //Parent value
request.realtime(false); //Set realtime flag to false (true by default)
request.refresh(true); //Perform a refresh before retrieving the document (false by default)
request.version(2); //Version
GetResponse getResponse = client.get(getRequest);
获取响应
The returned GetResponse allows to retrieve the requested document along with its metadata and eventually stored fields.
String index = getResponse.getIndex();
String type = getResponse.getType();
String id = getResponse.getId();
if (getResponse.isExists()) {
long version = getResponse.getVersion();
String sourceAsString = getResponse.getSourceAsString(); //Retrieve the document as a String
byte[] sourceAsBytes = getResponse.getSourceAsBytes(); //Retrieve the document as a byte[]
//Handle the scenario where the document was not found. Note that although the returned response has 404 status code, a valid GetResponse is returned rather than an exception thrown. Such response does not hold any source document and its isExists method returns false.
}
GetRequest request = new GetRequest("does_not_exist", "doc", "1");
try {
GetResponse getResponse = client.get(request);
} catch (ElasticsearchException e) {
if (e.status() == RestStatus.NOT_FOUND) {
// 处理因为索引不存在而抛出的异常,
}
如果请求了特定文档版本,但现有文档具有不同的版本号,则会引发版本冲突: