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

SpringBoot整合elasticsearch-java

一、依赖

系统使用的是ElasticSearch8.2.0

<dependency><groupId>co.elastic.clients</groupId><artifactId>elasticsearch-java</artifactId><version>8.1.0</version>
</dependency>

二、配置

1、yml文件配置

elasticsearch:url: 192.168.58.131port: 9200

2、config配置文件

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.json.jackson.JacksonJsonpMapper;
import co.elastic.clients.transport.ElasticsearchTransport;
import co.elastic.clients.transport.rest_client.RestClientTransport;
import org.apache.http.HttpHost;
import org.elasticsearch.client.RestClient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.io.IOException;@Configuration
public class RestClientConfig {@Value("${elasticsearch.url}")private String url;@Value("${elasticsearch.port}")private String port;@Beanpublic ElasticsearchClient elasticsearchClient() throws IOException {// 创建低级客户端RestClient restClient = RestClient.builder(new HttpHost(url, Integer.parseInt(port))).build();// 使用 Jackson 映射器创建传输层ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());// 创建 API 客户端ElasticsearchClient client = new ElasticsearchClient(transport);// 关闭 ES 客户端transport.close();restClient.close();return client;}
}

3、测试是否连接成功

@SpringBootTest
class ElasticSearchApplicationTests {@Autowiredprivate ElasticsearchClient elasticsearchClient;@Testvoid test(){System.out.println(elasticsearchClient);}
}

运行成功

 三、索引基本语法

参考文档:Elasticsearch8.x版本中RestHighLevelClient被弃用,新版本中全新的Java客户端Elasticsearch Java API Client中常用API练习

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

相关文章:

  • 网络服务与应用
  • Git项目如何配置,如何上传至GitHub
  • Python教程(一):环境搭建及PyCharm安装
  • 神经网络与注意力机制的权重学习对比:公式探索
  • C语言------指针讲解(3)
  • 博客建站 - 常用的公共DNS服务器
  • 用Redisson的RMap做一个简单的购物车示例
  • 「12月·长沙」第四届机器人、自动化与智能控制国际会议(ICRAIC 2024)
  • 传神社区|数据集合集第7期|法律NLP数据集合集
  • 完美解决Ubuntu的MySQL临时文件夹修改调整
  • shell基础编程
  • 近期代码报错解决笔记
  • apache设置ssl代理
  • 数据库中单表的查询(select)
  • Spring源码-BeanFactory类关系层级
  • Electron 结合 Selenium + chromedriver 驱动服务实现浏览器多开
  • 手持式气象检测设备:便携科技,气象探测
  • shell 发送邮件脚本(免密)
  • Web动画(lottie篇)
  • 昇思25天学习打卡营第20天|CV-ResNet50图像分类
  • grep: /etc/mysql/my.cnf: 没有那个文件或目录
  • 养猫好物|宠物空气净化器是不是智商税?靠谱猫毛空气净化器推荐
  • 【CPS出版】2024年智能计算与数据分析国际学术会议(ICDA 2024,9月6日-8)
  • AutoGen框架革新:解锁新闻稿写作的新境界
  • 数据结构之队列详解
  • [渗透测试] 反序列化漏洞
  • C++ 类型转换 包括C风格的转换、static_cast、const_cast、reinterpret_cast、dynamic_cast、模板特化等
  • 等保通过标准
  • reduceByKey 函数详解
  • CSI-RS在信道中传输的过程