Get API

获取请求对象

GetRequest 需要以下参数:

  1. GetRequest getRequest = new GetRequest(
  2. "posts", // 索引
  3. "doc", // 类别
  4. "1"); // 文档id

可选参数

提供以下可选参数:

  1. request.fetchSourceContext(new FetchSourceContext(false)); //禁用检索源,默认为启用

String[] includes = new String[]{“message”, “*Date”};
String[] excludes = Strings.EMPTY_ARRAY;
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
request.fetchSourceContext(fetchSourceContext); //设置源包含的特定域

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.storedFields(“message”); //Configure retrieval for specific stored fields (requires fields to be stored separately in the mappings)
GetResponse getResponse = client.get(request);
String message = (String) getResponse.getField(“message”).getValue(); //Retrieve the message stored field (requires the field to be stored separately in the mappings)

  1. ```
  2. request.routing("routing"); //Routing value
  3. request.parent("parent"); //Parent value
  4. request.preference("preference"); //Preference value
  5. request.realtime(false); //Set realtime flag to false (true by default)
  6. request.refresh(true); //Perform a refresh before retrieving the document (false by default)
  7. request.version(2); //Version
  8. request.versionType(VersionType.EXTERNAL); //Version type

同步执行

  1. GetResponse getResponse = client.get(getRequest);

异步执行

  1. client.getAsync(request, new ActionListener<GetResponse>() {
  2. @Override
  3. public void onResponse(GetResponse getResponse) {
  4. //Called when the execution is successfully completed. The response is provided as an argument.
  5. }
  6. @Override
  7. public void onFailure(Exception e) {
  8. //Called in case of failure. The raised exception is provided as an argument.
  9. }
  10. });

获取响应

The returned GetResponse allows to retrieve the requested document along with its metadata and eventually stored fields.

  1. String index = getResponse.getIndex();
  2. String type = getResponse.getType();
  3. String id = getResponse.getId();
  4. if (getResponse.isExists()) {
  5. long version = getResponse.getVersion();
  6. String sourceAsString = getResponse.getSourceAsString(); //Retrieve the document as a String
  7. Map<String, Object> sourceAsMap = getResponse.getSourceAsMap(); //Retrieve the document as a Map<String, Object>
  8. byte[] sourceAsBytes = getResponse.getSourceAsBytes(); //Retrieve the document as a byte[]
  9. } else {
  10. //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.
  11. }

当针对不存在的索引执行获取请求时,响应有404状态码,抛出一个 ElasticsearchException 异常,需要如下处理:

  1. GetRequest request = new GetRequest("does_not_exist", "doc", "1");
  2. try {
  3. GetResponse getResponse = client.get(request);
  4. } catch (ElasticsearchException e) {
  5. if (e.status() == RestStatus.NOT_FOUND) {
  6. // 处理因为索引不存在而抛出的异常,
  7. }
  8. }

如果请求了特定文档版本,但现有文档具有不同的版本号,则会引发版本冲突:

  1. try {
  2. GetRequest request = new GetRequest("posts", "doc", "1").version(2);
  3. GetResponse getResponse = client.get(request);
  4. } catch (ElasticsearchException exception) {
  5. if (exception.status() == RestStatus.CONFLICT) {
  6. //表示返回了版本冲突错误引发异常
  7. }
  8. }