Elastic Search Learning Notes 29 GET APIs of JAVA Client
Get API
Get Request
GetRequest is like:
GetRequest getRequest = new GetRequest( "posts", //Index "doc", //Type "1"); //Document id
Optional arguments
The configurable parameters are as follows:
//Disable Source Retrieval and Open by Default request.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
//Configure the source include field String[] includes = new String[]{"message", "*Date"}; String[] excludes = Strings.EMPTY_ARRAY; FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes); request.fetchSourceContext(fetchSourceContext);
To configure source exclude field String[] includes = Strings.EMPTY_ARRAY; String[] excludes = new String[]{"message"}; FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes); request.fetchSourceContext(fetchSourceContext);
//Configure retrieve storage fields (fields that need to be stored in the mapping) request.storedFields("message"); GetResponse getResponse = client.get(request, RequestOptions.DEFAULT); String message = getResponse.getField("message").getValue();
//Routing value request.routing("routing");
//Parent value request.parent("parent");
//Preference value request.preference("preference");
//Set realtime flag to false (true by default) request.realtime(false);
//Set to refresh before retrieving documents (false by default) request.refresh(true);
//Setting Version request.version(2);
//Version type request.versionType(VersionType.EXTERNAL);
Synchronous Execution
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
Asynchronous Execution
Asynchronous execution of index requests requires passing the GetRequest instance and ActionListener instance to the asynchronous method:
client.getAsync(request/*GetRequest to be executed*/, RequestOptions.DEFAULT, listener/*Callback after completion of execution*/);
Asynchronous execution does not block and returns immediately. Once completed, if the execution is successful, the onResponse method is used to call back the ActionListener, and if the execution fails, the onFailure method is used to call back the ActionListener.
Typical GetResponse:
ActionListener<GetResponse> listener = new ActionListener<GetResponse>() { //Callback when the call is successful and return information as a parameter @Override public void onResponse(GetResponse getResponse) { } //Callback when call fails, error message is passed in as parameter @Override public void onFailure(Exception e) { } };
Get Response
The response information returned from GetResponse is as follows:
String index = getResponse.getIndex(); String type = getResponse.getType(); String id = getResponse.getId(); if (getResponse.isExists()) { long version = getResponse.getVersion(); //Get string response String sourceAsString = getResponse.getSourceAsString(); //Get Map < String, Object > response Map<String, Object> sourceAsMap = getResponse.getSourceAsMap(); //Get byte [] response byte[] sourceAsBytes = getResponse.getSourceAsBytes(); } else { }
If the document is not found, it returns a normal GetResponse with a 404 response code instead of throwing an exception. Such a response does not contain a source, and its isExists method returns false.
When operating on a non-existent index, the 404 response code and the following Elastic search Exception are returned:
GetRequest request = new GetRequest("does_not_exist", "doc", "1"); try { GetResponse getResponse = client.get(request, RequestOptions.DEFAULT); } catch (ElasticsearchException e) { //Document does not exist if (e.status() == RestStatus.NOT_FOUND) { } }
If you access a specified version of a document, but the document has a different version, a version conflict exception occurs:
try { GetRequest request = new GetRequest("posts", "doc", "1").version(2); GetResponse getResponse = client.get(request, RequestOptions.DEFAULT); } catch (ElasticsearchException exception) { //Version conflict if (exception.status() == RestStatus.CONFLICT) { } }