這裡分類和彙總了欣宸的全部原創(含配套原始碼):https://github.com/zq2599/blog_demos
在REST API檔案中,數量眾多API是按照特性(feature)來分組的,如下圖
在ES的Java庫Java API Client中,上圖中的各種feature被稱為namespace
在ES的Java庫Java API Client中,與REST API對應的的類和介面都在統一的包名co.elastic.clients.elasticsearch之下,然後再通過下一級package進行分類,這個分類與上圖的feature相對應,例如索引相關的,在REST API中的feature是Index APIs,那麼在Java API Client中,完整的package就是co.elastic.clients.elasticsearch.indices,這裡面有索引操作所需的請求、響應、服務等各種類,如下圖
每一個namespace(也就是REST API中的feature),都有自己的client,例如索引相關的操作都有索引專用的client類負責,範例程式碼如下,client.indices()返回的是ElasticsearchIndicesClient物件,這是索引操作專用的範例
ElasticsearchClient client = ...
client.indices().create(c -> c.index("products"));
NodeStatistics stats = NodeStatistics.of(b -> b
.total(1)
.failed(0)
.successful(1)
);
// The `failures` list was not provided.
// - it's not null
assertNotNull(stats.failures());
private NodeStatistics(Builder builder) {
this.failures = ApiTypeHelper.unmodifiable(builder.failures);
this.total = ApiTypeHelper.requireNonNull(builder.total, this, "total");
this.successful = ApiTypeHelper.requireNonNull(builder.successful, this, "successful");
this.failed = ApiTypeHelper.requireNonNull(builder.failed, this, "failed");
}
public static <T> List<T> unmodifiable(@Nullable List<T> list) {
if (list == null) {
return undefinedList();
}
if (list == UNDEFINED_LIST) {
return list;
}
return Collections.unmodifiableList(list);
}
{
"query":{"term":{ "interests":"youyong"}}
}
{
"query":{"match":{"interests": "changge"}}
}
{
"query": {
"intervals" : {
"my_text" : {
"all_of" : {
"ordered" : false,
"intervals" : [
{
"match" : {
"query" : "my favorite books",
"max_gaps" : 0,
"ordered" : true
}
},
{
"any_of" : {
"intervals" : [
{ "match" : { "query" : "java tutorials" } },
{ "match" : { "query" : "cold porridge" } }
]
}
}
]
}
}
}
}
}
Query query = new Query.Builder()
.term(t -> t
.field("name")
.value(v -> v.stringValue("foo"))
)
.build();
query.term().value().stringValue()
如果在設定的時候,並非用stringValue方法,而是其他型別,那麼上面的程式碼在獲取String型別的值時會丟擲IllegalStateException異常
variant type配有對應的isXXX方法返回其是否屬於某個型別,例如Query就有query.isTerm()表示自己是不是term查詢
還可以用_kind()返回當前型別,下面是範例
switch(query._kind()) {
case Term:
doSomething(query.term());
break;
case Intervals:
doSomething(query.intervals());
break;
default:
doSomething(query._kind(), query._get());
}
InputStream input = this.getClass()
.getResourceAsStream("some-index.json");
CreateIndexRequest req = CreateIndexRequest.of(b -> b
.index("some-index")
.withJson(input)
);
boolean created = client.indices().create(req).acknowledged();
Reader queryJson = new StringReader(
"{" +
" \"query\": {" +
" \"range\": {" +
" \"@timestamp\": {" +
" \"gt\": \"now-1w\"" +
" }" +
" }" +
" }," +
" \"size\": 100" +
"}");
Reader aggregationJson = new StringReader(
"{" +
" \"size\": 0, " +
" \"aggregations\": {" +
" \"hours\": {" +
" \"date_histogram\": {" +
" \"field\": \"@timestamp\"," +
" \"interval\": \"hour\"" +
" }," +
" \"aggregations\": {" +
" \"max-cpu\": {" +
" \"max\": {" +
" \"field\": \"host.cpu.usage\"" +
" }" +
" }" +
" }" +
" }" +
" }" +
"}");
SearchRequest aggRequest = SearchRequest.of(b -> b
.withJson(queryJson)
.withJson(aggregationJson)
.ignoreUnavailable(true)
);
Map<String, Aggregate> aggs = client
.search(aggRequest, Void.class)
.aggregations();