说在前头:本人为大二在读学生,书写文章的目的是为了对自己掌握的知识和技术进行一定的记录,同时乐于与大家一起分享,因本人资历尚浅,发布的文章难免存在一些错漏之处,还请阅读此文章的大牛们见谅与斧正。若在阅读时有任何的问题,也可通过评论提出,本人将根据自身能力对问题进行一定的解答。
前言
一、引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.5.2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
<version>2.5.2</version>
</dependency>
二、配置文件
spring:
data:
elasticsearch:
cluster-nodes: 127.0.0.1:9200 # Elasticsearch地址
cluster-name: elasticsearch # 默认为elasticsearch
repositories:
enabled: true # 开启本地存储
三、索引对象
package com.example.estest.entity;
import org.springframework.data.annotation.Id;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import java.io.Serializable;
@Document(indexName = "book")
public class Book implements Serializable {
@Id
private String id;
@Field
private String name;
public Book(String id, String name) {
this.id = id;
this.name = name;
}
@Override
public String toString() {
return "Book{" +
"id='" + id + '\'' +
", name='" + name + '\'' +
'}';
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
四、Dao层对象
package com.example.estest.repository;
import com.example.estest.entity.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
import org.springframework.stereotype.Repository;
import java.util.List;
@Repository
public interface BookRepository extends ElasticsearchRepository<Book, String> {
List<Book> findBooksByName(String name);
}
五、编写测试类
package com.example.estest;
import com.example.estest.entity.Book;
import com.example.estest.repository.BookRepository;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class ESTestApplicationTests {
@Autowired
BookRepository bookRepository;
/*
* 插入操作
*/
@Test
void insertTest() {
Book book = new Book("1", "基督山伯爵");
bookRepository.save(book);
System.out.println("插入操作执行成功!");
findAllTest();
}
/*
* 通过文档id进行删除
*/
@Test
void deleteByIdTest() {
bookRepository.deleteById("1");
System.out.println("删除操作执行成功!");
findAllTest();
}
/*
* 修改操作
*/
@Test
void updateTest() {
Book book = bookRepository.findById("1").get();
book.setName("克里斯汀");
bookRepository.save(book);
System.out.println("修改操作执行成功!");
findByIdTest();
}
/*
* 通过文档id查找
*/
@Test
void findByIdTest() {
System.out.println("搜索结果:" + bookRepository.findById("1"));
}
/*
* 查找该索引下所有文档
*/
@Test
void findAllTest() {
Iterable<Book> iter = bookRepository.findAll();
System.out.println("该索引下所有文档:");
for (Book book : iter) {
System.out.println(book);
}
}
}
测试
1.增
2.查
3.改
4.删
文章转载自云丶言,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。









