es 的 HighLevelClient存在es源代码的引用,结合springboot使用时,会存在es版本的冲突,这里纪录下解决冲突和使用方式(es已经不发起使用这个了)。
留意es服务端的版本必要与client的版本对齐,否则返回数据大概会解析失败!!!
文档地址:Java High Level REST Client | Java REST Client [7.17] | Elastic
1、起首创建个java springboot项目
源码地址:https://github.com/a66245753/es-7-high-level-client.git
pom依赖文件如下,在 dependencyManagement 里解决springboot引起的版本冲突-
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
- <modelVersion>4.0.0</modelVersion>
- <parent>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-parent</artifactId>
- <version>2.6.4</version>
- <relativePath/> <!-- lookup parent from repository -->
- </parent>
- <groupId>com.david</groupId>
- <artifactId>es-7-high-level-client</artifactId>
- <version>1.0-SNAPSHOT</version>
- <packaging>jar</packaging>
- <name>es-7-high-level-client</name>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <elastic.version>7.17.28</elastic.version>
- </properties>
- <dependencyManagement>
- <dependencies>
- <!--规定版本,解决springboot引起的版本冲突-->
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>elasticsearch-rest-client</artifactId>
- <version>${elastic.version}</version>
- </dependency>
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>elasticsearch-rest-high-level-client</artifactId>
- <version>${elastic.version}</version>
- </dependency>
- <dependency>
- <groupId>org.elasticsearch</groupId>
- <artifactId>elasticsearch</artifactId>
- <version>${elastic.version}</version>
- </dependency>
- </dependencies>
- </dependencyManagement>
- <dependencies>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-web</artifactId>
- </dependency>
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-actuator</artifactId>
- </dependency>
- <dependency>
- <groupId>org.elasticsearch.client</groupId>
- <artifactId>elasticsearch-rest-high-level-client</artifactId>
- </dependency>
- <dependency>
- <groupId>com.baomidou</groupId>
- <artifactId>mybatis-plus-boot-starter</artifactId>
- <version>3.4.1</version>
- </dependency>
- <dependency>
- <groupId>mysql</groupId>
- <artifactId>mysql-connector-java</artifactId>
- <scope>runtime</scope>
- </dependency>
- <!-- Lombok 依赖 -->
- <dependency>
- <groupId>org.projectlombok</groupId>
- <artifactId>lombok</artifactId>
- <version>1.18.22</version>
- <scope>provided</scope>
- </dependency>
- </dependencies>
- </project>
复制代码 2、配置es client为java bean
- @Configuration
- public class EsClientConfig {
- @Bean
- public RestHighLevelClient restHighLevelClient()
- {
- return new RestHighLevelClient(
- RestClient.builder(
- new HttpHost("localhost", 9200, "http")
- )
- );
- }
- }
复制代码 2.1 es id查询数据
- /**
- * id查询
- *
- * @return
- * @throws IOException
- */
- @PostMapping("/get")
- public ResponseEntity<Object> get() throws IOException {
- GetRequest getRequest = new GetRequest("ssp_ad_union_log_202403");
- getRequest.id("fcBZZZUBJ3krEJ13KbOG");
- GetResponse response = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);
- return new ResponseEntity<>(response.getSourceAsMap(), HttpStatus.OK);
- }
复制代码 2.2 es 多个id查询数据
- /**
- * ids查询
- *
- * @return
- * @throws IOException
- */
- @PostMapping("/getBatch")
- public ResponseEntity<Object> getBatch() throws IOException {
- MultiGetRequest multiGetRequest = new MultiGetRequest();
- multiGetRequest.add("ssp_ad_union_log_202403","fcBZZZUBJ3krEJ13KbOG");
- multiGetRequest.add("ssp_ad_union_log_202403","VsBZZZUBJ3krEJ13KbOG");
- MultiGetResponse multiGetItemResponses = restHighLevelClient.multiGet(multiGetRequest, RequestOptions.DEFAULT);
- return new ResponseEntity<>(multiGetItemResponses.getResponses(), HttpStatus.OK);
- }
复制代码 2.3 search 分页
- /**
- * 分页查询
- *
- * @param request
- * @return
- * @throws IOException
- */
- @PostMapping("/search")
- public ResponseEntity<EsSearchResult> search(@RequestBody EsSearchRequest request) throws IOException {
- SearchRequest searchRequest = new SearchRequest(request.getIndex());
- // 构建搜索请求
- SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
- sourceBuilder.trackTotalHits(true);
- sourceBuilder.from((request.getPageIndex() - 1) * request.getPageSize());
- sourceBuilder.size(request.getPageSize());
- searchRequest.source(sourceBuilder);
- BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
- if (request.getEqualsParams() != null && !request.getEqualsParams().isEmpty()) {
- for (Map.Entry<String, Object> entry : request.getEqualsParams().entrySet()) {
- queryBuilder.filter(QueryBuilders.termQuery(entry.getKey(), entry.getValue()));
- }
- }
- if (request.getLikeParams() != null && !request.getLikeParams().isEmpty()) {
- for (Map.Entry<String, String> entry : request.getLikeParams().entrySet()) {
- queryBuilder.must(QueryBuilders.matchQuery(entry.getKey(), entry.getValue()));
- }
- }
- if (request.getRangeParams() != null && !request.getRangeParams().isEmpty()) {
- for (EsRangeParams rangeParam : request.getRangeParams()) {
- RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery(rangeParam.getField());
- if (rangeParam.getGte() != null) {
- rangeQuery.gte(rangeParam.getGte());
- }
- if (rangeParam.getLte() != null) {
- rangeQuery.lte(rangeParam.getLte());
- }
- queryBuilder.filter(rangeQuery);
- }
- }
- sourceBuilder.query(queryBuilder);
- // 打印查询语句,可以放到kibana中执行并分析性能
- System.out.println(searchRequest.source().toString());
- // 执行搜索
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
- EsSearchResult result = new EsSearchResult();
- result.setPageIndex(request.getPageIndex());
- result.setPageSize(request.getPageSize());
- result.setTotal(searchResponse.getHits().getTotalHits().value);
- result.setList(Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getSourceAsMap).collect(Collectors.toList()));
- return new ResponseEntity<>(result, HttpStatus.OK);
- }
复制代码 2.4 aggs 聚合统计
- /**
- * 聚合统计
- *
- * @param request
- * @return
- * @throws IOException
- */
- @PostMapping("/aggs")
- public ResponseEntity<List<Object>> aggs(@RequestBody EsSearchRequest request) throws IOException {
- // 创建 SearchSourceBuilder 实例
- SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
- // 设置 track_total_hits
- searchSourceBuilder.trackTotalHits(true);
- // 设置分页参数
- searchSourceBuilder.from(0);
- searchSourceBuilder.size(1);
- // 构建 bool 查询
- BoolQueryBuilder boolQuery = QueryBuilders.boolQuery()
- .filter(QueryBuilders.termQuery("provinceName", "浙江"));
- // 添加查询到 SearchSourceBuilder
- searchSourceBuilder.query(boolQuery);
- // 构建聚合
- TermsAggregationBuilder cityGroup = AggregationBuilders.terms("city_group")
- .field("cityName").size(10)
- .subAggregation(AggregationBuilders.terms("network_group").field("network").size(10))
- .subAggregation(AggregationBuilders.terms("phoneBrand_group").field("phoneBrandName").size(10))
- .subAggregation(AggregationBuilders.terms("sdkVersion_group").field("sdkVersion").size(10))
- .subAggregation(AggregationBuilders.terms("platform_group").field("platformName").size(10))
- .subAggregation(AggregationBuilders.terms("req_group").field("bizType").size(10))
- .subAggregation(
- AggregationBuilders.filter("ecpm_group", QueryBuilders.termQuery("bizType", 2))
- .subAggregation(AggregationBuilders.avg("avg_ecpm").field("ecpm"))
- );
- // 添加聚合到 SearchSourceBuilder
- searchSourceBuilder.aggregation(cityGroup);
- // 创建 SearchRequest 并指定索引名称
- SearchRequest searchRequest = new SearchRequest("ssp_ad_union_log_202403");
- searchRequest.source(searchSourceBuilder);
- // 执行搜索请求
- SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
- List<Object> result = new ArrayList<>(searchResponse.getAggregations().asList());
- for (Aggregation aggregation : searchResponse.getAggregations().asList()) {
- String json = aggregation.toString();
- // String json = OBJECT_MAPPER.writeValueAsString(aggregation);
- // Map<String, Object> map = new HashMap<>();
- // BeanUtils.copyProperties(aggregation, map);
- result.add(json);
- }
- return new ResponseEntity<>(result, HttpStatus.OK);
- }
复制代码 2.5 新增单条数据
- /**
- * 通过json新增
- *
- * @return
- * @throws IOException
- */
- @RequestMapping(path = "add")
- public ResponseEntity<IndexResponse> add() throws IOException {
- IndexRequest indexRequest = new IndexRequest("ssp_ad_union_log_202403");
- // indexRequest.id("1234567890");
- indexRequest.opType(DocWriteRequest.OpType.CREATE);
- indexRequest.source("{\n" +
- " "id": 27731976,\n" +
- " "reqId": "d63e0377-639a-4ac4-96a7-677d507e627e",\n" +
- " "device": "android",\n" +
- " "platform": 3,\n" +
- " "platformName": "快手",\n" +
- " "clientType": 1,\n" +
- " "myAppId": "300001",\n" +
- " "deviceId": "d63e0377-639a-4ac4-96a7-677d507e627e",\n" +
- " "adSiteGroupId": 100000055,\n" +
- " "adSiteId": "6827003034",\n" +
- " "packagePath": "com.jihuomiao.app",\n" +
- " "ecpm": 10700,\n" +
- " "location": null,\n" +
- " "ip": null,\n" +
- " "cityId": 422800,\n" +
- " "areaId": 422822,\n" +
- " "cityName": "恩施",\n" +
- " "areaName": "建始",\n" +
- " "provinceId": 420000,\n" +
- " "provinceName": "湖北",\n" +
- " "phoneBrand": "OPPO",\n" +
- " "phoneBrandName": "oppo",\n" +
- " "phoneModel": null,\n" +
- " "idfa": null,\n" +
- " "bizType": 1,\n" +
- " "sdkVersion": "1.0.2",\n" +
- " "network": "5g",\n" +
- " "logTime": "2025-03-03 15:31:10",\n" +
- " "createdAt": "2025-03-03 15:31:10"\n" +
- " }", XContentType.JSON);
- IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
- return new ResponseEntity<>(indexResponse, HttpStatus.OK);
- }
复制代码 2.6 bulk 批量新增数据
- @RequestMapping(path = "addBatch")
- public ResponseEntity<BulkResponse> addBatch() throws IOException {
- // 创建 BulkRequest
- BulkRequest bulkRequest = new BulkRequest();
- // 添加多个
- bulkRequest.add(new IndexRequest("ssp_ad_union_log_202403")
- // .id("12345678909")
- .source("{ "field1": "value1", "field2": "value2" }", XContentType.JSON));
- bulkRequest.add(new IndexRequest("my_index")
- // .id("12345678987654")
- .source("{ "field1": "value2", "field2": "value2" }", XContentType.JSON));
- // 执行批量创建操作
- BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);
- // 检查是否有错误
- if (bulkResponse.hasFailures()) {
- System.err.println("Bulk operation had failures: " + bulkResponse.buildFailureMessage());
- } else {
- System.out.println("All documents created successfully.");
- }
- return new ResponseEntity<>(bulkResponse, HttpStatus.OK);
- }
复制代码 es 7 high Level client 主要的集成问题还是在与springboot的版本冲突,这点解决掉就可以了,官方文档里都能找到使用说明
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |