当前位置: 首页 > news >正文

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

http://www.lryc.cn/news/308256.html

相关文章:

  • HTML5+CSS3小实例:环绕小球弹性loading动画
  • SpringBoot 自定义注解实现操作日志记录
  • ubuntu常见配置
  • electron+vue3全家桶+vite项目搭建【27】封装窗口工具类【1】雏形
  • 从模型到复合AI系统的转变
  • 将仓库A中的部分提交迁移到仓库B中
  • 信息安全技术基础
  • flask知识--01
  • 软考52-上午题-【数据库】-关系模式2
  • devc++跑酷小游戏3.5.0
  • Redisson限流算法
  • GPT与MBR:硬盘分区表格式的革新与区别
  • 机器学习-1
  • Stream流详解
  • javaweb学习(day05-TomCat)
  • 【Unity】构建简单实用的年份选择器(简单原理示范)
  • LeetCode 2120.执行所有后缀指令
  • 租赁小程序|租赁系统|租赁软件开发带来高效运营
  • 大数据集群管理软件 CDH、Ambari、DataSophon 对比
  • 插值、逼近、拟合、光顺
  • Java单元测试 - mock静态方法
  • Unity使用PlayableAPI 动态播放动画
  • unity使用Registry类将指定内容写入注册表
  • Python进阶学习:Pandas--将一种的数据类型转换为另一种类型(astype())
  • OpenCV开发笔记(七十五):相机标定矫正中使用remap重映射进行畸变矫正
  • 光伏预测 | Matlab基于CNN-SE-Attention-ITCN的多特征变量光伏预测
  • k8s学习笔记-基础概念
  • C语言 变量
  • 2024年2月16日优雅草蜻蜓API大数据服务中心v1.1.1大更新-UI全新大改版采用最新设计ui·增加心率计算器·退休储蓄计算·贷款还款计算器等数接口
  • WEB漏洞 逻辑越权之支付数据篡改安全