首页 技术 正文
技术 2022年11月6日
0 收藏 999 点赞 521 浏览 3911 个字

ES支持SpringBoot使用类似于Spring Data Jpa的方式查询,使得查询更加方便。

1、依赖引入

compile “org.springframework.boot:spring-boot-starter-data-elasticsearch:2.1.7.RELEASE”
compile “org.elasticsearch.plugin:transport-netty3-client:5.6.10”

2、文件配置

yal文件

spring:
data:
elasticsearch:
cluster-name: cluster-stress-test
address: 10.0.230.97
port:
repositories:
enabled: true

P.S:cluster-name为集群名称,与es安装目录下的elasticsearch.yml 名称应一致

springboot集成spring data ElasticSearch

config类

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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;import java.net.InetAddress;@Configuration
public class ElasticSearchConfig {
private Logger logger = LoggerFactory.getLogger(this.getClass()); @Value("${spring.data.elasticsearch.address}")
private String ip;
@Value("${spring.data.elasticsearch.port}")
private String port;
@Value("${spring.data.elasticsearch..cluster-name}")
private String clusterName; @Bean
public TransportClient getTransportClient() {
TransportClient transportClient = null;
try {
Settings settings = Settings.builder()
.put("cluster.name",clusterName)
.put("client.transport.sniff",true)
.build();
transportClient = new PreBuiltTransportClient(settings);
TransportAddress firstAddress = new TransportAddress(InetAddress.getByName(ip),Integer.parseInt(port));
transportClient.addTransportAddress(firstAddress);
}catch (Exception e){
e.printStackTrace();
logger.error("getTransportClient fail:" + e.getMessage(),e);
}
return transportClient;
}
}

3、Repository配置

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import java.util.List;public interface TestRepository extends ElasticsearchRepository<IMDBProgram,String> { List<IMDBProgram> findByEpisode(Long eps);
}
import com.fasterxml.jackson.annotation.JsonProperty;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;@Document(indexName = "subprogram_data_test_3",type = "docs")
public class IMDBProgram { @Id
private Long id; @Field
@JsonProperty("subprogram_id")
private Long subprogramId; @Field
@JsonProperty("program_id")
private Long programId; @Field
private Integer episode; @Field
private Integer paragraph; @Field
@JsonProperty("direct_weight")
private Integer directWeight; @Field
@JsonProperty("source_type")
private String sourceType; @Field
@JsonProperty("delete_flag")
private String deleteFlag; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public Long getSubprogramId() {
return subprogramId;
} public void setSubprogramId(Long subprogramId) {
this.subprogramId = subprogramId;
} public Long getProgramId() {
return programId;
} public void setProgramId(Long programId) {
this.programId = programId;
} public Integer getEpisode() {
return episode;
} public void setEpisode(Integer episode) {
this.episode = episode;
} public Integer getParagraph() {
return paragraph;
} public void setParagraph(Integer paragraph) {
this.paragraph = paragraph;
} public Integer getDirectWeight() {
return directWeight;
} public void setDirectWeight(Integer directWeight) {
this.directWeight = directWeight;
} public String getSourceType() {
return sourceType;
} public void setSourceType(String sourceType) {
this.sourceType = sourceType;
} public String getDeleteFlag() {
return deleteFlag;
} public void setDeleteFlag(String deleteFlag) {
this.deleteFlag = deleteFlag;
}
}

P.S:实体类映射中的indexName 为索引名称,type对应es的type名称

4、代码调用

    @Autowired
private TestRepository testRepository; public void test( Long id) {
Iterable<IMDBProgram> imdbProgramDTOS = testRepository.findByEpisode(1L);
}

P.S:如果启动报错

[ ERROR] [2019-09-11 15:34:01] com.xx.xx.config.ElasticSearchConfig$$EnhancerBySpringCGLIB$$90ae5110 [39] - getTransportClient fail:availableProcessors is already set to [8], rejecting [8]

在启动类中加上

System.setProperty("es.set.netty.runtime.available.processors","false");
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,489
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,290