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 名称应一致

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");

springboot集成spring data ElasticSearch的更多相关文章

  1. SpringBoot整合Spring Data Elasticsearch

    Spring Data Elasticsearch提供了ElasticsearchTemplate工具类,实现了POJO与elasticsearch文档之间的映射 elasticsearch本质也是存 ...

  2. springboot集成Spring Data JPA数据查询

    1.JPA介绍 JPA(Java Persistence API)是Sun官方提出的Java持久化规范.它为Java开发人员提供了一种对象/关联映射工具来管理Java应用中的关系数据.它的出现主要是为 ...

  3. Spring Boot + Spring Data + Elasticsearch实例

    Spring Boot + Spring Data + Elasticsearch实例 学习了:https://blog.csdn.net/huangshulang1234/article/detai ...

  4. Springboot集成Spring Batch

    Spring官网 (https://spring.io/projects/spring-batch#overview)对Spring  Batch的解释: 一个轻量级的.全面的批处理框架,用于开发对企 ...

  5. elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    一.ES Client 简介 1. ES是一个服务,采用C/S结构 2. 回顾 ES的架构 3. ES支持的客户端连接方式 3.1 REST API ,端口 9200 这种连接方式对应于架构图中的RE ...

  6. Elasticsearch Java client(ES Client 简介、Java REST Client、Java Client、Spring Data Elasticsearch)

    elasticsearch系列七:ES Java客户端-Elasticsearch Java client(ES Client 简介.Java REST Client.Java Client.Spri ...

  7. 031 Spring Data Elasticsearch学习笔记---重点掌握第5节高级查询和第6节聚合部分

    Elasticsearch提供的Java客户端有一些不太方便的地方: 很多地方需要拼接Json字符串,在java中拼接字符串有多恐怖你应该懂的 需要自己把对象序列化为json存储 查询到结果也需要自己 ...

  8. SpringBoot 集成 Spring Session

    SpringBoot 集成 Spring Session 应该讲解清楚,为什么要使用 Redis 进行 Session 的管理. Session 复制又是什么概念. Spring Session 在汪 ...

  9. 3.4_springboot2.x整合spring Data Elasticsearch

    Spring Data Elasticsearch 是spring data对elasticsearch进行的封装. 这里有两种方式操作elasticsearch: 1.使用Elasticsearch ...

随机推荐

  1. Ubuntu放弃战斗, Linux桌面的悲哀 - 简书

    Ubuntu放弃战斗, Linux桌面的悲哀 - 简书 https://www.jianshu.com/p/86dd6e34ce91

  2. 8.18 NOIP模拟测试25(B) 字符串+乌鸦喝水+所驼门王的宝藏

    T1 字符串 卡特兰数 设1为向(1,1)走,0为向(1,-1)走,限制就是不能超过$y=0$这条线,题意转化为从(0,0)出发,走到(n+m,n-m)且不越过$y=0$,然后就裸的卡特兰数,$ans ...

  3. [LeetCode] 239. Sliding Window Maximum 滑动窗口最大值

    Given an array nums, there is a sliding window of size k which is moving from the very left of the a ...

  4. 算法马拉松35 E 数论只会Gcd - 类欧几里得 - Stern-Brocot Tree - 莫比乌斯反演

    题目传送门 传送门 这个官方题解除了讲了个结论,感觉啥都没说,不知道是因为我太菜了,还是因为它真的啥都没说. 如果 $x \geqslant y$,显然 gcd(x, y) 只会被调用一次. 否则考虑 ...

  5. 关于App收集个人信息基本规范,这里公开征求你的意见!

    关于App收集个人信息基本规范,这里公开征求你的意见! https://www.thepaper.cn/newsDetail_forward_4122573 以后国家开始规范App收集个信息基本规范, ...

  6. Python连载28-logging设置&logger解析

    一.logging模块讲解 1.函数:logging.basicConfig() 参数讲解: (1)level代表高于或者等于这个值时,那么我们才会记录这条日志 (2)filename代表日志会写在这 ...

  7. ROS融合IMU笔记

    ROS官网有一个叫robot_pose_ekf的包,是专门处理传感器融合的包,具体介绍:http://wiki.ros.org/robot_pose_ekf 其中主要功能是订阅主题包括odom(里程计 ...

  8. 第十一次作业 LL(1)文法的判断,递归下降分析程序

    1. 文法 G(S): (1)S -> AB (2)A ->Da|ε (3)B -> cC (4)C -> aADC |ε (5)D -> b|ε 验证文法 G(S)是不 ...

  9. TF-IDF & CNN

    TF-IDF----------------------------------------------------------------认为一个单词出现的文本频率越小,它区别不同类别的能力就越大, ...

  10. Mysql系列(五)—— 分页查询及问题优化

    一.用法 在Mysql中分页查询使用关键字limit.limit的语法如下: SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15 limit关键字带有 ...