下载Elasticsearch7.6.0window版(官网下载非常慢)
链接:https://pan.baidu.com/s/1-TsJOUmLez9eOr9SB6R2-A
提取码:g5w8
引入依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.hhf.el</groupId>
<artifactId>elsearchdemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>elsearchdemo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.11</java.version>
</properties>
<dependencies>
<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</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
添加配置
server.port=8080
server.servlet.context-path=/hhf
spring.elasticsearch.rest.uris=http://localhost:9200
spring.elasticsearch.rest.username=elastic
spring.elasticsearch.rest.password=123456
项目结构
Model
package com.hhf.el.bean;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import java.io.Serializable;
@Data
@Accessors(chain = true)
@Document(indexName = "blog", type = "java")
public class BlogModel implements Serializable {
private static final long serialVersionUID = 6320548148250372657L;
@Id
private String id;
//@Field(type = FieldType.Keyword)
private String title;
private String content;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
}
BlogRepository
package com.hhf.el.dao;
import com.hhf.el.bean.BlogModel;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
public interface BlogRepository extends ElasticsearchRepository<BlogModel, String> {
}
Result
package com.hhf.el.utils;
public class Result<T> {
private int code;
private String msg;
private T data;
public static <T> Result<T> success() {
return new Result<T>();
}
public static <T> Result<T> success(T data) {
return new Result<>(data);
}
public static <T> Result<T> error(int code, String msg) {
return new Result<>(code, msg);
}
public Result(int code, String msg) {
this.code = code;
this.msg = msg;
this.data = null;
}
public Result(T data) {
this.code = 0;
this.msg = "success";
this.data = data;
}
public Result() {
this.code = 0;
this.msg = "success";
this.data = null;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
Controller
package com.hhf.el.controller;
import com.hhf.el.bean.BlogModel;
import com.hhf.el.dao.BlogRepository;
import com.hhf.el.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
@RestController
@RequestMapping("/blog")
public class BlogController {
@Autowired
private BlogRepository blogRepository;
@PostMapping("/add")
public Result add(@RequestBody BlogModel blogModel) {
blogRepository.save(blogModel);
return Result.success(blogModel);
}
@GetMapping("/get/{id}")
public Result getById(@PathVariable String id) {
if (StringUtils.isEmpty(id))
return Result.error(1,"id为空!");
Optional<BlogModel> blogModelOptional = blogRepository.findById(id);
if (blogModelOptional.isPresent()) {
BlogModel blogModel = blogModelOptional.get();
return Result.success(blogModel);
}
return Result.error(2,"未找到!");
}
@GetMapping("/get")
public Result getAll() {
Iterable<BlogModel> iterable = blogRepository.findAll();
List<BlogModel> list = new ArrayList<>();
iterable.forEach(list::add);
return Result.success(list);
}
@PostMapping("/update")
public Result updateById(@RequestBody BlogModel blogModel) {
String id = blogModel.getId();
if (StringUtils.isEmpty(id))
return Result.error(1,"id为空!");
blogRepository.save(blogModel);
return Result.success(blogModel);
}
@DeleteMapping("/delete/{id}")
public Result deleteById(@PathVariable String id) {
if (StringUtils.isEmpty(id))
return Result.error(1,"id为空!");
blogRepository.deleteById(id);
return Result.success(id);
}
}
测试(使用postman测试)
1、添加一条数据:localhost:8080/hhf/blog/add
{
"title": "测试",
"content": "测试一下!"
}
2、根据id查找数据 localhost:8080/hhf/blog/get/zBID4XIB_ZjyQfo7NJh8
3、查询所有
4、根据id更新数据localhost:8080/hhf/blog/update
{
"id":"zBID4XIB_ZjyQfo7NJh8",
"title": "测试",
"content": "测试一下66666666666666!"
}
结果:
5、根据id删除 localhost:8080/hhf/blog/delete/zBID4XIB_ZjyQfo7NJh8
查找已删除的数据: