SpringBoot 整合 elasticsearch(本篇为ES基本操作 适合初学者)
相关配置
- 引入 elasticsearch 相关依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
- 配置application.yml
spring:
data:
elasticsearch:
cluster-nodes: 127.0.0.1:9300
SpringBoot提供ES注解介绍
@Document:修饰范围:类上. 作用:代表这个类的实例是一条文档document (indexName属性: 用来限定对象的实例放入ES中的哪个索引库中.
type属性:用来限定对象的实例放入ES中indexName索引中的哪个类型)
@Field:修饰范围:用在成员变量上,表示当前属性为ES中文档一个属性(type属性:用来限定当前索引中当前类型中当前字段在ES中的类型.
analyzer属性:用来指定字段分词时,分词器是哪个“ik_max_word”“ik_smart”)
@Id:修饰范围:用在成员变量上 作用:用来将类中成员变量与ES中的_id 映射
介绍完后我们来演示一个 对象类
package com.domain;
import lombok.Data;
import lombok.experimental.Accessors;
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.util.Date;
/**
* @Author: zh
* @Date: 2020/5/8 14:38
*/
@Data
@Document(indexName = "dangdang",type = "book")
public class Book {
@Id
private Long id;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String name;
@Field(type = FieldType.Double)
private Double price;
@Field(type = FieldType.Date)
private Date pubdate;
@Field(type = FieldType.Text,analyzer = "ik_max_word")
private String content;
@Field(type = FieldType.Keyword)
private String author;
}
基本功能操作
1 使用自定义接口并继承ElasticsearchRepository
package com.Dao;
import com.domain.Book;
import org.springframework.data.elasticsearch.repository.ElasticsearchRepository;
/**
* @Author: zh
* @Date: 2020/5/8 15:33
*/
// 使用自定义接口并继承ElasticsearchRepository 泛型Book 当前操作对象 泛型Long 对象Id数据类型
public interface BookRepository extends ElasticsearchRepository<Book,Long> {
}
2 创建test类 依赖注入 BookRepository 类
package com;
import com.Dao.BookRepository;
import com.domain.Book;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import java.util.Date;
/**
* Unit test for simple App.
*/
@SpringBootTest(classes = App.class)
@RunWith(SpringRunner.class)
public class AppTest
{
@Autowired
private BookRepository bookRepository;
}
3 向ES中 添加|更新 数据 注意:ES中没有update操作 只有save操作,当id存在即更新 不存在则添加
@Test
public void test1(){
Book book = new Book(1L,"小猪佩奇",55.5,new Date(),"小猪小猪 哼哼哼","阿斯特利、贝克、戴维斯");
bookRepository.save(book);
}
之后运行一下我们看看 效果。我们会看到ES中多了‘dangdang’索引 和 刚添加的那条数据 那就说明添加操作成功了
还有一个批量添加|修改 小伙伴们可以自个试一试
@Test
public void findSaveAll(){
Book book0 = new Book(2L,"大象艾米丽",55.5,new Date(),"大象大象 噗噗噗","阿斯特利、贝克、戴维斯");
Book book1 = new Book(3L,"大象艾米丽",55.5,new Date(),"大象大象 噗噗噗","阿斯特利、贝克、戴维斯");
Book book2 = new Book(4L,"大象艾米丽",55.5,new Date(),"大象大象 噗噗噗","阿斯特利、贝克、戴维斯");
List<Book> list=new ArrayList<>();
list.add(book0);
list.add(book1);
list.add(book2);
Iterable<Book> books = list;
Iterable<Book> allById = bookRepository.saveAll(books);
allById.forEach(book -> System.out.println(book));
}
4 查询所有
@Test
public void testFindAll(){
Iterable<Book> books = bookRepository.findAll();
books.forEach(book -> System.out.println(book));
}
查询效果
5 根据id查询
@Test
public void testFindById(){
Optional<Book> book = bookRepository.findById(1L);
System.out.println(book.get());
}
效果
其中还一个 findAllById根据多个id查询 相当于mysql里的in查询 小伙伴们可以自个试一试
@Test
public void findAllById(){
List<Long> list=new ArrayList<>();
list.add(1L);
list.add(2L);
Iterable<Long> books = list;
Iterable<Book> allById = bookRepository.findAllById(books);
allById.forEach(book -> System.out.println(book));
}
6 删除所有
@Test
public void testDelete(){
bookRepository.deleteAll();
}
效果
其中还有 根据id删除 、 根据一个对象来删除 和 批量删除 小伙伴们可以自个试一试
@Test
public void testDeleteByBook(){
Book book = new Book(2L,"大象艾米丽",55.5,new Date(),"大象大象 噗噗噗","阿斯特利、贝克、戴维斯");
bookRepository.delete(book);
}
@Test
public void testDeleteById(){
bookRepository.deleteById(1L);
}
@Test
public void testDeleteAll(){
Book book0 = new Book(5L,"大象艾米丽",55.5,new Date(),"大象大象 噗噗噗","阿斯特利、贝克、戴维斯");
Book book1 = new Book(3L,"大象艾米丽",55.5,new Date(),"大象大象 噗噗噗","阿斯特利、贝克、戴维斯");
Book book2 = new Book(4L,"大象艾米丽",55.5,new Date(),"大象大象 噗噗噗","阿斯特利、贝克、戴维斯");
List<Book> list=new ArrayList<>();
list.add(book0);
list.add(book1);
list.add(book2);
Iterable<Book> books = list;
bookRepository.deleteAll(books);
}
好啦 ES的基本操作就差不多到这了!! 赶明有时间 可以在整一篇 复杂的ES操作。