Springboot 整合 Elasticsearch 及其中的一些坑

1. 话不多说,先晒出pom文件中的依赖

<dependencies>
        <dependency>
            <groupId>org.elasticsearch</groupId>
            <artifactId>elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.8.5</version>
        </dependency>

    </dependencies>

lombok是一个不用自己去给POJO定义getter和setter的jar包,idea或者eclipse都需要安装插件才能使用,安装方法此处就不赘述了。

2. 配置文件 aplication.yml

spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch # 默认为elasticsearch
      cluster-nodes: 127.0.0.1:9300
      repositories:
        enabled: true

这里有个要注意的地方就是,在elasticsearch/conf/elasticsearch.yml中添加如下配置

http.cors.enabled: true
http.cors.allow-origin: "*"
network.host: 0.0.0.0
transport.tcp.port: 9300
transport.tcp.compress: true

network.host 是用于客户端连接的IP地址, 对应配置文件中的spring.data.cluster-nodes后的IP地址,一般配置为0.0.0.0即可。

tansport.tcp.port 是用于客户端连接的端口, 对应配置文件中的spring.data.cluster-nodes后的端口。(我之前一直以为是http.port,结果项目启动的时候总是报错)

3. 新建实体,这里由于使用了lombok框架,所以没有getter/setter已经tostring和构造函数等一系列的方法,代码比较简洁。

import lombok.Data;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Document(indexName = "company", type = "employee", shards = 1, replicas = 0, refreshInterval = "-1")
@Data
public class Employee {

    @Id
    private String id;

    @Field(type = FieldType.Text)
    private String firstName;

    @Field(type = FieldType.Text)
    private String lastName;

    @Field(type = FieldType.Integer)
    private Integer age = 0;

    @Field(type = FieldType.Text)
    private String about;
}

4. dao层

import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;

@Component
public interface EmployeeRepository extends ElasticsearchRepository<Employee, String> {

    Employee queryEmployeeById(String id);
}

5. 由于是入门级的,就不写service层了,直接controller好了

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/es")
public class ElasticSearchController {
    @Autowired
    private EmployeeRepository er;

    @RequestMapping(value = "/add")
    public String add() {
        Employee employee = new Employee();
        employee.setId("1");
        employee.setFirstName("海波");
        employee.setLastName("夏");
        employee.setAge(24);
        employee.setAbout("I am in coding");
        er.save(employee);
        System.out.println("add a obj");
        return "success";
    }

    @RequestMapping(value = "/delete")
    public String delete() {
        er.deleteById("1");
        return "success";
    }

    @RequestMapping(value = "/update")
    public String update() {
        Employee employee = er.queryEmployeeById("1");
        employee.setFirstName("哈哈");
        er.save(employee);
        return "success";
    }

    //查询
    @RequestMapping("/query")
    public Employee query(){

        Employee accountInfo=er.queryEmployeeById("1");
        System.err.println(new Gson().toJson(accountInfo));

        return accountInfo;
    }
}

5. 到这里基本就成功了,可以启动,并测试了。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值