elasticsearch在spring boot項目下的應用

 這裏下載的是elasticsearch2.4.4的版本html

 

elasticsearch-head-masterjava

下載地址https://github.com/mobz/elasticsearch-headnode

修改下圖文件git

雙擊以下圖batgithub

http://192.168.2.104:9200/_plugin/head/,192.168.2.104是剛纔在配置文件配置的本機ipspring

也能夠直接生成win服務app

 

 

 maven添加以下jar包elasticsearch

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>1.5.1.RELEASE</version>
</dependency>
增長一個es的配置文件
@Configuration
@EnableElasticsearchRepositories(basePackages = "com.example.demo.dao")
public class EsConfig {

    @Value("${elasticsearch.host}")
    private String EsHost;

    @Value("${elasticsearch.port}")
    private int EsPort;

    @Value("${elasticsearch.clustername}")
    private String EsClusterName;

    @Bean
    public Client client() throws Exception {

        Settings esSettings = Settings.settingsBuilder()
                .put("cluster.name", EsClusterName)
                .build();

        //https://www.elastic.co/guide/en/elasticsearch/guide/current/_transport_client_versus_node_client.html
        return TransportClient.builder()
                .settings(esSettings)
                .build()
                .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName(EsHost), EsPort));
    }

    @Bean
    public ElasticsearchOperations elasticsearchTemplate() throws Exception {
        return new ElasticsearchTemplate(client());
    }

  

這個必須和es配置文件裏面是對應的否則會報錯,端口號默認也是這個。maven

下面是配置對應的索引和類型ide

下面建立一個接口擴展es的JPA,這裏實現了基本的增刪改查

public interface UzaiProductRepository extends ElasticsearchRepository<EsProduct, String> {

    EsProduct save(EsProduct esProduct);
    /*
     *
     * @author:   wangchao
     * @date:     2017/9/22
     * @methodName:     分頁取數據
     * @param null
     * @return    
     */
    Page<EsProduct> findByLocationName(String locationName, Pageable pageable);
}

 下面是經過id實現了一個簡單的查詢
@GetMapping("/search") public ResponseEntity<EsProduct> getSearch(@RequestParam("id") String id) { EsProduct esProduct=uzaiProductService.findOne(id); return new ResponseEntity<EsProduct>(esProduct, HttpStatus.OK); }

到此基本spring boot下實現了es的curd  

相關文章
相關標籤/搜索