JavaEE:SpringBoot集成Elasticsearch搜索
程序员文章站
2022-07-06 16:32:10
前提:配置SpringBoot,见此地址:https://blog.csdn.net/a526001650a/article/details/106687559安装Elasticsearch服务器并启动,见此地址:https://blog.csdn.net/a526001650a/article/details/107235178一、配置Elasticsearch:1.导入Elasticsearch依赖包,在工程/pom.xml中: .....
前提:
配置SpringBoot,见此地址:
https://blog.csdn.net/a526001650a/article/details/106687559
安装Elasticsearch服务器并启动,见此地址:
https://blog.csdn.net/a526001650a/article/details/107235178
一、配置Elasticsearch:
1.导入Elasticsearch依赖包,在工程/pom.xml中:
<dependencies>
<!-- 导入elasticsearch搜索引擎依赖 -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-elasticsearch</artifactId>
<version>4.0.1.RELEASE</version>
</dependency>
</dependencies>
2.配置Elasticsearch服务器地址与端口(在工程/src/main/resources/application.yml中):
server:
port: 10004
spring:
application:
name: mall-search
data: #配置elasticsearch服务器地址与端口
elasticsearch:
cluster-nodes: 127.0.0.1:9300
二、操作索引库的数据:
1.创建实体类映射到索引库:
//与索引库文档相映射的实体类
@Document(indexName = "mall", type = "user") //indexName为索引库名,type为文档名
public class User {
@Id
private String id;
//默认,index表示是否要索引,analyzer配置存储时分司方式,searchAnalyzer配置查询时分词方式
@Field(index = true, searchAnalyzer = "ik_smart", analyzer = "ik_smart")
private String 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;
}
}
2.增删改查业务接口,需要继承ElasticsearchRepository接口:
//索引库增删改查操作接口,需要继承ElasticsearchRepository
public interface UserDao extends ElasticsearchRepository<User, String> { //类似Spring Data JPA
}
3.业务类中增删改查方法:
@Service
public class UserBiz {
@Autowired //自动创建索引库Dao实现类
private UserDao userDao;
//插入
public void insert(User user) {
userDao.save(user);
}
//更新
public void update(User user) {
userDao.save(user);
}
// 根据ID查
public User findById(String id) {
return userDao.findById(id).get();
}
//查全部
public List<User> findAll(int pageIndex, int pageSize) {
Pageable pageable = PageRequest.of(pageIndex, pageSize); //创建分页参数对象
return userDao.findAll(pageable).toList(); //分页获取列表
}
// 模糊匹配
public List<User> search(String key) {
WildcardQueryBuilder queryBuilder = QueryBuilders.wildcardQuery("name", "*" + key + "*");//搜索name中含有key的记录
Pageable pageable = PageRequest.of(0, 20); //创建分页条件
return userDao.search(queryBuilder, pageable).toList();
}
//删除
public void delete(String id) {
userDao.deleteById(id);
}
}
4.测试效果:
@Controller
public class UserController {
@Autowired //自动创建Dao实现类
private UserBiz userBiz;
@RequestMapping("/insert")
@ResponseBody
public String insert() {
User user = new User();
user.setName("杨行生2");
userBiz.insert(user);
return "插入成功";
}
@RequestMapping("/update")
@ResponseBody
public String update() {
User user = new User();
user.setId("8fmKM3MB_eVRoZWmJuBe");
user.setName("杨先生3");
userBiz.update(user);
return "更新成功";
}
@RequestMapping("/findById")
@ResponseBody
public User findById() {
return userBiz.findById("8fmKM3MB_eVRoZWmJuBe");
}
@RequestMapping("/findAll")
@ResponseBody
public List<User> findAll() {
return userBiz.findAll(0, 20);
}
@RequestMapping("/search")
@ResponseBody
public List<User> search() {
return userBiz.search("先");
}
@RequestMapping("/delete")
@ResponseBody
public String delete() {
userBiz.delete("8fmKM3MB_eVRoZWmJuBe");
return "删除成功";
}
}
本文地址:https://blog.csdn.net/a526001650a/article/details/107243495
上一篇: MyElipse切换JDK1.8
下一篇: Comparable接口
推荐阅读
-
SpringBoot 集成 Elasticsearch
-
SpringBoot框架集成ElasticSearch实现过程示例详解
-
JavaEE:SpringBoot集成Elasticsearch搜索
-
elasticsearch springboot 实现分词搜索
-
Springboot集成Elasticsearch的步骤与相关功能
-
Laravel使用scout集成elasticsearch做全文搜索的实现方法
-
APDPlat拓展搜索之集成ElasticSearch
-
SpringBoot 集成 Elasticsearch
-
JavaEE:SpringBoot集成Elasticsearch搜索
-
SpringBoot框架集成ElasticSearch实现过程示例详解