Elasticsearch-三-SpringDataElasticsearch-搜索

本文详细介绍了如何在SpringBoot项目中集成Elasticsearch,包括环境搭建、依赖引入、配置文件修改、Document类创建、Repository接口定义及测试用例编写,提供了完整的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

环境搭建

本文项目基于 SpringBoot 2.1.3.RELEASE 进行构建,首先引入 Spring Data ElasticSearch 的依赖。

引入依赖 spring-boot-starter-data-elasticsearch

1
2
3
4
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

Spring Data ElasticSearch 与 ElasticSearch 有对应关系

https://github.com/spring-projects/spring-data-elasticsearch


根据上面的版本信息
而本文使用的 SpringBoot 2.1.3.RELEASE 自动依赖的 Spring Data ElasticSearch 版本是 3.1.5.RELEASE,对应的 elasticsearch 版本是 6.4.3

修改application.yml

1
2
3
4
5
6
7
8
spring.data.elasticsearch.repositories.enabled=true
spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300
spring.data.elasticsearch.properties.transport.tcp.connect_timeout=120s
spring.data.elasticsearch.cluster-name=elasticsearch
spring.elasticsearch.jest.read-timeout=3s
spring.elasticsearch.jest.uris=http://localhost:9200
spring.elasticsearch.jest.connection-timeout=5s
spring.elasticsearch.jest.multi-threaded=true

创建Document 类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.Field;
import org.springframework.data.elasticsearch.annotations.FieldType;

@Data
@AllArgsConstructor
@Document(indexName = "es_user",type = "user")
public class User {
private Long id;
@Field(type = FieldType.Text)
private String name;
private int age;
@Field(type = FieldType.Text,analyzer = "ik_smart")
private String description;
}

创建 Repository 接口

1
2
3
4
5
6
7
8
import com.felix.project.model.User;
import org.springframework.data.elasticsearch.repository.ElasticsearchCrudRepository;

import java.util.List;

public interface UserRepository extends ElasticsearchCrudRepository<User,Long> {
List<User> findByName(String name);
}

测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

import com.felix.project.model.User;
import com.felix.project.repository.UserRepository;
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.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.test.context.junit4.SpringRunner;


@RunWith(SpringRunner.class)
@SpringBootTest
public class UserTests {
@Autowired(required = false)
private UserRepository userRepository;

/**
* 创建索引
*/
@Test
public void testUserIndex() {
for (int i = 0; i < 10; i++) {
userRepository.save(new User((long) i, "Felix" + i, i, "Java开发工程师"));
}
}

/**
* 查询所有
*/
@Test
public void findAllTest() {
Iterable<User> list = userRepository.findAll();
for (User user : list) {
System.out.println(user.getId() + " " + user.getName());
}
}

/**
* 查询所有
*/
@Test
public void findAllBySortTest() {
//分页并排序
Iterable<User> list = userRepository.findAll(PageRequest.of(0, 15, Sort.by("id").descending()));
for (User user : list) {
System.out.println(user.getId() + " " + user.getName());
}
}

/**
* 删除索引
*/
@Test
public void deleteTest() {
//根据id删除索引
userRepository.deleteById(1L);
}

/**
* 更新索引
*/
@Test
public void updateTest(){
//Lucene没有更新索引
//只需要保证id是唯一的没有会添加索引,存在则会删除后,重新添加索引
userRepository.save(new User(1L, "Felix" , 12,"Java开发工程师"));
}

/**
* 根据name查询
*/
@Test
public void findByNameTest(){
List<User> felix = userRepository.findByName("Felix");
System.out.println(felix);
}

}

参考

SpringDataElasticsearch 自定义查询参考
https://docs.spring.io/spring-data/elasticsearch/docs/current/reference/html/#elasticsearch.query-methods
https://blog.youkuaiyun.com/larger5/article/details/79777319
https://www.jianshu.com/p/27e1d583aafb

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值