ElasticSearch學習筆記之三十一 JAVA Client 之 GET APIs
阿新 • • 發佈:2018-11-06
ElasticSearch學習筆記之三十一 JAVA Client 之 GET APIs
Get API
Get Request
GetRequest
形如:
GetRequest getRequest = new GetRequest(
"posts" , //Index
"doc", //Type
"1"); //Document id
Optional arguments(引數配置)
可配置引數如下:
//禁用源檢索,預設開啟
request.fetchSourceContext(FetchSourceContext.DO_NOT_FETCH_SOURCE);
//配置source include欄位
String[] includes = new String[]{"message", "*Date"};
String[] excludes = Strings.EMPTY_ARRAY;
FetchSourceContext fetchSourceContext =
new FetchSourceContext(true, includes, excludes);
request.fetchSourceContext(fetchSourceContext);
配置source exclude欄位
String[] includes = Strings.EMPTY_ARRAY;
String[] excludes = new String[]{"message"};
FetchSourceContext fetchSourceContext =
new FetchSourceContext(true, includes, excludes);
request.fetchSourceContext(fetchSourceContext);
//配置檢索儲存欄位(需要在對映中儲存的欄位)
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);
//設定在檢索文件之前執行重新整理(預設情況下為false)
request.refresh(true);
//設定Version
request.version(2);
//Version type
request.versionType(VersionType.EXTERNAL);
Synchronous Execution(同步執行)
GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
Asynchronous Execution(非同步執行)
索引請求的非同步執行需要將GetRequest例項和ActionListener例項傳遞給非同步方法:
client.getAsync(request/*需要執行的GetRequest*/, RequestOptions.DEFAULT, listener/*執行完成之後的回撥*/);
非同步執行不會堵塞並且立即返回,一旦完成,如果執行成功完成,則使用onResponse方法回撥ActionListener,如果執行失敗,則使用onFailure方法回撥ActionListener。
典型的GetResponse
:
ActionListener<GetResponse> listener = new ActionListener<GetResponse>() {
//呼叫成功時回撥,返回資訊作為引數傳入
@Override
public void onResponse(GetResponse getResponse) {
}
//呼叫失敗時回撥,錯誤資訊作為引數傳入
@Override
public void onFailure(Exception e) {
}
};
Get Response
從GetResponse
獲取返回的響應資訊方式如下:
String index = getResponse.getIndex();
String type = getResponse.getType();
String id = getResponse.getId();
if (getResponse.isExists()) {
long version = getResponse.getVersion();
//獲取字串響應
String sourceAsString = getResponse.getSourceAsString();
//獲取 Map<String, Object>響應
Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
//獲取byte[]響應
byte[] sourceAsBytes = getResponse.getSourceAsBytes();
} else {
}
如果文件沒有找到,它會返回一個帶有404響應碼的正常GetResponse
而不是丟擲異常. 這樣的響應不包含source
,它的isExists
方法返回false
;
當在一個不存在的索引上操作的時候, 會返回 404 響應碼和下面的 ElasticsearchException
:
GetRequest request = new GetRequest("does_not_exist", "doc", "1");
try {
GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
} catch (ElasticsearchException e) {
//文件不存在
if (e.status() == RestStatus.NOT_FOUND) {
}
}
假如訪問指定版本的文件,但是文件有一個不同的版本則會出現版本衝突異常:
try {
GetRequest request = new GetRequest("posts", "doc", "1").version(2);
GetResponse getResponse = client.get(request, RequestOptions.DEFAULT);
} catch (ElasticsearchException exception) {
//版本衝突
if (exception.status() == RestStatus.CONFLICT) {
}
}