SpringBoot集成Elasticsearch并进行增删改查操作

2021年12月8日 183点热度 0条评论

SpringBoot集成Elasticsearch并进行增删改查操作

今天给大家简单的介绍一下SpringBoot如何集成Elasticsearch,并简单的介绍一下基于SpringBoot模式下怎么进行简单的增删改查操作,这边增删改查操作有点类似于JPA的模式。(什么是JPA模式,大家可以自行搜索答案)

想学习分布式、微服务、JVM、多线程、架构、java、python的童鞋,千万不要扫码,否则后果自负~

%title插图%num

废话不多说,现在马上开始我们今天的内容。如何新建Springboot项目我这边就不废话了,不会的同学可以看我以前写的教程。

文章地址:https://blog.csdn.net/linzhiqiang0316/article/details/79177132

1.首先是引入相关的依赖,下面是我的pom文件。



	4.0.0

	com.elasticsearch
	elasticsearch
	0.0.1-SNAPSHOT
	jar

	ElasticSearch
	ElasticSearch project for Spring Boot

	
		org.springframework.boot
		spring-boot-starter-parent
		2.0.2.RELEASE
		 
	

	
		UTF-8
		UTF-8
		1.8
	

	
		
		
			com.google.code.gson
			gson
			2.8.0
		

		
		
			org.springframework.boot
			spring-boot-starter-data-elasticsearch
			2.0.2.RELEASE
		
		
			org.springframework.boot
			spring-boot-starter-web
		

		
			org.springframework.boot
			spring-boot-starter-test
			test
		
	

	
		
			
				org.springframework.boot
				spring-boot-maven-plugin
			
		
	

pom.xml文件中最重要的其实就是引入ES(Elasticsearch的简称后面我都这么叫),也就是spring-boot-starter-data-elasticsearch 依赖

2.接下来就是对应的配置文件了,具体配置文件如下所示:

# elasticsearch集群名称,默认的是elasticsearch
spring.data.elasticsearch.cluster-name=my-application

#节点的地址 注意api模式下端口号是9300,千万不要写成9200
spring.data.elasticsearch.cluster-nodes=192.168.11.24:9300

#是否开启本地存储
spring.data.elasticsearch.repositories.enable=true

3.索引对应的实体类如下所示:

package com.elasticsearch.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;

/**
 * @author linzhiqiang
 * @date 2018/5/16
 */
@Document(indexName = "company",type = "employee", shards = 1,replicas = 0, refreshInterval = "-1")
public class Employee {
    @Id
    private String id;
    @Field
    private String firstName;
    @Field
    private String lastName;
    @Field
    private Integer age = 0;
    @Field
    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;
    }
}

4.实体类对应的dao接口如下所示:

package com.elasticsearch.dao;

/**
 * Created by 19130 on 2018/5/16.
 */
import com.elasticsearch.entity.Employee;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Component;
/**
 * @author linzhiqiang
 */
@Component
public interface EmployeeRepository extends ElasticsearchRepository{

    /**
     * 查询雇员信息
     * @param id
     * @return
     */
    Employee queryEmployeeById(String id);
}

5.实体类对应的控制类如下所示:

package com.elasticsearch.controller;
import com.elasticsearch.dao.EmployeeRepository;
import com.elasticsearch.entity.Employee;
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;

/**
 * @author linzhiqiang
 */
@RestController
@RequestMapping("es")
public class EmployeeController {

    @Autowired
    private EmployeeRepository employeeRepository;

    /**
     * 添加
     * @return
     */
    @RequestMapping("add")
    public String add() {
        Employee employee = new Employee();
        employee.setId("1");
        employee.setFirstName("xuxu");
        employee.setLastName("zh");
        employee.setAge(26);
        employee.setAbout("i am in peking");
        employeeRepository.save(employee);
        System.err.println("add a obj");
        return "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;
    }
}  

以上所有的增删改查操作都要基于搭建好的ES系统(关于Linux上面如何搭建ES系统大家可以自行google)

关于我踩过的坑:

1.ES中API的端口号是9300而不是9200。

2.ES系统中Elasticsearch.yml配置文件中要加入network.host: 0.0.0.0,否则外网地址访问不了。

3.最新的资料一定要去官网上面查看,博客上面好多都是过时的。官网地址:https://www.elastic.co

4.注意JDK、ES、Springboot三者之间的版本,很多时候错误都是版本冲突引起的。

下一节课程将会给大家介绍一下,Java是如何通过ES提供的API进行索引的增删改查操作。(和今天的模式不一样哦

对文章有什么疑问或者想要看更多文章可以加我订阅号,欢迎大家的踩踩~

%title插图%num

 

文章来源于互联网:SpringBoot集成Elasticsearch并进行增删改查操作

harry

这个人很懒,什么都没留下

文章评论