首页 > 其他分享 >ES教程-springboot整合ES

ES教程-springboot整合ES

时间:2022-11-20 17:58:02浏览次数:36  
标签:教程 springboot spring indexRequest IOException cloud throws ES

1、springboot如何整合ES

1、导入坐标 (下面是老版本的es坐标官方已经不推荐使用)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

高版本的es坐标
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
</dependency>

2、在配置文件设置es服务器的连接

3、使用ES 注入依赖 创建索引 添加数据 批量添加数据
@Autowired
private RestHighLevelClient restHighLevelClient;

/**
* es创建索引库流程
* @throws IOException
*/
@Test
public void testCreatedIndex() throws IOException {

//获取索引库对象
IndicesClient indicesClient = restHighLevelClient.indices();

//创建索引库请求
CreateIndexRequest createIndexRequest = new CreateIndexRequest("java2022");

//设置索引库的输入格式
createIndexRequest.source("{\n" +
" \"settings\":{\n" +
" \"number_of_shards\" : 2,\n" +
" \"number_of_replicas\" : 0\n" +
" }\n" +
"}", XContentType.JSON);

//建表使用mapping创建映射,数据格式依然为Json
createIndexRequest.mapping("{\n" +
" \"_source\": {\n" +
" \"excludes\":[\"description\"]\n" +
" }, \n" +
" \t\"properties\": {\n" +
" \"name\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\":\"ik_max_word\",\n" +
" \"search_analyzer\":\"ik_smart\"\n" +
" },\n" +
" \"description\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\":\"ik_max_word\",\n" +
" \"search_analyzer\":\"ik_smart\"\n" +
" },\n" +
" \"studymodel\": {\n" +
" \"type\": \"keyword\"\n" +
" },\n" +
" \"price\": {\n" +
" \"type\": \"float\"\n" +
" },\n" +
" \"pic\":{\n" +
"\t\t \"type\":\"text\",\n" +
"\t\t \"index\":false\n" +
"\t }\n" +
" }\n" +
"}", XContentType.JSON);
indicesClient.create(createIndexRequest,RequestOptions.DEFAULT);
}

/**
* es添加数据
* @throws IOException
*/
@Test
public void testAddDoc() throws IOException {

IndexRequest indexRequest = new IndexRequest("java2022","course","1");

indexRequest.source("{\n" +
" \"name\":\"spring cloud实战\",\n" +
" \"description\":\"本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。\",\n" +
" \"studymodel\":\"201001\",\n" +
" \"price\":5.6\n" +
"}",XContentType.JSON);
restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);
}

/**
* 批量添加数据
* @throws IOException
*/
@Test
public void testAddBulkRequest() throws IOException {
//设置批量添加请求
BulkRequest bulkRequest = new BulkRequest();

//假设定义一个文章的数组
String[] stringList = new String[2];
stringList[0] = "\"{\\n\" +\n" +
" \" \\\"name\\\":\\\"spring cloud实战1\\\",\\n\" +\n" +
" \" \\\"description\\\":\\\"本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。\\\",\\n\" +\n" +
" \" \\\"studymodel\\\":\\\"201001\\\",\\n\" +\n" +
" \" \\\"price\\\":5.6\\n\" +\n" +
" \"}\"";
stringList[1] = "\"{\\n\" +\n" +
" \" \\\"name\\\":\\\"spring cloud实战2\\\",\\n\" +\n" +
" \" \\\"description\\\":\\\"本课程主要从四个章节进行讲解: 1.微服务架构入门 2.spring cloud 基础入门 3.实战Spring Boot 4.注册中心eureka。\\\",\\n\" +\n" +
" \" \\\"studymodel\\\":\\\"201001\\\",\\n\" +\n" +
" \" \\\"price\\\":5.6\\n\" +\n" +
" \"}\"";
for (String stringItem : stringList){
System.out.println(stringItem);
//创建单条请求
IndexRequest indexRequest = new IndexRequest("java2022");
//设置数据信息 数据格式为json
//此处的数据格式需要转化为json
indexRequest.source(stringItem,XContentType.JSON);
bulkRequest.add(indexRequest);
}
restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT);
}

 

标签:教程,springboot,spring,indexRequest,IOException,cloud,throws,ES
From: https://www.cnblogs.com/tiantianyang/p/16909060.html

相关文章

  • 17_1_kubernetes 常用命令与应用部署
    01基本概念Pod:K8s最小部署单元,一组容器的集合Deployment:最常见的控制器,用于更高级别部署和管理PodService:为一组Pod提供负载均衡,对外提供统一访问入口Label......
  • SQL server 2016 安装步骤图文教程
    本文通过图文并茂的形式给大家介绍了SQLserver2016安装步骤,非常不错,具有参考借鉴价值,需要的朋友参考下吧 下载地址:安装包可以从这里下载:http://www.itellyou.cn/SQL......
  • ES6之导入NPM包
    NPM和模块化结合使用 前提是要先安装node.js如果在vscode的终端中一直不成功的话,可以在cmd里,以管理员身份来安装jquery:  下面是使用方法://修改背景颜色为粉色......
  • 46.通过字典和Series对象进行分组统计
     -----------------------------------------------------------------------------------------------------------------------------------------------------------......
  • springboot+vue 前后端分离项目对 token 的无痛刷新
    前言最近在做一个系统时,使用了token令牌来进行前后端交互的权限认证。token一般用于前端向后端发起请求时的权限认证。用户登录自己的账号后,会得到一个token,放在每......
  • G. Restore the Permutation
    G.RestorethePermutationAsequenceof$n$numbersiscalledpermutationifitcontainsallnumbersfrom$1$to$n$exactlyonce.Forexample,thesequences......
  • Kubernetes 1.25.4数据平面自带nginx负载均衡实现高可用
    1、环境准备要点:1、使用一个FQDN统一作为APIServer的接入点;2、加入集群之前,每个节点都将该FQDN解析至第一个Master;3、加入集群之后,每个Master节点将该FQDN都解析至自身......
  • 2022/11/20 集训题解 Longest Loose Segment
    linkDescription定义\(a_{1,2,...,m}\)为好序列当且仅当\(\maxa_i+\mina_i>m\),给出一个长度为\(n\)的序列,问最长好序列子段长度。有\(T\)次修改。\(n\le10^6,......
  • AES_加密模块
    AES_加密模块参考连接https://mp.weixin.qq.com/s?__biz=MzIwNDI1NjUxMg==&mid=2651262030&idx=1&sn=9fe47330bad26de7b5025a812aff476d&chksm=8d314821ba46c1375ca4edaf......
  • VNC连接问题 Too many security failures
    环境腾讯云VNCWindows10报错有以下4种解决方法杀掉vncserver进程,再重新启动vncserver-kill:1vncserver-geometry1920x1080:1密码被人暴力破解,引......