springboot 链接elasticsearch
链接方式参考官网
https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-high-getting-started.html
传统的方式是JEST。但是我建议用官网的API比较方便
建立一个congif类
package com.test.es.yang.config;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;/*** @author: 杨丰源* @date: 2021-06-29 14:48* @description:*/
@Configuration
public class ESConfiguration {@Value("${elasticsearch.hostname}")String hostname;@Value("${elasticsearch.port}")int port;@Beanpublic RestHighLevelClient restHighLevelClient() {// 如果有多个从节点可以持续在内部new多个HttpHost,参数1是IP,参数2是端口,参数3是通信协议return new RestHighLevelClient(RestClient.builder(new HttpHost(hostname, port, "http")));}}
这里配置文件的配置如下
elasticsearch:hostname: localhostport: 9200
建立接口:
IRestHighLevelClientService 实现创建删除索引
package com.test.es.yang.service;import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;import java.io.IOException;/*** @author: 杨丰源* @date: 2021-06-29 16:00* @description:*/
public interface IRestHighLevelClientService {CreateIndexResponse createIndex(String indexName) throws IOException;AcknowledgedResponse deleteIndex(String indexName) throws IOException;
}
建立service实现类实现接口
package com.test.es.yang.service.impl;import com.test.es.yang.service.IRestHighLevelClientService;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;import java.io.IOException;/*** @author: 杨丰源* @date: 2021-06-29 16:05* @description:*/@Service
public class RestHighLevelClientServiceImpl implements IRestHighLevelClientService {@Autowiredprivate RestHighLevelClient restHighLevelClient;@Overridepublic CreateIndexResponse createIndex(String indexName) throws IOException {CreateIndexRequest request = new CreateIndexRequest(indexName.toLowerCase());request.settings(Settings.builder().put("index.number_of_shards", 5).put("index.number_of_replicas", 0));CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);return createIndexResponse;}@Overridepublic AcknowledgedResponse deleteIndex(String indexName) throws IOException {DeleteIndexRequest indexRequest = new DeleteIndexRequest(indexName);AcknowledgedResponse delete = restHighLevelClient.indices().delete(indexRequest, RequestOptions.DEFAULT);return delete;}
}
建立controller
package com.test.es.yang.controller;import com.test.es.yang.service.IRestHighLevelClientService;
import common.CommonResult;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import java.io.IOException;/*** @author: 杨丰源* @date: 2021-06-29 15:58* @description:*/
@RestController
@RequestMapping("/es")
public class ESController {@Autowiredprivate IRestHighLevelClientService restHighLevelClientService;/*** 创建索引* @return* @throws IOException*/@PostMapping("/createIndex")public CommonResult createIndex(@RequestParam String indexName) throws IOException {CreateIndexResponse index = restHighLevelClientService.createIndex(indexName);String success="1";return new CommonResult(200,"success",index);}/*** 删除索引* @return* @throws IOException*/@PostMapping("/deleteIndex")public CommonResult deleteIndex(@RequestParam String indexName) throws IOException {AcknowledgedResponse acknowledgedResponse = restHighLevelClientService.deleteIndex(indexName);return new CommonResult(200,"success",acknowledgedResponse);}}
最简单的建立索引搞定
springboot 链接elasticsearch相关推荐
- es springboot 不设置id_原创 | 一篇解决Springboot 整合 Elasticsearch
ElasticSearch 结合业务的场景,在目前的商品体系需要构建搜索服务,主要是为了提供用户更丰富的检索场景以及高速,实时及性能稳定的搜索服务. ElasticSearch是一个基于Lucene的 ...
- 七十九、Springboot 整合 Elasticsearch
@Author:Runsen 来源:尚硅谷 下面建议读者学习尚硅谷的B站的SpringBoot视频,我是学雷丰阳视频入门的. 具体链接如下:B站尚硅谷SpringBoot教程 文章目录 Elastic ...
- SpringBoot和Elasticsearch集成
SpringBoot和Elasticsearch的集成: 步骤 依赖 在Maven的pom文件中 123456789 <!--SpringBoot默认使用SpringData ElasticSe ...
- Springboot整合Elasticsearch(High-level-Client)
前言 通过学习Elasticsearch一小段时间来稍微认识了一点ES的体系架构.发现ES最大的坑就是版本兼容性问题了-在整合Springboot也不例外,但是,有一种方式能较好的解决-通过restc ...
- 史上最简单的Elasticsearch教程:SpringBoot集成Elasticsearch 实时流量监测平台
SpringBoot集成Elasticsearch 实时流量监测平台 目录: 第一章:初尝 Elasticsearch 第二章:玩转 Kibana 第三章:开发原生 Elasticsearch 案例 ...
- Springboot 整合 ElasticSearch 入门教学必看
ElasticSearch 相比搜到这篇文章的人,都已经有过对它的了解, 一种流行的企业级搜索引擎,是一个分布式,高性能.高可用.可伸缩的搜索和分析系统. 那么用我粗俗的言语来说,它就是提供一个存储数 ...
- 微服务商城系统(六)商品搜索 SpringBoot 整合 Elasticsearch
文章目录 一.Elasticsearch 和 IK 分词器的安装 二.Kibana 使用 三.数据导入 Elasticsearch 1.SpringData Elasticsearch 介绍 2.搜索 ...
- SpringBoot集成ElasticSearch在启动时报availableProcessors is already set to [8], rejecting [8]
背景 项目基于SpringBoot并且集成ElasticSearch,今天在编写测试类准备进行单元测试时,报了如下这个错误. Caused by: org.springframework.beans. ...
- SpringBoot整合Elasticsearch(一)
SpringBoot整合Elasticsearch 基础环境 SpringBoot 版本 : 2.4.0 ES 版本: 7.9.3 Kibana版本: 7.9.3 SpringBoot内置Tomcat ...
最新文章
- 【HTML】兴唐第二十八节课之初识HTML
- 一口气说出 过滤器 和 拦截器 6个区别,别再傻傻分不清了
- 饮冰三年-人工智能-linux-07 硬盘分区、格式化及文件系统的管理
- js-this作用域
- [LeetCode][JavaScript]Power of Three
- cookie购物车php简单,php中利用cookie实现购物车实例_PHP教程
- 统计一行文本的单词个数_LeetCode68-文本左右对齐
- 鸿蒙电视是无线么,鸿蒙系统首秀,在自家设备上和普通电视大不相同赵崇带你走世界...
- 为什么互联网公司需要测试人员?
- 2021年年度最优质开源软件
- 给年轻工程师的十大忠告[转载]
- iOS流布局UICollectionView系列四——自定义FlowLayout进行瀑布流布局
- java 注解解析_Java知识点总结(注解-解析注解)
- IC工程师简历制作全解读
- 深入理解ext2文件系统
- 2020年中国养老地产行业市场现状分析,提高养老地产运营水平是关键「图」
- X8AIP 驱动程序
- SMAIL 语法大全(Dalvik 虚拟机操作码)
- 全国区号省份mysql_中国各个省份的区号是多少
- Java前端内联和外联的区别,css外联和内联、嵌入的区别是什么?