spring boot集成Elasticsearch 7.16.3

2024-03-01 01:20

本文主要是介绍spring boot集成Elasticsearch 7.16.3,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

环境:Elasticsearch 版本 7.16.3

Elasticsearch for windows下载地址
windows
若依
spring boot版本 2.6.0

在这里插入图片描述

pom文件添加

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

自定义配置类

import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;@Configuration
public class ESConfig extends AbstractElasticsearchConfiguration {@Overridepublic RestHighLevelClient elasticsearchClient() {// 设置连接超时和读取超时时间(单位:毫秒)int connectionTimeout = 60000; // 60 秒int readTimeout = 60000; // 60 秒// 创建 RequestConfigRequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectionTimeout).setSocketTimeout(readTimeout).build();// 创建 RestClientBuilderRestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200)).setHttpClientConfigCallback(httpClientBuilder ->httpClientBuilder.setDefaultRequestConfig(requestConfig));// 创建 RestHighLevelClientRestHighLevelClient client = new RestHighLevelClient(builder);return client;}
}

实体类

indexName = “zbinfo” //索引名

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;@Document(indexName = "zbinfo",shards = 1,replicas = 0)
@Data
@AllArgsConstructor
@NoArgsConstructor
public class ESZbInfo {@Id@Field(type = FieldType.Keyword)private Long id;@Field(type = FieldType.Long)private Long nid;/** 标题 */@Field(type = FieldType.Text)private String title;@Field(type = FieldType.Text)private String content;
}

创建操作的Repository

import com.dataDemo.system.domain.ESZbInfo;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;@Repository
public interface ESZbInfoRepstitory extends ElasticsearchRepository<ESZbInfo, String> {}

操作CRUD

	@Autowiredprivate ESZbInfoRepstitory esZbInfoRepstitory;@Autowiredprivate ElasticsearchOperations elasticsearchOperations;//批量插入
public void insertEsZbInfo(){List<ZbInfoTxy> list= zbInfoTxyService.selectZbInfoTxyListLocal(new ZbInfoTxy());Integer count = 0;List<ESZbInfo> lists = new ArrayList<>();if(list.size() != 0){for(ZbInfoTxy zbInfoTxy:list){System.out.println("数量----"+count+"-----date---"+ DateUtils.getTime());ESZbInfo zbInfo = new ESZbInfo();zbInfo.setId(zbInfoTxy.getNid());zbInfo.setTitle(zbInfoTxy.getTitle());               zbInfo.setContent(zbInfoTxy.getContent());lists.add(zbInfo);count++;}}//批量插入方法esZbInfoRepstitory.saveAll(lists);}//单个插入esZbInfoRepstitory.save(new ZbInfoTxy ());//删除public void deleteEsAll(){System.out.println("删除全部:");esZbInfoRepstitory.deleteAll();}//查询 --根据关键词查询public void selectEsZbInfo(String title){System.out.println("根据关键词查询:");/*** //单条件模糊查询* NativeSearchQuery query = new NativeSearchQueryBuilder()*                 .withQuery(QueryBuilders.matchPhraseQuery("title", title))*                 .build();** //多条件匹配查询*  NativeSearchQuery query = new NativeSearchQueryBuilder()*                 .withQuery(QueryBuilders.boolQuery()*                         .must(QueryBuilders.matchPhraseQuery("title", title))*                         .must(QueryBuilders.matchQuery("city", 98)))*                 .build();** //检索一个字段在两个字段中出现过的* NativeSearchQuery query = new NativeSearchQueryBuilder()*         .withQuery(QueryBuilders.boolQuery()*                 .must(QueryBuilders.multiMatchQuery(title, "title", "content")))*         .build();*/NativeSearchQuery query = new NativeSearchQueryBuilder().withQuery(QueryBuilders.boolQuery().must(QueryBuilders.matchPhraseQuery("title", title)).must(QueryBuilders.matchQuery("city", 98))).build();SearchHits<ESZbInfo> searchHits = elasticsearchOperations.search(query, ESZbInfo.class);List<ESZbInfo> list = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());if(list.size() != 0){for(ESZbInfo esZbInfo:list){System.out.println(esZbInfo);}}}//根据id查询public void selectEsById(String id){Optional<ESZbInfo> optionalById = this.esZbInfoRepstitory.findById(id);System.out.println("单个查询---"+optionalById);}

//查询es状态
http://localhost:9200/_cluster/health

//查询索引的zbinfo映射字段
http://localhost:9200/zbinfo/_mapping

如果插入es的数据量过大,可调整Elasticsearch 目录下/config/elasticsearch.yml 配置文件
添加 http.max_content_length: 200mb(可根据需求更改)
然后重启es

这篇关于spring boot集成Elasticsearch 7.16.3的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/760732

相关文章

一篇文章彻底搞懂macOS如何决定java环境

《一篇文章彻底搞懂macOS如何决定java环境》MacOS作为一个功能强大的操作系统,为开发者提供了丰富的开发工具和框架,下面:本文主要介绍macOS如何决定java环境的相关资料,文中通过代码... 目录方法一:使用 which命令方法二:使用 Java_home工具(Apple 官方推荐)那问题来了,

Java HashMap的底层实现原理深度解析

《JavaHashMap的底层实现原理深度解析》HashMap基于数组+链表+红黑树结构,通过哈希算法和扩容机制优化性能,负载因子与树化阈值平衡效率,是Java开发必备的高效数据结构,本文给大家介绍... 目录一、概述:HashMap的宏观结构二、核心数据结构解析1. 数组(桶数组)2. 链表节点(Node

Java AOP面向切面编程的概念和实现方式

《JavaAOP面向切面编程的概念和实现方式》AOP是面向切面编程,通过动态代理将横切关注点(如日志、事务)与核心业务逻辑分离,提升代码复用性和可维护性,本文给大家介绍JavaAOP面向切面编程的概... 目录一、AOP 是什么?二、AOP 的核心概念与实现方式核心概念实现方式三、Spring AOP 的关

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三

Java中的.close()举例详解

《Java中的.close()举例详解》.close()方法只适用于通过window.open()打开的弹出窗口,对于浏览器的主窗口,如果没有得到用户允许是不能关闭的,:本文主要介绍Java中的.... 目录当你遇到以下三种情况时,一定要记得使用 .close():用法作用举例如何判断代码中的 input

Spring Gateway动态路由实现方案

《SpringGateway动态路由实现方案》本文主要介绍了SpringGateway动态路由实现方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随... 目录前沿何为路由RouteDefinitionRouteLocator工作流程动态路由实现尾巴前沿S

JavaScript对象转数组的三种方法实现

《JavaScript对象转数组的三种方法实现》本文介绍了在JavaScript中将对象转换为数组的三种实用方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友... 目录方法1:使用Object.keys()和Array.map()方法2:使用Object.entr

idea+spring boot创建项目的搭建全过程

《idea+springboot创建项目的搭建全过程》SpringBoot是Spring社区发布的一个开源项目,旨在帮助开发者快速并且更简单的构建项目,:本文主要介绍idea+springb... 目录一.idea四种搭建方式1.Javaidea命名规范2JavaWebTomcat的安装一.明确tomcat

Java高效实现PowerPoint转PDF的示例详解

《Java高效实现PowerPoint转PDF的示例详解》在日常开发或办公场景中,经常需要将PowerPoint演示文稿(PPT/PPTX)转换为PDF,本文将介绍从基础转换到高级设置的多种用法,大家... 目录为什么要将 PowerPoint 转换为 PDF安装 Spire.Presentation fo