因为手里的项目是Spring boot 1.5.X的,所以能选择的es版本就需要去对应寻找,我本地搭建的版本是2.4.2版本的。对应的版本选择可以看下图
需要准备elasticsearch服务器,以及对应的可视化的kibana,方便观察操作是否成功。和我版本相似的可以看一下这个网站:link,从上面可以找到对应的工具,还有对应讲解。
在Spring boot 项目的基础上,需要引入对应的依赖。 <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>org.codehaus.jackson</groupId> <artifactId>jackson-mapper-asl</artifactId> <version>1.9.13</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.dataformat</groupId> <artifactId>jackson-dataformat-xml</artifactId> <version>2.6.0</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.6.0</version> </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-data-elasticsearch</artifactId> </dependency> 但是调用个别方法时就会出现jackson版本问题,所以我就直接引入对应jackson依赖了。 然后再yml配置文件中需要添加 data: elasticsearch: cluster-nodes: 127.0.0.1:9300 因为我是本机搭建,所以IP就直接放1270.0.1就可以了。
首先编写对应的实体类: `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;
import java.io.Serializable;
/**
@author@despriction@date 2020/7/1 0030 */@Document(indexName = “test”,type = “category”) public class Category implements Serializable { @Id private int id; @Field(type = FieldType.String ) private String name; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } }` 其中的 @Document注解就表明了要连接到 ElasticSearch 的哪个索引和哪个 type 上;@Id注解就表明该字段是主键,是会和es中_id匹配的。
这个只要继承了ElasticsearchRepository就可以了,需要注意的是ElasticsearchRepository后面的泛型,第一的值对应的是操作的对象类型,也就是对应实体类,第二个值是操作对象的主键类型,就是实体类中主键的类型。写好以后就可以使用了。
这个controller 实现封装的接口
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import thirdquery.domain.workspace.pojo.Category; import java.util.List; /** * @author * @despriction * @date 2020/7/1 0001 */ public interface CategoryApi { @RequestMapping("/addCategory") void addCategory( @RequestBody Category c); @RequestMapping("/deleteCategory") void deleteCategory(@RequestBody Category c); @RequestMapping("/updateCategory") void updateCategory(@RequestBody Category c); @RequestMapping("/editCategory/{id}") @ResponseBody Category ediitCategory(@PathVariable String id); @RequestMapping("/findAll") Iterable<Category> findAll(); @RequestMapping("/getByName/{name}") List<Category> getByName(@PathVariable String name); }这块就没有什么好说的,都是直接使用ElasticsearchRepository封装好的方法进行操作。
使用postman 调用接口,再通过kibana工具查看就可以了。kibana工具用法在这里面也有讲解。link.