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