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:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId></dependency>
SpringDataElasticSearch继承了官方客户端elasticsearch-rest-client
- Elasticsearch.Net
- devtool:kibana
- HttpClient
- postman
- ……
特殊的客户端,节点客户端(NodeClient),将自己作为Es-sad8erver集群的节点。
SpringDataElasticsearch
- ElasticsearchRepository
- ElasticsearchRestTemplate
- ElasticsearchOperations
ElasticsearchRestTemplate继承了ElasticsearchOperations,一般开发中都是基于ElasticsearchRestTemplate来访问ES服务端。
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\":{&nbsad8p;\"type\":\"date\",\"format\":\"yyyy-MM-ddHH: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\"&nbsad0p;},\"snap_time\":{\"type\":\"date\",\"format\":\"yyyy-MM-ddHH:mm:ss||yyyy-MM-dd||epoch_millis\"}}}}}
引入pom依赖
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency> <dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.71</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId></dependency><dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional></dependency>
配置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;ad8 @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<Vehicle> 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<Vehicle> result = template.search(query, Vehicle.class); List<Vehicle> lists = result.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList()); return lists;}
查询过滤
@Autowiredprivate ElasticsearchRestTemplate template;@PostMapping(\"/filter\")public List<Vehicle> 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<Vehicle> result = template.search(query, Vehicle.class); List<Vehicle> lists = result.getSearchHits().stream().map(SearchHit::getContent).collect(Colle2067ctors.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<Vehicle> result = template.search(query, Vehicle.class); List<Vehicle> lists = result.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList()); vehicleAggregationDto.setVehicles(lists); Map<String, Object> 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<Vehicle> 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<Vehicle> result = template.search(query, Vehicle.class); List<Vehicle> lists = result.getSearchHits().stream().map(SearchHit::getContent).collect(Collectors.toList()); return lists;}
引用
- 官网:https://www.elastic.co/guide/en/elasticsearch/client/java-rest/7.15/java-rest-low-usage-maven.html
- SpringDataElasticsearch官网:https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.clients.rest