码迷,mamicode.com
首页 > 编程语言 > 详细

elasticsearch在spring boot项目下的应用

时间:2017-09-29 01:43:27      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:repo   opera   response   service   conf   数据   cluster   packages   auth   

 这里下载的是elasticsearch2.4.4的版本

 

elasticsearch-head-master

下载地址https://github.com/mobz/elasticsearch-head

技术分享

修改下图文件

技术分享

技术分享

双击如下图bat

技术分享

http://192.168.2.104:9200/_plugin/head/,192.168.2.104是刚才在配置文件配置的本机ip

技术分享

也可以直接生成win服务

技术分享

 

技术分享

 

 maven添加如下jar包

<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配置文件里面是对应的不然会报错,端口号默认也是这个。

下面是配置对应的索引和类型

技术分享

下面创建一个接口扩展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  

elasticsearch在spring boot项目下的应用

标签:repo   opera   response   service   conf   数据   cluster   packages   auth   

原文地址:http://www.cnblogs.com/ok123/p/7527701.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!