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

ES: spring boot中使用ElasticsearchClient

一、依赖:(要根据不同版本的ES来调整依赖,否则会报错,不支持太低版本的ES,比如7.6以下的)

        <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId><version>3.2.3</version></dependency><dependency><groupId>co.elastic.clients</groupId><artifactId>elasticsearch-java</artifactId><version>7.16.0</version></dependency><dependency><groupId>com.fasterxml.jackson.core</groupId><artifactId>jackson-databind</artifactId><version>2.12.3</version></dependency>

二、定义实体类:

package cn.edu.tju.test4;public class Product {private String name;private String desc;private double price;public Product() {}public Product(String name, String desc, double price) {this.name = name;this.desc = desc;this.price = price;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getDesc() {return desc;}public void setDesc(String desc) {this.desc = desc;}public double getPrice() {return price;}public void setPrice(double price) {this.price = price;}@Overridepublic String toString() {return "Product{" +"name='" + name + '\'' +", desc='" + desc + '\'' +", price=" + price +'}';}
}

三、配置ElasticsearchClient

package cn.edu.tju.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;@Configuration
public class ElasticsearchClientConfig {@Value("${spring.elasticsearch.uris}")private String serverAddress;@Beanpublic ElasticsearchClient getElasticsearchClient(){RestClient restClient = RestClient.builder(HttpHost.create(serverAddress)).build();ElasticsearchTransport transport = new RestClientTransport(restClient, new JacksonJsonpMapper());ElasticsearchClient esClient = new ElasticsearchClient(transport);return esClient;}
}

其中ES服务器的地址复用spring data ES 的spring.elasticsearch.uris

四、使用ElasticsearchClient操作索引

package cn.edu.tju.controller;import cn.edu.tju.test4.Product;
import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.elasticsearch.core.GetResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class EsDemoController5 {@Autowiredprivate ElasticsearchClient esClient;@RequestMapping("/test6")public String test6() throws Exception{GetResponse<Product> productGetResponse = esClient.get(g -> g.index("demo7").id("1"),Product.class);String result = productGetResponse.source().toString();return result;}
}

其中ES 7.16.2中demo7这个索引的mapping如下:

{"demo7" : {"mappings" : {"properties" : {"desc" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}},"price" : {"type" : "float"}}}}
}
http://www.lryc.cn/news/322900.html

相关文章:

  • 实体框架EF(Entity Framework)简介
  • 使用CUDA 为Tegra构建OpenCV
  • YoloV8改进策略:BackBone改进|PKINet
  • 如何在Linux系统部署Dupal CMS结合内网穿透实现无公网IP访问web界面
  • 小巧玲珑的SQLite
  • 【Android 内存优化】 native内存泄漏监控方案源码分析
  • 数据结构 二叉树 力扣例题AC——代码以及思路记录
  • Android 11系统启动流程
  • python 爬取杭州小区挂牌均价
  • 数据可视化-ECharts Html项目实战(3)
  • 【理解机器学习算法】之Clustering算法(K-Means)
  • Transformer的前世今生 day02(神经网络语言模型、词向量)
  • 【Linux】多线程编程基础
  • 【地图】腾讯地图 - InfoWindow 自定义信息窗口内容时,内容 html 嵌套混乱问题
  • Vue3、element-plus和Vue2、elementUI的一些转换
  • Go语言gin框架中加载html/css/js等静态资源
  • #鸿蒙生态创新中心#揭幕仪式在深圳湾科技生态园举行
  • flask 继续学习
  • DockerFile遇到的坑
  • 并网型风光储微电网日前优化调度(MATLAB实现)
  • MATLAB环境下基于振动信号的轴承状态监测和故障诊断
  • 流畅的 Python 第二版(GPT 重译)(十二)
  • 【Python 48小时速成 2】关键字
  • 小程序socket 全局代码
  • 数据挖掘|数据集成|基于Python的数据集成关键问题处理
  • Linux-网络层IP协议、链路层以太网协议解析
  • 后端开发辅助
  • 插件电阻的工艺结构原理及选型参数总结
  • 视频私有云,HDMI/AV多硬件设备终端接入,SFU/MCU视频会议交互方案。
  • mac os 配置两个github账号