- 一、Spring Data简介
- 二、Spring Data ElasticSearch简介
- 二、基础入门案例
- 1.导入Spring Data ElasticSearch坐标
- 2.启动器配置文件
- 3.编写实体Article
- 4.编写Dao
- 5.创建测试类
一、Spring Data简介
Spring Data是一个用于简化数据库访问,并支持云服务的开源框架。其主要目标是使得对数据的访问变得方便快捷,并支持map-reduce框架和云计算数据服务。 Spring Data可以极大的简化JPA的写法,可以在几乎不用写实现的情况下,实现对数据的访问和 *** 作。除了CRUD外,还包括如分页、排序等一些常用的功能。
二、Spring Data ElasticSearch简介Spring Data ElasticSearch 基于 spring data API 简化 elasticSearch *** 作,将原始 *** 作elasticSearch的客户端API 进行封装 。Spring Data为Elasticsearch项目提供集成搜索引擎。Spring Data Elasticsearch POJO的关键功能区域为中心的模型与Elastichsearch交互文档和轻松地编写一个存储库数据访问层。
二、基础入门案例 1.导入Spring Data ElasticSearch坐标2.启动器配置文件4.0.0 org.springframework.boot spring-boot-starter-parent2.1.16.RELEASE com.example demo0.0.1-SNAPSHOT demo Demo project for Spring Boot 1.8 org.springframework.boot spring-boot-starter-data-elasticsearchorg.springframework.boot spring-boot-starter-testtest org.springframework.boot spring-boot-maven-plugin
package com.example.demo; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class DemoApplication { public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } }
spring: data: elasticsearch: cluster-name: my-elasticsearch cluster-nodes: 192.168.220.100:93003.编写实体Article
@document(indexName = "lxs_blog", type = "article") public class Article { @Id @Field(type = FieldType.Long, store = true) private long id; @Field(type = FieldType.Text, store = true, analyzer = "ik_smart") private String title; @Field(type = FieldType.Text, store = true, analyzer = "ik_smart") private String content; 。。。 }4.编写Dao
方法命名规则查询的基本语法findBy + 属性 + 关键词 + 连接符 + 属性 + 关键词。。。
方法中使用like才会分词
package com.lxs.esdemo2.dao; import com.lxs.esdemo2.domain.Article; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import java.util.List; public interface ArticleDao extends ElasticsearchRepository { public List findByTitleLike(String title); public List findByTitle(String title); public List findByTitleLikeOrContent(String title, String content); public List findByTitleOrContent(String title, String content, Pageable pageable); }5.创建测试类
package com.lxs.esdemo2; import com.lxs.esdemo2.dao.ArticleDao; import com.lxs.esdemo2.domain.Article; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; 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.Pageable; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.aggregation.AggregatedPage; import org.springframework.data.elasticsearch.core.query.FetchSourceFilter; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.test.context.junit4.SpringRunner; import java.util.List; @RunWith(SpringRunner.class) @SpringBootTest public class EsDemo2ApplicationTests { @Autowired private ArticleDao dao; @Autowired private ElasticsearchTemplate template; @Test public void createIndex() { // template.createIndex(Article.class); //配置mapping template.putMapping(Article.class); } @Test public void adddocument() throws Exception { for (int i = 1; i <= 20; i++) { //创建一个Article对象 Article article = new Article(); article.setId(new Long(i)); article.setTitle("女护士路遇昏迷男子跪地抢救:救人是职责更是本能" + i); article.setContent("这是一个美丽的女护士妹妹" + i); //把文档写入索引库 dao.save(article); } } @Test public void deletedocumentById() throws Exception { dao.deleteById(3l); //全部删除 // articleRepository.deleteAll(); } @Test public void findAll() { dao.findAll().forEach(System.out :: println); } @Test public void findById() { System.out.println(dao.findById(1l)); } @Test public void findByTitle() { dao.findByTitleLike("美丽女护士").forEach(System.out :: println); } @Test public void findByTitleOrContent() { dao.findByTitleLikeOrContent("美丽女护士", "男护士").forEach(System.out :: println); } @Test public void findByTitlePage() { Pageable pageable = PageRequest.of(1, 5); dao.findByTitleOrContent("女护士","女护士", pageable).forEach(System.out :: println); } @Test public void testNativeSearchQuery() throws Exception { NativeSearchQuery query = new NativeSearchQueryBuilder() .withQuery(QueryBuilders.queryStringQuery("女护士").defaultField("title")) .withPageable(PageRequest.of(1, 5)) .build(); template.queryForList(query, Article.class).forEach(System.out :: println); } }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)