springboot集成spring data ElasticSearch

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

 

转载于:https://www.cnblogs.com/gethinwang/p/11506613.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值