1.springboot 集成elasticsearch组件

1.前置条件已经安装和搭建好了elasticsearch中间件
一:项目中引入elasticsearch相关依赖
我安装的elasticsearch版本是7.10.2 对应依赖的版本保持一致
此处省略springboot 搭建及必要的依赖项

        <dependency><groupId>org.elasticsearch.client</groupId><artifactId>elasticsearch-rest-high-level-client</artifactId><version>7.10.2</version></dependency><dependency><groupId>org.elasticsearch</groupId><artifactId>elasticsearch</artifactId><version>7.10.2</version></dependency>

二:项目配置文件里配置上elasticsearch相关的信息
application.yml

es:data:host: ip # es安装所在的服务器ipport: 9200 #es对外暴露的端口clusterName: hc-es-cluster #es的集群

三:工程里编写elasticsearch相关的配置代码


import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class ESConfig {@Value("${es.data.host}")private String host;@Value("${es.data.clusterName}")private String clusterName;@Value("${es.data.port}")private Integer port;@Beanpublic RestHighLevelClient restHighLevelClient() {RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost(host, port, "http")));return client;}
}

四:项目中的引入和测试

import cn.hutool.json.JSONUtil;
import com.jinyi.up.user.JinyiUserProviderApplication;
import com.jinyi.up.user.esPojo.AggDimensionEnum;
import com.jinyi.up.user.esPojo.EsUser;
import com.jinyi.up.user.esPojo.EsUserSearchQueryDto;
import org.apache.commons.lang3.StringUtils;
import org.elasticsearch.action.DocWriteRequest;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.client.indices.GetIndexRequest;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.TermQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.Aggregation;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.*;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.FetchSourceContext;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;import javax.annotation.Resource;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;/*** @author huangchong* @date 2023/9/2 14:36* @desc*/
@SpringBootTest(classes = {JinyiUserProviderApplication.class})
public class TestES {@Resourceprivate RestHighLevelClient esClient;/*** 创建索引** @throws IOException*/@Testpublic void createIndex() throws IOException {//1.创建索引请求CreateIndexRequest request = new CreateIndexRequest("hc_index");//2.执行创建请求CreateIndexResponse indexResponse = esClient.indices().create(request, RequestOptions.DEFAULT);System.out.println(indexResponse);}/*** 获取索引 判断是否存在** @throws IOException*/@Testpublic void getIndex() throws IOException {//1.创建索引请求GetIndexRequest request = new GetIndexRequest("hc_index");//2.执行存在请求Boolean exists = esClient.indices().exists(request, RequestOptions.DEFAULT);System.out.println(exists);}/*** 删除索引** @throws IOException*/@Testpublic void deleteIndex() throws IOException {//1.创建索引请求DeleteIndexRequest request = new DeleteIndexRequest("hc_index");//2.执行删除请求AcknowledgedResponse response = esClient.indices().delete(request, RequestOptions.DEFAULT);System.out.println(response.isAcknowledged());}/*** 添加文档** @throws IOException*/@Testpublic void addEsDocument() throws IOException {EsUser esUser = new EsUser(1L, "hc", 32, 12, new Date());//1.指定索引库IndexRequest request = new IndexRequest("hc_index");// PUT /hc_index/_doc/1request.id("1").timeout(TimeValue.timeValueSeconds(2));//2.将数据放入请求request.source(JSONUtil.toJsonStr(esUser), XContentType.JSON);//发送请求IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);System.out.println(response.toString());}/*** 判断文档是否存在文档** @throws IOException*/@Testpublic void existsEsDocument() throws IOException {//1.指定索引库 和 查询idGetRequest request = new GetRequest("hc_index", "1");//不获取返回的_source 的上下文request.fetchSourceContext(new FetchSourceContext(false));request.storedFields("_none_");boolean exists = esClient.exists(request, RequestOptions.DEFAULT);System.out.println(exists);}/*** 获取文档** @throws IOException*/@Testpublic void getEsDocument() throws IOException {//1.指定索引库 和 查询idGetRequest request = new GetRequest("hc_index", "1");//不获取返回的_source 的上下文//  request.fetchSourceContext(new FetchSourceContext(false));//  request.storedFields("_none_");GetResponse getResponse = esClient.get(request, RequestOptions.DEFAULT);System.out.println(getResponse.toString());String sourceAsString = getResponse.getSourceAsString();System.out.println(sourceAsString); //返回全部内容 ,和命令一样}/*** 更新文档** @throws IOException*/@Testpublic void updateEsDocument() throws IOException {//1.指定索引库 和 查询idGetRequest request = new GetRequest("hc_index", "1");GetResponse getResponse = esClient.get(request, RequestOptions.DEFAULT);System.out.println(getResponse.toString());String sourceAsString = getResponse.getSourceAsString();EsUser esUser = JSONUtil.toBean(sourceAsString, EsUser.class);//1.指定索引库 和 查询idUpdateRequest updateRequest = new UpdateRequest("hc_index", String.valueOf(esUser.getId()));//不获取返回的_source 的上下文updateRequest.timeout(TimeValue.timeValueSeconds(2));esUser.setUserName("huangchong 2");updateRequest.doc(JSONUtil.toJsonStr(esUser), XContentType.JSON);UpdateResponse updateResponse = esClient.update(updateRequest, RequestOptions.DEFAULT);System.out.println(updateResponse.toString());}/*** 删除文档** @throws IOException*/@Testpublic void deleteEsDocument() throws IOException {//1.指定索引库 和 查询idDeleteRequest deleteRequest = new DeleteRequest("hc_index", "1");deleteRequest.timeout("1s");DeleteResponse delete = esClient.delete(deleteRequest, RequestOptions.DEFAULT);System.out.println(delete.toString());}/*** 批量操作** @throws IOException*/@Testpublic void bulkAddEsDocument() throws IOException {BulkRequest bulkRequest = new BulkRequest();bulkRequest.timeout("10s");List<EsUser> esUsers = new ArrayList<EsUser>();esUsers.add(new EsUser(11L, "hc1", 31, 1, new Date()));esUsers.add(new EsUser(17L, "hc2", 31, 1, new Date()));esUsers.add(new EsUser(18L, "hc3", 28, 2, new Date()));esUsers.add(new EsUser(19L, "hc4", 25, 2, new Date()));esUsers.add(new EsUser(20L, "hc5", 19, 3, new Date()));//批量插入for (int i = 0; i < esUsers.size(); i++) {EsUser esUser = esUsers.get(i);bulkRequest.add(new IndexRequest("hc_index").id(String.valueOf(esUser.getId())).source(JSONUtil.toJsonStr(esUser), XContentType.JSON).opType(DocWriteRequest.OpType.CREATE));}esClient.bulk(bulkRequest, RequestOptions.DEFAULT);}/*** private AggregatorFactories.Builder aggregations;* 1.SearchRequest 请求* 2.SearchSourceBuilder 条件构建* searchSourceBuilder.highlighter() 高亮** @throws IOException*/@Testpublic void queryEsDocument() throws IOException {// 1.指定索引库SearchRequest searchRequest = new SearchRequest("hc_index");//2.构建查询请求条件SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();//QueryBuilders工具类 termQuery 精确查询TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("userName", "hc4");searchSourceBuilder.query(termQueryBuilder);searchSourceBuilder.timeout(TimeValue.timeValueSeconds(60L));searchRequest.source(searchSourceBuilder);SearchResponse response = esClient.search(searchRequest, RequestOptions.DEFAULT);SearchHits searchHits = response.getHits();SearchHit[] hits = searchHits.getHits();for (int i = 0; i < hits.length; i++) {SearchHit hit = hits[i];String sourceAsString = hit.getSourceAsString();System.out.println(JSONUtil.toBean(sourceAsString, EsUser.class).toString());}}/*** private AggregatorFactories.Builder aggregations;* 1.SearchRequest 请求* 2.SearchSourceBuilder 条件构建* searchSourceBuilder.highlighter() 高亮** @throws IOException*/@Testpublic void aggsEsDocument() throws IOException {//对hc_index库的查询请求SearchRequest searchRequest = new SearchRequest("hc_index");//paramDto 转BoolQueryBuilder :对查询条件统一封装EsUserSearchQueryDto queryDto = new EsUserSearchQueryDto();queryDto.setLikeWords("hc");queryDto.setUserStatus("1,2,3");queryDto.setAggDimensions("age");BoolQueryBuilder boolQueryBuilder = buildQuery(queryDto);//查询条件构建SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();searchSourceBuilder.query(boolQueryBuilder);//聚合分析无需得到分页结果,只要聚合结果就行buildQueryFromSize(searchSourceBuilder, 1, 0);//构建聚合aggregationsBuild(searchSourceBuilder, queryDto);searchRequest.source(searchSourceBuilder);//获取结果SearchResponse searchResponse = esClient.search(searchRequest, RequestOptions.DEFAULT);Map<String, Aggregation> aggregationMap = searchResponse.getAggregations().asMap();ParsedValueCount idNumAggs = (ParsedValueCount) aggregationMap.get("idNum");ParsedSum ageSumAggs = (ParsedSum) aggregationMap.get("ageSum");ParsedAvg ageAveAggs = (ParsedAvg) aggregationMap.get("ageAve");ParsedCardinality userNumAggs = (ParsedCardinality) aggregationMap.get("userStatusCount");}/*** 构建聚合方式*/private void aggregationsBuild(SearchSourceBuilder sourceBuilder, EsUserSearchQueryDto queryDto) {ValueCountAggregationBuilder idNumAggs = AggregationBuilders.count("idNum").field("id");SumAggregationBuilder ageSumAggs = AggregationBuilders.sum("ageSum").field("age");//平均值AvgAggregationBuilder ageAvgAggs = AggregationBuilders.avg("ageAve").field("age");//统计去重后的数量CardinalityAggregationBuilder userStatusAggs = AggregationBuilders.cardinality("userStatusCount").field("userStatus");sourceBuilder.aggregation(idNumAggs);sourceBuilder.aggregation(ageSumAggs);sourceBuilder.aggregation(userStatusAggs);sourceBuilder.aggregation(ageAvgAggs);if (StringUtils.isNotEmpty(queryDto.getAggDimensions())) {List<String> list = Arrays.asList(queryDto.getAggDimensions().split(",")).stream().map(s -> s.trim()).collect(Collectors.toList());for (String dimension : list) {AggDimensionEnum dimensionEnum = AggDimensionEnum.getByDimension(dimension);if (dimension == null) {continue;}TermsAggregationBuilder aggs = AggregationBuilders.terms(dimensionEnum.getDimension()).field(dimensionEnum.getField()).size(10000);sourceBuilder.aggregation(aggs);aggs.subAggregation(idNumAggs);aggs.subAggregation(ageSumAggs);aggs.subAggregation(ageAvgAggs);aggs.subAggregation(userStatusAggs);}}}protected void buildQueryFromSize(SearchSourceBuilder sourceBuilder, Integer start, Integer size) {sourceBuilder.from(start);sourceBuilder.size(size);}/*** 构建查询条件** @param queryDto* @return*/private BoolQueryBuilder buildQuery(EsUserSearchQueryDto queryDto) {BoolQueryBuilder query = QueryBuilders.boolQuery();//搜索  分词if (StringUtils.isNotEmpty(queryDto.getUserName())) {query.must(QueryBuilders.matchQuery("userName", queryDto.getUserName()));}//匹配  包含  模糊查询if (StringUtils.isNotEmpty(queryDto.getLikeWords())) {query.must(QueryBuilders.wildcardQuery("userName", "*" + queryDto.getLikeWords() + "*"));}//订单状态  多状态 or查询if (StringUtils.isNotEmpty(queryDto.getUserStatus())) {String[] orderStatuss = StringUtils.split(queryDto.getUserStatus(), ",");BoolQueryBuilder orQuery = QueryBuilders.boolQuery()// 多个状态查询.should(QueryBuilders.termsQuery("userStatus", orderStatuss));query.must(orQuery);}//订单创建时间if (queryDto.getBeginCreateTime() != null) {query.must(QueryBuilders.rangeQuery("createTime").gte(queryDto.getBeginCreateTime().getTime()));}if (queryDto.getEndCreateTime() != null) {query.must(QueryBuilders.rangeQuery("createTime").lte(queryDto.getEndCreateTime().getTime()));}return query;}
}
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.stereotype.Component;import java.util.Date;/*** @author huangchong* @date 2023/9/2 14:51* @desc*/
@Data
@AllArgsConstructor
@NoArgsConstructor
@Component
public class EsUser {private Long id;private String userName;private Integer age;private Integer userStatus;private Date createTime;}
import lombok.Data;import java.util.Date;/*** @author huangchong* @date 2023/9/3 16:12* @desc*/
@Data
public class EsUserSearchQueryDto {private Long id;private String userName;private String likeWords;private Integer age;private String userStatus;private Date beginCreateTime;private Date endCreateTime;private String aggDimensions;
}
package com.jinyi.up.user.esPojo;import java.util.Arrays;
import java.util.Map;
import java.util.stream.Collectors;/*** 聚合分析维度* @author huangchong* @date 2023/9/3 17:00* @desc*/
public enum AggDimensionEnum {userStatus("userStatus", "userStatus", "用户状态"),age("age", "age", "年纪类型"),name("userName", "userName", "名字");private final String dimension;private final String field;private final String desc;AggDimensionEnum(String dimension, String field, String desc) {this.dimension = dimension;this.field = field;this.desc = desc;}public String getDesc() {return desc;}public String getDimension() {return dimension;}public String getField() {return field;}private static final Map<String, String> aggDimensionEnumMap = Arrays.stream(values()).collect(Collectors.toMap(AggDimensionEnum::getDimension, AggDimensionEnum::getDesc));public static Map<String, String> getAggDimensionEnumMap() {return aggDimensionEnumMap;}public static AggDimensionEnum getByDimension(String dimension) {for (AggDimensionEnum dimensionEnum : values()) {if (dimensionEnum.dimension.equals(dimension)) {return dimensionEnum;}}return null;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/70139.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

华为OD机考算法题:根据某条件聚类最少交换次数

目录 题目部分 解读与思路 代码实现 题目部分 题目根据某条件聚类最少交换次数题目说明给出数字K&#xff0c;请输出所有结果小于K的整数组合到一起的最少交换次数。 组合一起是指满足条件的数字相邻&#xff0c;不要求相邻后在数组中的位置。 数据范围 -100 <K < 10…

深度学习模型调参经验

调参经验&#xff1a; 1.Seed在小数据集上有着重要作用&#xff0c;用于参数初始化&#xff0c;可以选取合适的seed。 2.确定epoch&#xff0c;是根据损失函数的值&#xff0c;画出损失函数曲线图&#xff0c;人工确认或者运用早停法。进而再确定auc。 3.batch_size在大数据…

如何将枯燥的大数据进行可视化处理?

在数字时代&#xff0c;大数据已经成为商业、科学、政府和日常生活中不可或缺的一部分。然而&#xff0c;大数据本身往往是枯燥的、难以理解的数字和文字&#xff0c;如果没有有效的方式将其可视化&#xff0c;就会错失其中的宝贵信息。以下是一些方法&#xff0c;可以将枯燥的…

BRAM/URAM资源介绍

BRAM/URAM资源简介 Bram和URAM都是FPGA&#xff08;现场可编程门阵列&#xff09;中的RAM资源。 Bram是Block RAM的缩写&#xff0c;是Xilinx FPGA中常见的RAM资源之一&#xff0c;也是最常用的资源之一。它是一种单独的RAM模块&#xff0c;通常用于存储大量的数据&#xff0…

xctf攻防世界 MISC之CatFlag

0x01.进入环境&#xff0c;下载附件 拿到的是一个无后缀的flag文件&#xff0c;用winhex打开后发现是奇奇怪怪的乱码&#xff0c;用kali的strings搜索也没找到flag情况。 0x02.问题分析 题目提示如图&#xff1a; 让直接cat就行&#xff0c;在kali中直接尝试输入&#xff1a…

Ubuntu22.04 install Kafka

kafka quickstart install kafka

springWeb

springweb就是spring框架中的一个模块&#xff0c;对web层进行了封装&#xff0c;使用起来更加方便。如何方便&#xff1f;参数接收框架进行封装 SpringWeb拥有控制器&#xff0c;接收外部请求&#xff0c;解析参数传给服务层。 SpringWeb运行流程 用户发起请求 ip:端口/项目名…

仿射密码 affine

参考链接&#xff1a;https://www.cnblogs.com/0yst3r-2046/p/12172757.html 仿射加密法 在仿射加密法中&#xff0c;字母表的字母被赋予一个数字&#xff0c;例如 a0&#xff0c;b1&#xff0c;c2…z25 。仿射加密法的密钥为0-25直接的数字对。 仿射加密法与单码加密法没什么…

nginx-QPS限制

漏桶算法&#xff1a; 通过nginx配置实现QPS限速。 #设置请求并发量 qps1&#xff0c;不设置burst&#xff0c;会同时处理并发的请求&#xff0c;但是由于我们只设置了1个qps&#xff0c;所以同一时间内的请求&#xff0c;只有一个是正常的&#xff0c;其他都是失败的。 http配…

GRU门控循环单元

GRU 视频链接 https://www.bilibili.com/video/BV1Pk4y177Xg?p23&spm_id_frompageDriver&vd_source3b42b36e44d271f58e90f86679d77db7Zt—更新门 Rt—重置门 控制保存之前一层信息多&#xff0c;还是保留当前神经元得到的隐藏层的信息多。 Bi-GRU GRU比LSTM参数少 …

升级iOS 17出现白苹果、不断重启等系统问题怎么办?

iOS 17发布后了&#xff0c;很多果粉都迫不及待的将iphone/ipad升级到最新iOS17系统&#xff0c;体验新系统功能。 但部分果粉因硬件、软件的各种情况&#xff0c;导致升级系统后出现故障&#xff0c;比如白苹果、不断重启、卡在系统升级界面等等问题。 如果遇到了这些系统问题…

数学建模--决策树的预测模型的Python实现

目录 1.算法流程简介 2.算法核心代码 3.算法效果展示 1.算法流程简介 """ 决策树的应用:对泰坦尼克号数据集成员进行预测生死 算法流程还是比较简单的,简单学习一下决策树跟着注释写即可 文章参考:https://zhuanlan.zhihu.com/p/133838427 算法种遇上sklear…

【MQTT接收数据写入数据库】

MQTT接收数据写入数据库 1.搭建MQTT服务器 参考上一篇文章 2.安装数据库mysql sudo apt update sudo apt install mysql-server创建一个数据库和数据表存储mqtt消息 首先&#xff0c;登录到MySQL服务器&#xff1a; mysql -u root -p输入你的root用户密码。默认root 3.创…

sqlserver数据库链接mysql服务器访问数据

sqlserver数据库链接mysql服务器访问数据 关于SqlServer数据库怎么链接mysql数据库我一直不明白&#xff0c;今天项目碰到一个问题需要链接&#xff0c;我就研究了一下&#xff0c;然后就成功了&#xff0c;在这里记录一下。也欢迎朋友互相学习交流借鉴。 1.使用navicat打开S…

ui设计岗位招聘要求有哪些? 优漫动游

UI设计师的工作是接到使用者轮廓、线框草稿后&#xff0c;创造出好看、好用的画面&#xff0c;视情况会搭配VisualDesigner&#xff08;视觉设计师&#xff09;&#xff0c;不过偶尔还是需要自己做图&#xff0c;包含插图、icon绘制等&#xff1b;透过设计过的画面&#xff0c;…

【EI/SCOPUS会议征稿】第二届环境遥感与地理信息技术国际学术会议(ERSGIT 2023)

第二届环境遥感与地理信息技术国际学术会议 2023 2nd International Conference on Environmental Remote Sensing and Geographic Information Technology 第二届环境遥感与地理信息技术国际学术会议&#xff08;ERSGIT 2023&#xff09;定于2023年11月10-12日在中国陕西西安…

Hadoop的第二个核心组件:MapReduce框架第二节

Hadoop的第二个核心组件&#xff1a;MapReduce框架第二节 六、MapReduce的工作流程原理&#xff08;简单版本&#xff09;七、MapReduce中的序列化机制问题八、流量统计案例实现&#xff08;序列化机制的实现&#xff09; 六、MapReduce的工作流程原理&#xff08;简单版本&…

Lua03——开发环境搭建

1 安装开发插件 在 idea 或 vscode 中安装 lua 的开发插件 EmmyLua 2 创建工程 在 idea 中创建一个新的工程 工程的类型选择 lua 输入工程名及目标目录 在工程结构的SDK中设置lua在本地安装目录 在工程结构的modules中选择 lua 3 编写第一个lua程序 在工程下添加程序包&#…

Java 中 List 集合取补集

交集 Intersection 英 [ˌɪntəˈsekʃn] 并集 Union 英 [ˈjuːniən] 差集 difference of set 补集 complement set 英 [ˈkɒmplɪment] Java 中 List 集合取交集 Java 中 List 集合取并集 Java 中 List 集合取差集 Java 中 List 集合取补集 # 求两个集合交集的补集 List&l…

阿里云服务器怎么退款?云服务器退款流程图

阿里云服务器如何退款&#xff1f;云服务器在哪申请退款&#xff1f;在用户中心订单管理中的退订管理中退款&#xff0c;阿里云百科分享阿里云服务器退款流程&#xff0c;包括申请退款入口、云服务器退款限制条件、退款多久到账等详细说明&#xff1a; 目录 阿里云服务器退款…