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. Excel-数据透视表

    例如: 购买数量采用求和的方式 用户数ID数据采用计数的方式 一.数据透视表的结构 二.数据透视表的步骤 1.订单表 提出问题,理解数据,数据清晰,构建模型,数据可视化 问题1:每个客户的订单量? 问 ...

  2. ESA2GJK1DH1K升级篇: 快速的移植升级程序到自己的项目(BootLoader程序制作)

    前言 此代码兼容STM32F103全系列 为避免添加上升级程序造成内存不足,请使用128KB Flash及其以上的型号 这篇文章是为了能够让大家快速移植我的升级模板程序到自己的项目 BootLoade ...

  3. 简析平衡树(四)——FHQ Treap

    前言 好久没码过平衡树了! 这次在闪指导的指导下学会了\(FHQ\ Treap\),一方面是因为听说它可以可持久化,另一方面则是因为听说它是真的好写. 简介 \(FHQ\ Treap\),又称作非旋\ ...

  4. [LeetCode] 222. Count Complete Tree Nodes 求完全二叉树的节点个数

    Given a complete binary tree, count the number of nodes. Note: Definition of a complete binary tree ...

  5. pymongo方法详解

    1.连接mongodb ######### 方法一 ########## import pymongo # MongoClient()返回一个mongodb的连接对象client client = p ...

  6. Vertica性能分析

    Vertica的特点简单的说可以总结为:列存储.MPP架构.技术比较新.列存储本身带来了数据高度压缩的便利,MPP架构使得可以用相对廉价的PC级服务器横向扩展到较大规模(PB级),05年才问世使得它在 ...

  7. 解决IDEA卡顿的问题(Windows和Mac)

    IDEA卡顿 最近一段时间经常会在开发的时候感觉到 IDEA 很卡,在一个类里上下滚动或者切换类文件时都能够明显的感觉到,我以为是我项目打开的太多了,毕竟内存优化已经做过了,但是今天实在是被这玩意儿卡 ...

  8. 《Linux就该这么学》培训笔记_ch02_一些必须掌握的Linux命令

    本文在原来作者的基础上做一些符合自己的修改.原文参考: <Linux就该这么学>培训笔记_ch02_一些必须掌握的Linux命令.     本章的内容虽然多,基本都是书本原话,但是笔记能精 ...

  9. 使用python远程连接数据库

    根据web连接服务的原理,我们可以通过一台电脑连接我们另一台电脑上的数据库 一.开启数据库的权限1.Mysql:1)修改访问权限首先修改可以访问的ip,把‘localhost’全部修改为‘%’,打开c ...

  10. UI事件定位--HitTest

    In computer graphics programming, hit-testing (hit detection, picking, or pick correlation) is the p ...