package sss.first;
import java.io.IOException;
import java.util.Map;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.Strings;
import org.elasticsearch.index.VersionType;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
public class GetApi {
//get api
// get request
public static GetRequest getRequest() {
GetRequest getRequest = new GetRequest(
"posts",
"doc",
"1");
return getRequest;
}
// optional parameters
public static void optional(RestHighLevelClient client,GetRequest request) throws IOException {
//
Disable source retrieval, enabled by default
request.fetchSourceContext(new FetchSourceContext(false));
//
Configure source inclusion for specific fields
String[] includes = new String[]{"message", "*Date"};
String[] excludes = Strings.EMPTY_ARRAY;
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
request.fetchSourceContext(fetchSourceContext);
//
Configure source exclusion for specific fields
String[] includes1 = Strings.EMPTY_ARRAY;
String[] excludes1 = new String[]{"message"};
FetchSourceContext fetchSourceContext1 = new FetchSourceContext(true, includes1, excludes1);
request.fetchSourceContext(fetchSourceContext1);
//
Configure the retrieval of specific storage fields (requires fields to be stored separately in the map)
//
Retrieve the fields stored by the message (requires the fields to be stored separately in the map)
request.storedFields("message");
GetResponse getResponse = client.get(request);
String message = (String) getResponse.getField("message").getValue();
//
route value
request.routing("routing");
//
value of parent
request.parent("parent");
//
preference value
request.preference("preference");
//
Set the real-time flag to false (true default)
request.realtime(false);
//
perform a refresh before retrieving the document (false by default)
request.refresh(true);
//
version number
request.version(2);
//
version type
request.versionType(VersionType.EXTERNAL);
}
// execute synchronously
public static GetResponse synchronous(RestHighLevelClient client,GetRequest getRequest) throws IOException {
GetResponse getResponse = client.get(getRequest);
return getResponse;
}
public static GetResponse asynchronous(RestHighLevelClient client,GetRequest getRequest) throws IOException {
client.getAsync(getRequest, new ActionListener<GetResponse>() {
@Override
public void onResponse(GetResponse response) {
// Called when execution completes successfully. The reply is provided as a parameter.
}
@Override
public void onFailure(Exception e) {
// Called on failure. The exception thrown is provided as a parameter.
}
});
return null;
}
// get response
public static void getResponse(GetResponse getResponse) {
String index = getResponse.getIndex();
String type = getResponse.getType();
String id = getResponse.getId();
if (getResponse.isExists()) {
long version = getResponse.getVersion();
// Retrieve the document String as a file
String sourceAsString = getResponse.getSourceAsString();
//Retrieve document as file Map<String, Object>
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
//Retrieve document byte[] as file
byte[] sourceAsBytes = getResponse.getSourceAsBytes();
} else {
//Handle the case where the document is not found. Note that while the returned response has a 404 status code,
// but returns a valid value GetResponse instead of throwing an exception.
// Such responses do not contain any source files and their isExists method returns false.
}
}
// When a fetch request is performed for a non-existent index, the response has a 404 status code,
//ElasticsearchException is thrown and needs to be handled as follows:
public static void tryException(RestHighLevelClient client,GetRequest request) throws IOException {
request = new GetRequest("does_not_exist", "doc", "1");
try {
GetResponse getResponse = client.get(request);
} catch (ElasticsearchException e) {
if (e.status() == RestStatus.NOT_FOUND) {
//handle the exception thrown because the index does not exist
}
}
}
//
If a specific document version is requested, and the existing document has a different version number, a version conflict is raised:
public static void tryException2(RestHighLevelClient client,GetRequest request) throws IOException {
try {
request = new GetRequest("posts", "doc", "1").version(2);
GetResponse getResponse = client.get(request);
} catch (ElasticsearchException exception) {
if (exception.status() == RestStatus.CONFLICT) {
//The exception raised indicates that a version conflict error was returned
}
}
}
}