- 一、创建springboot项目
- 二、导入es依赖坐标
- 三、创建实体类
- 四、创建dao层
- 五、添加配置类
- 六、测试
二、导入es依赖坐标这里不做演示
三、创建实体类org.springframework.boot spring-boot-starter-weborg.springframework.boot spring-boot-starter-data-elasticsearchorg.projectlombok lomboktrue
package com.example.springboot_es.user; import lombok.Data; 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; //indexName 指定索引名称 @document(indexName = "lx-sd") @Data public class Article { @Id @Field(index = false,type = FieldType.Integer) private Integer id; @Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text) private String title; @Field(analyzer = "ik_smart",searchAnalyzer = "ik_smart",store = true,type = FieldType.Text) private String context; @Field(store = true,type = FieldType.Integer) private Integer hits; }四、创建dao层
package com.example.springboot_es.dao; import com.example.springboot_es.user.Article; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Component; import java.util.List; @Component public interface ArticleDao extends ElasticsearchRepository { List findByTitle(String title); List findByTitleOrContext(String title,String context); List findByTitleOrContext(String title, String context, Pageable pageable); }五、添加配置类
spring: elasticsearch: rest: uris: localhost:9200 read-timeout: 30s connection-timeout: 5s六、测试
package com.example.springboot_es; import com.example.springboot_es.dao.ArticleDao; import com.example.springboot_es.user.Article; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import java.util.List; import java.util.Optional; @SpringBootTest public class Estest { @Autowired private ElasticsearchRestTemplate template; @Autowired private ArticleDao articleDao; //通过springboot es向elasticsearch数据库储存一条数据 @Test public void testSave() { //创建文档 Article article = new Article(); article.setId(1); article.setTitle("es搜索"); article.setContext("成功了吗"); //保存文档 articleDao.save(article); } //修改 @Test public void testUpdate() { //判断数据库中是否有你指定的id的文档,如果没有。就进行保存,如果有,就进行更新 //创建文档 Article article = new Article(); article.setId(1); article.setTitle("es搜索1"); article.setContext("成功了吗1"); //保存文档 articleDao.save(article); } //删除 @Test public void testDelete() { // 根据主键删除 articleDao.deleteById(1); } //重新构建数据 @Test public void makeData(){ for (int i = 1; i <= 10; i++) { //创建文档 Article article = new Article(); article.setId(i); article.setTitle("es搜索"+i); article.setContext("成功了吗"+i); article.setHits(100+i); //保存数据 articleDao.save(article); } } //查询所有 @Test public void findAll(){ Iterable all = articleDao.findAll(); for (Article article : all) { System.out.println(article); } } //主键查询 @Test public void testFindById(){ Optional id = articleDao.findById(1); System.out.println(id.get()); } //分页查询 @Test public void testFindAllWithPage(){ //设置分页条件 //page代表页码,从0开始 PageRequest pageRequest = PageRequest.of(1, 3); Page all = articleDao.findAll(pageRequest); for (Article article : all) { System.out.println(article); } } //排序查询 @Test public void testFindWithSort(){ //设置排序条件 Sort sort = Sort.by(Sort.Order.desc("hits")); Iterable all = articleDao.findAll(sort); for (Article article : all) { System.out.println(article); } } //分页加排序查询 @Test public void testFindAllWithPageAndSort(){ //设置排序条件 Sort sort = Sort.by(Sort.Order.desc("hits")); //设置分页条件 PageRequest pageable = PageRequest.of(1, 3, sort); Page page = articleDao.findAll(pageable); for (Article article : page.getContent()) { System.out.println(article); } } //根据标题查询 @Test public void testFindByTitle(){ List es = articleDao.findByTitle("es"); for (Article e : es) { System.out.println(e); } } //根据标题或内容查询 @Test public void testFindByTitleOrContext(){ List es = articleDao.findByTitleOrContext("es", "1"); for (Article e : es) { System.out.println(e); } } //根据标题和内容查询(含分页) @Test public void testFindByTitleOrContextWithPage(){ //设置排序条件 Sort sort = Sort.by(Sort.Order.desc("hits")); //设置分页条件 PageRequest pageRequest = PageRequest.of(1, 3, sort); List es = articleDao.findByTitleOrContext("es", "1", pageRequest); for (Article e : es) { System.out.println(e); } } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)