一、Spring Data ElasticSearch簡介
1、什么是Spring Data
Spring Data是一個用於簡化數據庫訪問,並支持雲服務的開源框架。其主要目標是使得對數據的訪問變得方便快
捷,並支持map-reduce框架和雲計算數據服務。 Spring Data可以極大的簡化JPA的寫法,可以在幾乎不用寫實現
的情況下,實現對數據的訪問和操作。除了CRUD外,還包括如分頁、排序等一些常用的功能。
Spring Data的官網:http://projects.spring.io/spring-data/
Spring Data常用的功能模塊如下:

2、什么是Spring Data ElasticSearch
Spring Data ElasticSearch 基於 spring data API 簡化 elasticSearch操作,將原始操作elasticSearch的客戶端API
進行封裝 。Spring Data為Elasticsearch項目提供集成搜索引擎。Spring Data Elasticsearch POJO的關鍵功能區域
為中心的模型與Elastichsearch交互文檔和輕松地編寫一個存儲庫數據訪問層。
官方網站:http://projects.spring.io/spring-data-elasticsearch/
二、Spring Data ElasticSearch環境
1、啟動ES集群
2、啟動node.js
3、目錄展示
4、導入依賴
<dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>5.6.8</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>5.6.8</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-to-slf4j</artifactId> <version>2.9.1</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.24</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-simple</artifactId> <version>1.7.24</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.8.1</version> </dependency> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.0.5.RELEASE</version> <exclusions> <exclusion> <groupId>org.elasticsearch.plugin</groupId> <artifactId>transport‐netty4‐client</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.0.4.RELEASE</version> </dependency> </dependencies>
5、applicationContext.xml配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch.xsd"> <!--掃描Dao包,自動創建實例--> <elasticsearch:repositories base-package="com.zn.dao"/> <!--掃描service包,創建service的實體--> <context:component-scan base-package="com.zn.service"/> <!--配置elasticsearch的連接--> <elasticsearch:transport-client id="client" cluster-nodes="127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302" cluster-name="my-elasticsearch"/> <!--ElasticSearch模板對象--> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client"></constructor-arg> </bean> </beans>
6、Article實體
package com.zn.entity; 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; //@Document 文檔對象 (索引信息,文檔類型) @Document(indexName = "es_test03",type = "article") public class Article { @Id //@Id文檔主鍵 唯一標識 //@Field 每個文檔的字段配置(類型、是否分詞、是否存儲、分詞器 ) @Field(type = FieldType.Integer,store = true,index = false) private Integer id; @Field(type = FieldType.text,index = true,store = true,analyzer = "ik_max_word") private String title; @Field(type = FieldType.text,index = true,store = true,analyzer = "ik_max_word") private String content; public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getContent() { return content; } public void setContent(String content) { this.content = content; } @Override public String toString() { return "Article{" + "id=" + id + ", title='" + title + '\'' + ", content='" + content + '\'' + '}'; } }
三、創建索引庫
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; public interface ArticleService { }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; /*創建索引庫*/ @Test public void creatIndex(){ //創建空的索引庫 template.createIndex(Article.class); } }
5、效果展示
四、創建索引庫並指定mappings
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; public interface ArticleService { }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; /*創建索引庫並添加mapping映射*/ @Test public void creatIndexAndMapping(){ template.createIndex(Article.class); template.putMapping(Article.class); } }
5、效果展示
五、創建文檔
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; public interface ArticleService { //創建文檔 public void save(Article article); }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; @Override public void save(Article article) { dao.save(article); } }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; /*創建文檔*/ @Test public void saveDocument(){ Article article=new Article(); article.setId(1); article.setTitle("測試SpringData ElasticSearch"); article.setContent("Spring Data ElasticSearch基於spring data API簡化elasticSearch操作," + "將原始操作elasticSearch的客戶端API進行封裝"); service.save(article); } }
5、效果展示
六、刪除文檔--根據id
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; public interface ArticleService {
//根據文檔id刪除 public void deleteById(Integer id); }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; @Override public void deleteById(Integer id) { dao.deleteById(id); } }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; /*根據id刪除文檔*/ @Test public void deleteDocumentById(){ //根據文檔ID刪除 service.deleteById(1); } }
5、效果展示
七、刪除全部文檔
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; public interface ArticleService { //刪除全部文檔 public void deleteAll(); }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; @Override public void deleteAll() { dao.deleteAll(); } }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; /*刪除全部文檔*/ @Test public void deleteAllDocument(){ //全部刪除 service.deleteAll(); } }
5、效果展示
八、修改文檔
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; public interface ArticleService { //修改文檔 public void save(Article article); }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; @Override public void save(Article article) { dao.save(article); } }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; /*修改文檔,先刪除再修改,調用的還是save方法*/ @Test public void updateDocument(){ Article article=new Article(); article.setId(1); article.setTitle("1[修改]測試SpringData ElasticSearch"); article.setContent("1[修改]Spring Data ElasticSearch基於spring data API簡化elasticSearch操作," + "將原始操作elasticSearch的客戶端API進行封裝"); service.save(article); } }
5、效果展示
九、根據ID查詢文檔
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; import java.util.Optional; public interface ArticleService { //根據ID查詢文檔 public Optional<Article> findById(Integer id); }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.Optional; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; @Override public Optional<Article> findById(Integer id) { return dao.findById(id); } }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Optional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; /*根據ID查詢文檔*/ @Test public void findById(){ Optional<Article> optional=service.findById(2); Article article = optional.get(); System.out.println(article); } }
5、效果展示
十、查詢全部文檔
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import java.util.Optional; public interface ArticleService { //查詢全部文檔 public Iterable<Article> findAll(); }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.util.Optional; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; @Override public Iterable<Article> findAll() { return dao.findAll(); } }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Optional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; /*查詢全部文檔*/ @Test public void findAllDocument(){ Iterable<Article> all = service.findAll(); all.forEach(item-> System.out.println(item)); } }
5、效果展示
十一、分頁查詢全部文檔
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import java.util.Optional; public interface ArticleService { //分頁查詢全部文檔
public Page<Article> findAll(Pageable pageable); }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.util.Optional; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; @Override public Page<Article> findAll(Pageable pageable) { return dao.findAll(pageable); } }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.Optional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; /*分頁查詢全部文檔*/ @Test public void findAllDocumentByPage(){ Page<Article> all = service.findAll(PageRequest.of(0, 5)); for (Article list:all.getContent()) { System.out.println(list); } } }
5、效果展示
十二、自定義規則查詢--Title域
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { //自定義規則 //根據Title域中的關鍵詞查找 public List<Article> findByTitle(String title); }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import java.util.List; import java.util.Optional; public interface ArticleService { //自定義規則 //根據Title域中的關鍵詞查找 public List<Article> findByTitle(String title); }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; @Override public List<Article> findByTitle(String title) { return dao.findByTitle(title); } }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; import java.util.Optional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; //自定義規則 /*根據Title域中的關鍵詞查找*/ @Test public void findByTitle(){ List<Article> lists= service.findByTitle("修改");
lists.stream().forEach(item-> System.out.println(item));
} }
5、效果展示
十三、分頁自定義規則查詢--Title域
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { //自定義規則 //分頁根據Title域中的關鍵詞查找 public List<Article> findByTitle(String title, Pageable pageable); }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import java.util.List; import java.util.Optional; public interface ArticleService { //自定義規則 //分頁根據Title域中的關鍵詞查找 public List<Article> findByTitle(String title, Pageable pageable); }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; @Override public List<Article> findByTitle(String title, Pageable pageable) { return dao.findByTitle(title,pageable); } }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; import java.util.Optional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; //自定義規則 /*分頁根據Title域中的關鍵詞查找*/ @Test public void findByTitleAndPage(){ List<Article> title = service.findByTitle("測試", PageRequest.of(0, 5)); title.stream().forEach(item-> System.out.println(item)); } }
5、效果展示
十四、根據一段內容查詢--NativeSearchQuery(Elasticsearch原生查詢)
1、ArticleDao
package com.zn.dao; import com.zn.entity.Article; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { }
2、ArticleService
package com.zn.service; import com.zn.entity.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import java.util.List; import java.util.Optional; public interface ArticleService { }
3、ArticleServiceImpl
package com.zn.service.impl; import com.zn.dao.ArticleDao; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; import java.util.List; import java.util.Optional; @Service public class ArticleServiceImpl implements ArticleService { @Autowired private ArticleDao dao; }
4、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.index.query.QueryBuilders; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; import java.util.Optional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; /*根據一段內容查詢--NativeSearchQuery*/ @Test public void findByNativeSearchQuery(){ //創建NativeSearchQueryBuilder對象 NativeSearchQueryBuilder nativeSearchQueryBuilder=new NativeSearchQueryBuilder(); //指定查詢規則 (先分詞再搜索) NativeSearchQuery build = nativeSearchQueryBuilder.withQuery(QueryBuilders.queryStringQuery("搜索測試").defaultField("title")).build(); //使用ElasticsearchTemplate執行查詢
List<Article> articles = template.queryForList(build, Article.class); articles.stream().forEach(item-> System.out.println(item)); } }
5、效果展示
十五、分頁根據一段內容查詢--NativeSearchQuery(Elasticsearch原生查詢)
1、SpringDataESTest
package com.zn; import com.zn.entity.Article; import com.zn.service.ArticleService; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.index.query.QueryBuilders; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; import java.util.Optional; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired private ArticleService service; @Autowired private ElasticsearchTemplate template; /*分頁根據一段內容查詢--NativeSearchQuery*/ @Test public void findByNativeSearchQueryAndPage(){ //創建NativeSearchQueryBuilder對象 NativeSearchQueryBuilder nativeSearchQueryBuilder=new NativeSearchQueryBuilder(); //指定查詢規則 (先分詞再搜索) NativeSearchQuery build = nativeSearchQueryBuilder.withQuery(QueryBuilders.queryStringQuery("搜索測試").defaultField("title")) .withPageable(PageRequest.of(0,5)) .build(); //使用ElasticsearchTemplate執行查詢 List<Article> articles = template.queryForList(build, Article.class); articles.stream().forEach(item-> System.out.println(item)); } }
2、效果展示