ES客户端分类
Es-Server提供RESTFul-Api,客户端通过发起http请求,调用api实现索引库的管理,数据的交换,server端状态的监控......
- 官方java客户端:
<dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-client</artifactId> <version>7.15.2</version> </dependency>
- SpringDataElasticSearch:
~~~
org.springframework.boot
spring-boot-starter-data-elasticsearch
SpringDataElasticSearch继承了官方客户端elasticsearch-rest-client
![](https://img2020.cnblogs.com/blog/718864/202112/718864-20211215153439146-564044069.png)
![](https://img2020.cnblogs.com/blog/718864/202112/718864-20211215153455295-432675008.png)
* Elasticsearch.Net
* devtool:kibana
* HttpClient
* postman
* ......
> 特殊的客户端,节点客户端(NodeClient),将自己作为Es-server集群的节点。
# SpringDataElasticsearch
* ElasticsearchRepository
* ElasticsearchRestTemplate
* ElasticsearchOperations
ElasticsearchRestTemplate继承了ElasticsearchOperations,一般开发中都是基于ElasticsearchRestTemplate来访问ES服务端。
![](https://img2020.cnblogs.com/blog/718864/202112/718864-20211215153503528-1936136269.png)
# ElasticsearchRestTemplate
## 环境
* ES-Server:7.14.0
* SpringBoot:2.5.3
* spring-boot-starter-data-elasticsearch:2.5.3
* Spring Data Elasticsearch: 4.2.3
* Elasticsearch Client used: 7.12.1
* Elasticsearch cluster: 7.14.0
* 测试索引库名称:vehicle
* 索引库数据结构:
{
"_index": "vehicle",
"_type": "_doc",
"_id": "19771755968",
"_version": 1,
"_score": 1.0,
"_source": {
"id": 19771755968,
"crossing_id": 30474,
"plate_number": "沪D86447",
"plate_color": "黄色",
"is_valid": 1,
"snap_time": "2021-08-29 08:00:00",
"create_time": "2021-08-29 08:07:51",
"lane_number": 1,
"crossing_timestamp": 1630195671.0
}
}
* 索引库映射:
{
"vehicle": {
"mappings": {
"properties": {
"create_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
},
"crossing_id": {
"type": "keyword"
},
"crossing_timestamp": {
"type": "long"
},
"id": {
"type": "keyword"
},
"is_valid": {
"type": "keyword"
},
"lane_number": {
"type": "keyword"
},
"plate_color": {
"type": "keyword"
},
"plate_number": {
"type": "keyword"
},
"snap_time": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
}
}
}
}
}
## 引入pom依赖
org.springframework.boot
spring-boot-starter-data-elasticsearch
com.alibaba
fastjson
1.2.71
org.springframework.boot
spring-boot-starter-web
org.projectlombok
lombok
true
## 配置ES-Server地址
application.yml
spring:
elasticsearch:
rest:
uris: http://192.168.1.149:19200
## 定义model
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.DateFormat;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;
import java.io.Serializable;
import java.time.LocalDateTime;
@Accessors(chain = true)
@Data
@Document(indexName = "vehicle")
public class Vehicle implements Serializable {
@Id
private String id;
@Field("plate_number")
private String plateNumber;
@Field("plate_color")
private String plateColor;
@Field("is_valid")
private Integer valid;
@Field("lane_number")
private Integer laneNumber;
@Field(name = "create_time", type = FieldType.Date, format = DateFormat.custom, pattern = "uuuu-MM-dd HH:mm:ss")
private LocalDateTime createTime;
@Field("crossing_id")
private Long crossingId;
@Field("crossing_timestamp")
private Long crossingTimestamp;
private Long sum;
}
## 根据id查询
@Autowiredprivate ElasticsearchRestTemplate template;
@GetMapping("/{id}")
public Vehicle getVehicle(@PathVariable("id") String id) {
Vehicle vehicle = template.get(id, Vehicle.class);
return vehicle;
}
## list查询
@Autowiredprivate ElasticsearchRestTemplate template;
@PostMapping("/list")
public List findVehicle(@RequestBody FindVehicleParam param) {
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("plate_number", param.getPlateNumber()))
.withQuery(null != param.getPlateColor() ? QueryBuilders.matchQuery("plate_color", param.getPlateColor()) : null)
.build();
SearchHits result = template.search(query, Vehicle.class);
List lists = result.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
return lists;
}
## 查询过滤
@Autowiredprivate ElasticsearchRestTemplate template;
@PostMapping("/filter")
public List findVehicleWithFilter(@RequestBody FindVehicleParam param) {
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("plate_number", param.getPlateNumber()))
.withQuery(null != param.getPlateColor() ? QueryBuilders.matchQuery("plate_color", param.getPlateColor()) : null)
//crossing_id 大于 2000
.withFilter(QueryBuilders.rangeQuery("crossing_id").gt(2000))
.build();
SearchHits result = template.search(query, Vehicle.class);
List lists = result.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
return lists;
}
## 查询聚合
@Autowiredprivate ElasticsearchRestTemplate template;
@PostMapping("/agg")
public VehicleAggregationDto findVehicleWithAgg(@RequestBody FindVehicleParam param) {
VehicleAggregationDto vehicleAggregationDto = new VehicleAggregationDto();
NativeSearchQuery query = new NativeSearchQueryBuilder()
.withQuery(QueryBuilders.matchQuery("plate_number", param.getPlateNumber()))
.withQuery(null != param.getPlateColor() ? QueryBuilders.matchQuery("plate_color", param.getPlateColor()) : null)
//crossing_id 大于 2000
.withFilter(QueryBuilders.rangeQuery("crossing_id").gt(2000))
.addAggregation(AggregationBuilders.sum("sumCrossingTimestamp").field("crossing_timestamp"))
.addAggregation(AggregationBuilders.sum("sumCreateTime").field("create_time"))
.withPageable(PageRequest.of(0, 50))
.build();
SearchHits result = template.search(query, Vehicle.class);
List lists = result.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
vehicleAggregationDto.setVehicles(lists);
Map agg = new ConcurrentHashMap<>();
result.getAggregations().getAsMap().forEach((key, value) -> {
agg.put(key, ((Sum) value).getValue());
});
vehicleAggregationDto.setAggregation(agg);
return vehicleAggregationDto;
}
## 分页
@Autowiredprivate ElasticsearchRestTemplate template;
@PostMapping("/page")
public List findVehiclePage(@RequestBody FindVehiclePageParam param) {
NativeSearchQuery query = new NativeSearchQueryBuilder()
// .withQuery(QueryBuilders.matchAllQuery())
.withQuery(QueryBuilders.matchQuery("plate_number", param.getPlateNumber()))
.withPageable(PageRequest.of(0, param.getPageSize()))
.withSort(SortBuilders.fieldSort("id"))
.build();
SearchHits result = template.search(query, Vehicle.class);
List lists = result.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList());
return lists;
}
~~~