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

最新文章

  1. Marketing with Microsoft Dynamics CRM IDEA CONFERENCE
  2. CDN下nginx获取用户真实IP地址
  3. 数据库mark
  4. Programming Assignment 4: 8 Puzzle
  5. HDU 2899 Strange fuction 【三分】
  6. df、du、fdisk:Linux磁盘管理三板斧的使用心得(转载)
  7. C++ 学习基础一
  8. Servlet 技术全总结 (已完成,不定期增加内容)
  9. 开发纯ndk程序之环境搭配
  10. 待机状态下,服务类型 WCDMA Service type in Idle mode
  11. SpringMVC 上传下载 异常处理
  12. GitHub上最火的Android开源项目(一)
  13. Android layout属性之gravity和layout_gravity
  14. 第十五节:HttpContext五大核心对象的使用(Request、Response、Application、Server、Session)
  15. Url的拦截问题
  16. 异常处理汇总 ~ 修正果带着你的Code飞奔吧!
  17. 小程序BindTap快速连续点击页面跳转多次
  18. ss-libev控制脚本
  19. CentOS6.9 网络设置
  20. ArrayList、LinkedList、Vector的区别。

热门文章

  1. 5.Vue的组件
  2. DVWA Command Injection 通关教程
  3. plsql安装
  4. Vertica性能分析
  5. linux 查看用户列表
  6. python多条插入问题
  7. Kubernetes ingress 上传文件大小限制
  8. Rancher 部署 loonflow 工单系统
  9. laravel框架中Job和事件event的解析
  10. axios解决跨域问题