SpringBoot2.x系列教程54--NoSQL之SpringBoot整合ElasticSearch方式一

作者:一一哥

一. Elastic Search

1. Elastic Search简介

ElasticSearch是一个基于Lucene的开源的、分布式实时搜索和分析引擎。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。

Spring Boot为Elasticsearch提供基本的自动配置,我们可以利用Spring Data Elasticsearch在Spring Boot中进行开发。

2. 搭建配置Elastic Search环境

在利用Elastic Search开发之前,请先搭建好Elastic Search环境,我这里配置了ELK环境。关于ELK环境的搭建,感兴趣的朋友可以自行搜索。

3. Spring Boot与Elastic Search版本关系

在新版本的Spring boot2.x中,spring-boot-starter-data-elasticsearch支持的Elasticsearch版本是2.X,但Elasticsearch实际上已经发展到6.5.X版本了,为了更好的使用Elasticsearch的新特性,我们可以弃用spring-boot-starter-data-elasticsearch依赖,改为直接使用Spring-data-elasticsearch,以便启用对新版本支持,目前的版本对应关系如下:

二. Spring Boot2.2.5整合Elastic Search 5.6.8

1. 环境要求

  • Spring Boot 2.2.5;
  • Elastic Search 5.6.8;
  • Kibana 5.6.8;
  • Maven

2. 创建web项目

我们按照之前的经验,创建一个web程序,并将之改造成Spring Boot项目,具体过程略。

3. 添加依赖包

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

注意:因为我的elastic search 并不是6.5.x以上的最新版本,而是5.6.8版本,所以我这里使用的还是spring-boot-starter-data-elasticsearch的依赖包。

4. 创建application.yml配置文件

# Elasticsearch
# 9200作为Http协议,主要用于外部通讯,而9300作为Tcp协议,是传输层监听的默认端口,jar之间就是通过tcp协议通讯
elasticsearch:#注意cluster.name需要与config/elasticsearch.yml中的cluster.name一致# Elasticsearch cluster name.cluster:name: elasticsearch# Comma-separated list of cluster node addresses.ip: localhostport: 9300pool: 5

elastic search默认的端口号是9300。

5. 创建ElasticSearchConfig配置类

package com.yyg.boot.config;import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.core.ElasticsearchOperations;
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;import java.net.InetAddress;/*** @Author 一一哥Sun* @Date Created in 2020/4/13* @Description Description*/
@Slf4j
@Configuration
public class ElasticSearchConfig {@Value("${elasticsearch.ip}")private String hostName;@Value("${elasticsearch.port}")private String port;@Value("${elasticsearch.cluster.name}")private String clusterName;@Value("${elasticsearch.pool}")private String poolSize;/*** 构建TransportClient对象*/@Bean(name = "transportClient")public TransportClient transportClient() {log.info("Elasticsearch初始化开始。。。。。");TransportClient transportClient = null;try {// 配置信息Settings esSetting = Settings.builder()//集群名字.put("cluster.name", clusterName)//增加嗅探机制,找到ES集群.put("client.transport.sniff", true)//增加线程池个数,暂时设为5.put("thread_pool.search.size", Integer.parseInt(poolSize)).build();//配置信息Settings自定义transportClient = new PreBuiltTransportClient(esSetting);TransportAddress transportAddress = new TransportAddress(InetAddress.getByName(hostName), Integer.valueOf(port));transportClient.addTransportAddresses(transportAddress);} catch (Exception e) {log.error("elasticsearch TransportClient create error!!", e);}return transportClient;}/*** 构建ElasticsearchTemplate对象*/@Beanpublic ElasticsearchOperations elasticsearchTemplate() {Client client = transportClient();if (client != null) {return new ElasticsearchTemplate(client);} else {//弹出异常对象throw new RuntimeException("初始化Elasticsearch失败!");}}}

6. 创建增删改查接口

我这里直接使用TransportClient进行增删改查,后面的章节中,我会讲解简化方案。

package com.yyg.boot.web;import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.RangeQueryBuilder;
import org.elasticsearch.search.SearchHit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;/*** @Author 一一哥Sun* @Date Created in 2020/4/13* @Description Description*/
@RestController
@RequestMapping("/book")
public class BookController {@Autowiredprivate TransportClient transportClient;/*** 添加博客索引信息*/@PostMapping("/add")public ResponseEntity add(@RequestParam(name = "title") String title,@RequestParam(name = "author") String author,@RequestParam(name = "word_count") int wordCount,@RequestParam(name = "publish_date") @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date publishDate) {try {XContentBuilder content = XContentFactory.jsonBuilder().startObject().field("title", title).field("author", author).field("word_count", wordCount).field("publish_date", publishDate.getTime()).endObject();IndexResponse result = this.transportClient.prepareIndex("blogs", "blog").setSource(content).get();return new ResponseEntity(result.getId(), HttpStatus.OK);} catch (Exception e) {e.printStackTrace();;return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);}}/*** 根据id查询*/@GetMapping("/get")public ResponseEntity get(@RequestParam(name = "id", defaultValue = "") String id) {if (id.isEmpty()) {return new ResponseEntity((HttpStatus.NOT_FOUND));}GetResponse result = transportClient.prepareGet("blogs", "blog", id).get();if (!result.isExists()) {return new ResponseEntity((HttpStatus.NOT_FOUND));}return new ResponseEntity(result.getSource(), HttpStatus.OK);}/*** 按条件查询*/@GetMapping("/query")public ResponseEntity query(@RequestParam(name = "author", required = false) String author,@RequestParam(name = "title", required = false) String title,@RequestParam(name = "gt_word_count", defaultValue = "0") int gtWordCount,@RequestParam(name = "lt_word_count", required = false) Integer ltWordCount) {//设置查询条件BoolQueryBuilder boolQuery = QueryBuilders.boolQuery();if (author != null) {boolQuery.must(QueryBuilders.matchQuery("author", author));}if (title != null) {boolQuery.must(QueryBuilders.matchQuery("title", title));}//按范围查询RangeQueryBuilder rangeQuery = QueryBuilders.rangeQuery("word_count").from(gtWordCount);if (ltWordCount != null && ltWordCount > 0) {rangeQuery.to(ltWordCount);}boolQuery.filter(rangeQuery);SearchRequestBuilder builder = this.transportClient.prepareSearch("blogs").setTypes("blog").setSearchType(SearchType.DFS_QUERY_THEN_FETCH).setQuery(boolQuery)//设置分页查询.setFrom(0).setSize(10);System.out.println(builder);SearchResponse response = builder.get();List<Map<String, Object>> result = new ArrayList<>();for (SearchHit hit : response.getHits()) {result.add(hit.getSourceAsMap());}return new ResponseEntity(result, HttpStatus.OK);}}

7. 创建Application入口类

package com.yyg.boot;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** @Author 一一哥Sun* @Date Created in 2020/4/9* @Description Description*/
@SpringBootApplication
public class ElasticApplication {public static void main(String[] args) {SpringApplication.run(ElasticApplication.class, args);}}

完整项目结构:

8. 启动elastic search和kibana环境

elastic search正常启动后效果

kibana正常启动后效果

9. 在postman中进行测试

测试添加博客功能


可以看到最后返回了新增的博客id。

在kibana中进行索引的查看

在我们前面的代码中,我们已经创建了index索引为blogs,所以我们可以在kibana中把这个blogs索引同步过来。

到这一步,我们就把代码中创建好的blogs索引同步过来了,接下来我们可以查看blogs索引中的内容。

发现blogs索引中已经有了我们之前添加的博客内容。

测试按id查询博客接口

测试查询全部结果的接口

elasticsearch 9300端口连接不上_SpringBoot2.x系列教程54--SpringBoot整合ElasticSearch方式一...相关推荐

  1. springboot socket服务端_SpringBoot2.x系列教程81--SpringBoot中整合WebSockets

    SpringBoot2.x系列教程81--SpringBoot中整合WebSockets 作者:一一哥 一. WebSockets简介 1. 什么是WebSockets WebSocket是HTML5 ...

  2. channelfuture怎么拿到数据_SpringBoot2.x系列教程66--Spring Boot整合分布式事务之数据库事务回顾

    SpringBoot2.x系列教程66--Spring Boot整合分布式事务之数据库事务回顾 作者:一一哥 本节主要内容 一. 事务出现的原因 转账是生活中常见的操作,比如从A账户转账100元到B账 ...

  3. getprivateprofilestring读不到数据_SpringBoot2.x系列教程66--Spring Boot整合分布式事务之数据库事务回顾

    SpringBoot2.x系列教程66--Spring Boot整合分布式事务之数据库事务回顾 作者:一一哥 本节主要内容 一. 事务出现的原因 转账是生活中常见的操作,比如从A账户转账100元到B账 ...

  4. exchange无法收发邮件_SpringBoot2.x系列教程69--SpringBoot中整合邮件发送

    SpringBoot2.x系列教程69--SpringBoot中整合邮件发送 作者:一一哥 注: 本文案例以QQ邮箱发送的实现为例! 一. 邮件发送概述 1. 概述 在Spring框架中提供了一个Ja ...

  5. exchange无法收发邮件_SpringBoot2.x系列教程69--SpringBoot中整合Mail实现邮件发送

    SpringBoot2.x系列教程69--SpringBoot中整合邮件发送 作者:一一哥 注: 本文案例以QQ邮箱发送的实现为例! 一. 邮件发送概述 1. 概述 在Spring框架中提供了一个Ja ...

  6. SpringBoot进阶教程(七十三)整合elasticsearch

    Python微信订餐小程序课程视频 https://edu.csdn.net/course/detail/36074 Python实战量化交易理财系统 https://edu.csdn.net/cou ...

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

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

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

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

  9. java-web系列(九)---SpringBoot整合ElasticSearch

    前言 这个项目的github地址:extensible项目的github地址 extensible项目当前功能模块如下: java-web系列(一)-搭建一个基于SSM框架的java-web项目 ja ...

最新文章

  1. python练习:猜价钱小游戏
  2. Yarn取代job/task tracker
  3. boost::hana::find用法的测试程序
  4. cvNamedWindow与namedWindow,cvShowImage与inshow的区别
  5. plsql如何执行单个语句_在单个try-with-resources语句中仔细指定多个资源
  6. windows10中屏幕键盘 vs 触摸键盘
  7. 开课吧:什么是包?如何定义包?
  8. 怎么用editplus写asm文件_本周分享 | 用Typora写markdown文件
  9. 507 LOJ 「LibreOJ NOI Round #1」接竹竿
  10. 编写README文档(Markdown)
  11. 无人驾驶技术——YOLO目标检测
  12. 【三石jQuery视频教程】01.图片循环展示_再次重发
  13. 电子病历质控系列-医疗质量工作任务003
  14. 关于技术博客--找工作--这些年--陆续的
  15. 2022亚太杯A题思路
  16. 键盘的各个部分和指法(图片版)
  17. java网络编程案例9-1模拟微信聊天
  18. 关于python霍兰德人格分析雷达图报错,求解答
  19. 基于DSP TMS320F2812的1553B总线开发板
  20. 不同路径中的文件如何批量改名的实用技巧

热门文章

  1. django2 mysql配置_Django:Python3.6.2+Django2.0配置MySQL 转载
  2. java swing setborder_Swing编程边框(Border)的用法总结
  3. 这一篇让你真正理解Mysql的四种隔离级别
  4. springboot redis
  5. iOS开发之使用CocoaPods更新第三方出现“target overrides the `OTHER_LDFLAGS`……”问题解决方案
  6. 生产热更新技术_Reifenh?user引入新一代PET热成型板材生产线
  7. activemq 连接mysql_ActiveMQ 内存配置,ActiveMQ 和mysql
  8. php读取文件内容不全,php读取文件内容的三种方法
  9. css 联系我们,CSS3 模态窗口联系我们表单模板
  10. antv g6 禁止移动_antV G6流程图在Vue中的使用