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

    1. ```
    2. request.routing("routing"); //Routing value
    3. request.parent("parent"); //Parent value
    4. request.realtime(false); //Set realtime flag to false (true by default)
    5. request.refresh(true); //Perform a refresh before retrieving the document (false by default)
    6. request.version(2); //Version
    1. GetResponse getResponse = client.get(getRequest);

    获取响应

    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. byte[] sourceAsBytes = getResponse.getSourceAsBytes(); //Retrieve the document as a byte[]
    8. //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.
    9. }
    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. }

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