链接方式参考官网

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相关推荐

  1. es springboot 不设置id_原创 | 一篇解决Springboot 整合 Elasticsearch

    ElasticSearch 结合业务的场景,在目前的商品体系需要构建搜索服务,主要是为了提供用户更丰富的检索场景以及高速,实时及性能稳定的搜索服务. ElasticSearch是一个基于Lucene的 ...

  2. 七十九、Springboot 整合 Elasticsearch

    @Author:Runsen 来源:尚硅谷 下面建议读者学习尚硅谷的B站的SpringBoot视频,我是学雷丰阳视频入门的. 具体链接如下:B站尚硅谷SpringBoot教程 文章目录 Elastic ...

  3. SpringBoot和Elasticsearch集成

    SpringBoot和Elasticsearch的集成: 步骤 依赖 在Maven的pom文件中 123456789 <!--SpringBoot默认使用SpringData ElasticSe ...

  4. Springboot整合Elasticsearch(High-level-Client)

    前言 通过学习Elasticsearch一小段时间来稍微认识了一点ES的体系架构.发现ES最大的坑就是版本兼容性问题了-在整合Springboot也不例外,但是,有一种方式能较好的解决-通过restc ...

  5. 史上最简单的Elasticsearch教程:SpringBoot集成Elasticsearch 实时流量监测平台

    SpringBoot集成Elasticsearch 实时流量监测平台 目录: 第一章:初尝 Elasticsearch 第二章:玩转 Kibana 第三章:开发原生 Elasticsearch 案例 ...

  6. Springboot 整合 ElasticSearch 入门教学必看

    ElasticSearch 相比搜到这篇文章的人,都已经有过对它的了解, 一种流行的企业级搜索引擎,是一个分布式,高性能.高可用.可伸缩的搜索和分析系统. 那么用我粗俗的言语来说,它就是提供一个存储数 ...

  7. 微服务商城系统(六)商品搜索 SpringBoot 整合 Elasticsearch

    文章目录 一.Elasticsearch 和 IK 分词器的安装 二.Kibana 使用 三.数据导入 Elasticsearch 1.SpringData Elasticsearch 介绍 2.搜索 ...

  8. SpringBoot集成ElasticSearch在启动时报availableProcessors is already set to [8], rejecting [8]

    背景 项目基于SpringBoot并且集成ElasticSearch,今天在编写测试类准备进行单元测试时,报了如下这个错误. Caused by: org.springframework.beans. ...

  9. SpringBoot整合Elasticsearch(一)

    SpringBoot整合Elasticsearch 基础环境 SpringBoot 版本 : 2.4.0 ES 版本: 7.9.3 Kibana版本: 7.9.3 SpringBoot内置Tomcat ...

最新文章

  1. matlab在命令行注册,命令行运行matlab
  2. 定义指令时“控制器”,“链接”和“编译”函数之间的区别
  3. C语言练习题——动态数组
  4. Intel(R)Turbo Boost Technology Driver上面显示为感叹号
  5. 木马开机启动的六种方法
  6. intersect函数_PHP array_intersect()函数与示例
  7. Windows下 maven3.0.4的安装步骤+maven配置本地仓库
  8. 领导看了我写的关闭超时订单,让我出门左转!
  9. 使用Zeigarnik效应来学习编码更快
  10. 得物App联合Zippo推出限量款打火机 首批开售十分钟即售罄
  11. 52 - 算法 - 数据结构 vector
  12. 数值分析方程求根实验matlab,基于matlab的数值分析( 非线性方程求根)上机实验报告1...
  13. 集成电路的设计 —— 半导体
  14. line-height在安卓机位置靠上
  15. 鸿蒙系统麒麟970芯片支持,受鸿蒙系统影响,众多华为手机或要说再见,包括麒麟970机型!...
  16. Marlin固件学习总结(一)
  17. linux c 获取usb vid,Linux如何使用libudev获取USB设备VID及PID
  18. 理解和解决requireJS的报错:MODULE NAME HAS NOT BEEN LOADED YET FOR CONTEXT
  19. 重磅!罗振宇跨年演讲:扎心5问
  20. python strftime时分秒_python如何把秒换成时分秒

热门文章

  1. 鸡叫与天亮:大数据中的关联与因果
  2. 【MATLAB编程实例练习】-(15)红绿色方块染色问题
  3. 微信小程序(看文档写实例六)微信小程序课堂宝APP实现签到逻辑
  4. ASDL的详细配置(图文)
  5. 软文推广最终目的是什么?
  6. 理性探讨:赚多少月薪,才称得上不穷?
  7. 2023保密教育线上培训考试参考答案 03
  8. 类似于网易新闻客户端的界面实现02
  9. 基因组变异检测SNPcalling(GATK)
  10. Java程序运行机制简介