SpringBoot整合ElasticSearch

226 阅读2分钟

SpringBoot整合ElasticSearch

一,创建索引

创建一个名为company,type为employee的索引。

PUT company
 {
   "settings": {
     "number_of_shards": 5, 
     "number_of_replicas": 1
   },
   "mappings": {
     "employee":{
       "properties": {
         "id":{"type": "text"},
         "firstName":{"type": "keyword"},
         "lastName":{"type": "keyword"},
         "age":{"type": "long"},
         "about":{"type": "text"}
       }
     }
   }
 }

查看索引结构

GET company/_mapping
结果:
{
  "company": {
    "mappings": {
      "employee": {
        "properties": {
          "about": {
            "type": "text"
          },
          "age": {
            "type": "long"
          },
          "firstName": {
            "type": "keyword"
          },
          "id": {
            "type": "text"
          },
          "lastName": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

完成以上后,Elasticsearch索引就说明创建好了。

查看company下是不是有document

GET /company/employee/1 
结果:
{
  "_index": "company",
  "_type": "employee",
  "_id": "1",
  "found": false
}

二,创建SpringBoot应用

  • 添加elasticsearch和jackson依赖

    导入依赖jackson依赖的时候,注意版本冲突,我使用的是2.6.0,太高版本会和spring冲突

        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.6.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.6.0</version>
        </dependency>
        
        <!--引入elasticsearch依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
            <version>6.3.2</version>
        </dependency>
  • 向配置文件(application.properties或者application.yml)中添加elasticsearch的配置,yml为例

集群名查看命令

http://127.0.0.1:9200/
结果
{
  "name" : "jzDOyhy",  # 节点名
  "cluster_name" : "elasticsearch",  #默认是elasticsearch
  "cluster_uuid" : "p0MLM3GIRO2DgIpOLVIZJg",
  "version" : {
    "number" : "6.3.2",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "053779d",
    "build_date" : "2018-07-20T05:20:23.451332Z",
    "build_snapshot" : false,
    "lucene_version" : "7.3.1",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

yml配置文件添加配置

spring:
  data:
    elasticsearch:
      cluster-name: elasticsearch   #集群名
      cluster-nodes: 127.0.0.1:9300 
      repositories:
        enabled: true
  • 给Elasticsearch中的索引创建一个java实体
@Document(indexName = "company",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1")
public class Employee {
    @Id
    private String id;
    @Field(type = FieldType.Keyword)
    private String firstName;
    @Field(type = FieldType.Keyword)
    private String lastName;
    @Field(type = FieldType.Long)
    private Integer age = 0;
    @Field(type = FieldType.Text)
    private String about;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getAbout() {
        return about;
    }

    public void setAbout(String about) {
        this.about = about;
    }
}
  • 创建dao
@Component
public interface EmployeeRepository extends ElasticsearchRepository<Employee,String> {
    Employee queryEmployeeById(String id);
}
  • 创建Controller
@RestController
public class EmployeeController {

    @Autowired
    private EmployeeRepository employeeRepository;

    @RequestMapping("add")
    public String add() {
        Employee employee = new Employee();
        employee.setId("1");
        employee.setFirstName("你好");
        employee.setLastName("中国");
        employee.setAge(26);
        employee.setAbout("我爱你中国");
        employeeRepository.save(employee);
        System.err.println("add a obj");
        return "add a obj to elasticsearch success";
    }

    /*   
     * 删除
     * @return*/
     
    @RequestMapping("delete")
    public String delete() {
        Employee employee = employeeRepository.queryEmployeeById("1");
        employeeRepository.delete(employee);
        return "success";
    }
    /*
     * 局部更新
     * @return*/
     
    @RequestMapping("update")
    public String update() {
        Employee employee = employeeRepository.queryEmployeeById("1");
        employee.setFirstName("哈哈");
        employeeRepository.save(employee);
        System.err.println("update a obj");
        return "success";
    }
    /*
     * 查询
     * @return*/
     
    @RequestMapping("query")
    public Employee query() {
        Employee accountInfo = employeeRepository.queryEmployeeById("1");
        //System.err.println(new Gson().toJson(accountInfo));
        return accountInfo;
    }
}
  • 启动SpringBoot应用
通过以下方式访问项目
http://localhost:8080/add

看到页面返回成功!

在kibana中查看结果

GET /company/employee/1 
结果
{
  "_index": "company",
  "_type": "employee",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "id": "1",
    "firstName": "你好",
    "lastName": "中国",
    "age": 26,
    "about": "我爱你中国"
  }
}

三,Elasticsearch封装实现好了CRUD

在这里插入图片描述