ES支持SpringBoot使用相似於Spring Data Jpa的方式查詢,使得查詢更加方便。java
一、依賴引入spring
compile 「org.springframework.boot:spring-boot-starter-data-elasticsearch:2.1.7.RELEASE」 compile 「org.elasticsearch.plugin:transport-netty3-client:5.6.10」
二、文件配置elasticsearch
yal文件spring-boot
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 名稱應一致ui
config類this
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; } }
三、Repository配置spa
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名稱.net
四、代碼調用netty
@Autowired private TestRepository testRepository; public void test( Long id) { Iterable<IMDBProgram> imdbProgramDTOS = testRepository.findByEpisode(1L); }
P.S:若是啓動報錯code
[ 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");