Java High Level REST Client
Elasticsearch 官方高级客户端: 基于低级客户端,提供特定的方法的API,并处理请求编码和响应解码
Java高级REST客户端可以在Java Low Level REST客户机之上工作。 其主要目标是公开特定方法的API,接受请求对象作为参数并返回响应对象,以便客户端自己处理请求编组和响应解组。
每个API可以同步或异步地调用。 同步方法返回一个响应对象,而名称以 async
后缀结尾的异步方法需要收到响应或错误后才会通知(在低级别客户端管理的线程池上)的侦听器参数。
Java高级REST客户端依赖于 Elasticsearch core
项目。 它接受与 TransportClient
相同的请求参数,并返回相同的响应对象。
ElasticSeach6.6.0版本 Java Rest High Client,官网API,如下。相比之前,的确丰富了很多。
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.6/java-rest-high.html
本节介绍的内容,从如何high-level REST client获取包到在应用程序中使用高级REST客户端
Java高级REST客户端需要Java 1.8,并依赖于Elasticsearch核心项目。 客户端版本要与客户端开发的Elasticsearch版本相同。 它接受与 TransportClient
相同的请求参数,并返回相同的响应对象。 如果需要将应用程序从TransportClient迁移到新的REST客户端,请参阅 迁移指南
。
高级客户端保证能够与运行在相同主版本和大于或等于次要版本的任何Elasticsearch节点进行通信。它不需要与它进行通信的弹性搜索节点相同的次要版本,因为它是向前兼容的,意味着它支持与之前开发的弹性搜索的更新版本进行通信。
6.0 客户端能够与任何 6.x Elasticsearch 节点进行通信,而 6.1 客户端确实能够与 6.1,6.2 和以后的 6.x 版本进行通信,但与以前的 Elasticsearch 节点版本通信时可能会出现不兼容问题例如 6.1 到 6.0 之间,例如 6.1 客户端支持而 6.0 节点不知道的某些API的新请求主体字段。
建议在将Elasticsearch集群升级到新的主要版本时升级高级客户端,因为REST API突破性更改可能会导致意外的结果,具体取决于请求所击中的节点,新添加的API只能由较新版本的客户端。一旦群集中的所有节点都升级到新的主版本,则客户端应当更新。
The javadoc for the REST high level client can be found at https://artifacts.elastic.co/javadoc/org/elasticsearch/client/elasticsearch-rest-high-level-client/6.6.2/index.html.
高级 Java REST 客户端被托管在 Maven 中央仓库里。所需的最低Java版本为 1.8。
高级 REST 客户端与 elasticsearch 的发行周期相同。可以使用期望的版本进行替换。
Maven 配置
若使用 Maven 作依赖管理,你可以这样配置依赖。将下列内容添加到你的 pom.xml 文件里:
org.elasticsearch.client
elasticsearch-rest-high-level-client
6.6.0
高级 Java REST Client 依赖以下包:
参考之前的内容:
ElasticSearch 应用开发(二)Java Client 连接ElasticSearch集群
RestHighLevelClient
所有APIs都能接收一个能够自定义request请求的RequestOptions,而且不用改变elasticsearch如何执行request。例如,可以设定NodeSelector
决定哪个节点接收request。具体查看: low level client documentation
The Java High Level REST Client supports the following Document APIs:
Single document APIs
Multi-document APIs
The Java High Level REST Client supports the following Search APIs:
The Java High Level REST Client supports the following Miscellaneous APIs:
The Java High Level REST Client supports the following Indices APIs:
Index Management
Mapping Management
Alias Management
Template Management
The Java High Level REST Client supports the following Cluster APIs:
The Java High Level REST Client supports the following Ingest APIs:
The Java High Level REST Client supports the following Snapshot APIs:
The Java High Level REST Client supports the following Tasks APIs:
The Java High Level REST Client supports the following Scripts APIs:
The Java High Level REST Client supports the following Licensing APIs:
The Java High Level REST Client supports the following Machine Learning APIs:
The Java High Level REST Client supports the following Migration APIs:
The Java High Level REST Client supports the following Rollup APIs:
The Java High Level REST Client supports the following Security APIs:
The Java High Level REST Client supports the following Watcher APIs:
The Java High Level REST Client supports the following Graph APIs:
The Java High Level REST Client supports the following CCR APIs:
The Java High Level REST Client supports the following Index Lifecycle Management APIs:
Java高级REST客户端依赖于 Elasticsearch 核心项目提供的不同类型的 Java Builders 对象,包括:
(一)Query Builders
Query builders只适用于创建一次搜索请求,每次查询由 Query DSL支持。每一个Query builder由QueryBuilder
接口实现,并允许对每个给定类型的query设置特定参数。一旦完成创建, QueryBuilder
能够设置为SearchSourceBuilder的参数
. Search Request 展示了使用 SearchSourceBuilder
和QueryBuilder
进行搜索的例子.过程如下图所示:
Building Search Queries (如下所示)页给出 与QueryBuilder
相对于的 QueryBuilders
进行搜索的函数。
This page lists all the available search queries with their corresponding QueryBuilder
class name and helper method name in the QueryBuilders
utility class.
Match All Query
Search Query | QueryBuilder Class | Method in QueryBuilders |
---|---|---|
Match All |
MatchAllQueryBuilder |
QueryBuilders.matchAllQuery() |
Search Query | QueryBuilder Class | Method in QueryBuilders |
---|---|---|
Match |
MatchQueryBuilder |
QueryBuilders.matchQuery() |
Match Phrase |
MatchPhraseQueryBuilder |
QueryBuilders.matchPhraseQuery() |
Match Phrase Prefix |
MatchPhrasePrefixQueryBuilder |
QueryBuilders.matchPhrasePrefixQuery() |
Multi Match |
MultiMatchQueryBuilder |
QueryBuilders.multiMatchQuery() |
Common Terms |
CommonTermsQueryBuilder |
QueryBuilders.commonTermsQuery() |
Query String |
QueryStringQueryBuilder |
QueryBuilders.queryStringQuery() |
Simple Query String |
SimpleQueryStringBuilder |
QueryBuilders.simpleQueryStringQuery() |
Term level queries
Search Query | QueryBuilder Class | Method in QueryBuilders |
---|---|---|
Term |
TermQueryBuilder |
QueryBuilders.termQuery() |
Terms |
TermsQueryBuilder |
QueryBuilders.termsQuery() |
Range |
RangeQueryBuilder |
QueryBuilders.rangeQuery() |
Exists |
ExistsQueryBuilder |
QueryBuilders.existsQuery() |
Prefix |
PrefixQueryBuilder |
QueryBuilders.prefixQuery() |
Wildcard |
WildcardQueryBuilder |
QueryBuilders.wildcardQuery() |
Regexp |
RegexpQueryBuilder |
QueryBuilders.regexpQuery() |
Fuzzy |
FuzzyQueryBuilder |
QueryBuilders.fuzzyQuery() |
Type |
TypeQueryBuilder |
QueryBuilders.typeQuery() |
Ids |
IdsQueryBuilder |
QueryBuilders.idsQuery() |
Compound queries
Search Query | QueryBuilder Class | Method in QueryBuilders |
---|---|---|
Constant Score |
ConstantScoreQueryBuilder |
QueryBuilders.constantScoreQuery() |
Bool |
BoolQueryBuilder |
QueryBuilders.boolQuery() |
Dis Max |
DisMaxQueryBuilder |
QueryBuilders.disMaxQuery() |
Function Score |
FunctionScoreQueryBuilder |
QueryBuilders.functionScoreQuery() |
Boosting |
BoostingQueryBuilder |
QueryBuilders.boostingQuery() |
Joining queries
Search Query | QueryBuilder Class | Method in QueryBuilders |
---|---|---|
Nested |
NestedQueryBuilder |
QueryBuilders.nestedQuery() |
Has Child |
HasChildQueryBuilder |
|
Has Parent |
HasParentQueryBuilder |
|
Parent Id |
ParentIdQueryBuilder |
|
Geo queries
Search Query | QueryBuilder Class | Method in QueryBuilders |
---|---|---|
GeoShape |
GeoShapeQueryBuilder |
QueryBuilders.geoShapeQuery() |
Geo Bounding Box |
GeoBoundingBoxQueryBuilder |
QueryBuilders.geoBoundingBoxQuery() |
Geo Distance |
GeoDistanceQueryBuilder |
QueryBuilders.geoDistanceQuery() |
Geo Polygon |
GeoPolygonQueryBuilder |
QueryBuilders.geoPolygonQuery() |
Specialized queries
Search Query | QueryBuilder Class | Method in QueryBuilders |
---|---|---|
More Like This |
MoreLikeThisQueryBuilder |
QueryBuilders.moreLikeThisQuery() |
Script |
ScriptQueryBuilder |
QueryBuilders.scriptQuery() |
Percolate |
PercolateQueryBuilder |
|
Wrapper |
WrapperQueryBuilder |
QueryBuilders.wrapperQuery() |
Span queries
Search Query | QueryBuilder Class | Method in QueryBuilders |
---|---|---|
Span Term |
SpanTermQueryBuilder |
QueryBuilders.spanTermQuery() |
Span Multi Term |
SpanMultiTermQueryBuilder |
QueryBuilders.spanMultiTermQueryBuilder() |
Span First |
SpanFirstQueryBuilder |
QueryBuilders.spanFirstQuery() |
Span Near |
SpanNearQueryBuilder |
QueryBuilders.spanNearQuery() |
Span Or |
SpanOrQueryBuilder |
QueryBuilders.spanOrQuery() |
Span Not |
SpanNotQueryBuilder |
QueryBuilders.spanNotQuery() |
Span Containing |
SpanContainingQueryBuilder |
QueryBuilders.spanContainingQuery() |
Span Within |
SpanWithinQueryBuilder |
QueryBuilders.spanWithinQuery() |
Span Field Masking |
FieldMaskingSpanQueryBuilder |
QueryBuilders.fieldMaskingSpanQuery() |
(二)Aggregation Builders
和query builders类似,在搜索执行的过程,aggregation builders是用来聚合计算的。每一个类型的aggregation builder,都有与之对应的并且由ELasticsearch支持的aggregation (or pipeline aggregation)。创建成功后,`AggregationBuilder
可以作为SearchSourceBuilder的参数。
Search Request page,包含 AggregationBuilder
用于 SearchSourceBuilder
进行聚合计算的例子. The Building Aggregations page 列出了,AggregationBuilder
和 AggregationBuilders
相对于的帮助方法(如下所示)。
This page lists all the available aggregations with their corresponding AggregationBuilder
class name and helper method name in the AggregationBuilders
or PipelineAggregatorBuilders
utility classes.
Metrics Aggregations
Aggregation | AggregationBuilder Class | Method in AggregationBuilders |
---|---|---|
Avg |
AvgAggregationBuilder |
AggregationBuilders.avg() |
Cardinality |
CardinalityAggregationBuilder |
AggregationBuilders.cardinality() |
Extended Stats |
ExtendedStatsAggregationBuilder |
AggregationBuilders.extendedStats() |
Geo Bounds |
GeoBoundsAggregationBuilder |
AggregationBuilders.geoBounds() |
Geo Centroid |
GeoCentroidAggregationBuilder |
AggregationBuilders.geoCentroid() |
Max |
MaxAggregationBuilder |
AggregationBuilders.max() |
Min |
MinxAggregationBuilder |
AggregationBuilders.min() |
Percentiles |
PercentilesAggregationBuilder |
AggregationBuilders.percentiles() |
Percentile Ranks |
PercentileRanksAggregationBuilder |
AggregationBuilders.percentileRanks() |
Scripted Metric |
ScriptedMetricAggregationBuilder |
AggregationBuilders.scriptedMetric() |
Stats |
StatsAggregationBuilder |
AggregationBuilders.stats() |
Sum |
SumAggregationBuilder |
AggregationBuilders.sum() |
Top hits |
TopHitsAggregationBuilder |
AggregationBuilders.topHits() |
Value Count |
ValueCountAggregationBuilder |
AggregationBuilders.count() |
Bucket Aggregations
Aggregation | AggregationBuilder Class | Method in AggregationBuilders |
---|---|---|
Adjacency Matrix |
AdjacencyMatrixAggregationBuilder |
AggregationBuilders.adjacencyMatrix() |
Children |
ChildrenAggregationBuilder |
|
Date Histogram |
DateHistogramAggregationBuilder |
AggregationBuilders.dateHistogram() |
Date Range |
DateRangeAggregationBuilder |
AggregationBuilders.dateRange() |
Diversified Sampler |
DiversifiedAggregationBuilder |
AggregationBuilders.diversifiedSampler() |
Filter |
FilterAggregationBuilder |
AggregationBuilders.filter() |
Filters |
FiltersAggregationBuilder |
AggregationBuilders.filters() |
Geo Distance |
GeoDistanceAggregationBuilder |
AggregationBuilders.geoDistance() |
GeoHash Grid |
GeoGridAggregationBuilder |
AggregationBuilders.geohashGrid() |
Global |
GlobalAggregationBuilder |
AggregationBuilders.global() |
Histogram |
HistogramAggregationBuilder |
AggregationBuilders.histogram() |
IP Range |
IpRangeAggregationBuilder |
AggregationBuilders.ipRange() |
Missing |
MissingAggregationBuilder |
AggregationBuilders.missing() |
Nested |
NestedAggregationBuilder |
AggregationBuilders.nested() |
Range |
RangeAggregationBuilder |
AggregationBuilders.range() |
Reverse nested |
ReverseNestedAggregationBuilder |
AggregationBuilders.reverseNested() |
Sampler |
SamplerAggregationBuilder |
AggregationBuilders.sampler() |
Significant Terms |
SignificantTermsAggregationBuilder |
AggregationBuilders.significantTerms() |
Significant Text |
SignificantTextAggregationBuilder |
AggregationBuilders.significantText() |
Terms |
TermsAggregationBuilder |
AggregationBuilders.terms() |
Pipeline Aggregations
Pipeline on | PipelineAggregationBuilder Class | Method in PipelineAggregatorBuilders |
---|---|---|
Avg Bucket |
AvgBucketPipelineAggregationBuilder |
PipelineAggregatorBuilders.avgBucket() |
Derivative |
DerivativePipelineAggregationBuilder |
PipelineAggregatorBuilders.derivative() |
Max Bucket |
MaxBucketPipelineAggregationBuilder |
PipelineAggregatorBuilders.maxBucket() |
Min Bucket |
MinBucketPipelineAggregationBuilder |
PipelineAggregatorBuilders.minBucket() |
Sum Bucket |
SumBucketPipelineAggregationBuilder |
PipelineAggregatorBuilders.sumBucket() |
Stats Bucket |
StatsBucketPipelineAggregationBuilder |
PipelineAggregatorBuilders.statsBucket() |
Extended Stats Bucket |
ExtendedStatsBucketPipelineAggregationBuilder |
PipelineAggregatorBuilders.extendedStatsBucket() |
Percentiles Bucket |
PercentilesBucketPipelineAggregationBuilder |
PipelineAggregatorBuilders.percentilesBucket() |
Moving Average |
MovAvgPipelineAggregationBuilder |
PipelineAggregatorBuilders.movingAvg() |
Cumulative Sum |
CumulativeSumPipelineAggregationBuilder |
PipelineAggregatorBuilders.cumulativeSum() |
Bucket Script |
BucketScriptPipelineAggregationBuilder |
PipelineAggregatorBuilders.bucketScript() |
Bucket Selector |
BucketSelectorPipelineAggregationBuilder |
PipelineAggregatorBuilders.bucketSelector() |
Serial Differencing |
SerialDiffPipelineAggregationBuilder |
PipelineAggregatorBuilders.diff() |
Matrix Aggregations
Aggregation | AggregationBuilder Class | Method in MatrixStatsAggregationBuilders |
---|---|---|
Matrix Stats |
MatrixStatsAggregationBuilder |
MatrixStatsAggregationBuilders.matrixStats() |
本节介绍如何将现有代码从 TransportClient 迁移到使用 Elasticsearch 5.6.0 版本发布的新的 Java 高级 REST 客户端。
ElasticSearch6.6中,Rest High Level Client的API如下 ,以Async的都是异步请求。