Spring Data是一個用於簡化數據庫訪問,並支持雲服務的開源框架。其主要目標是使得對數據的訪問變得方便快捷,並支持map-reduce框架和雲計算數據服務。 Spring Data可以極大的簡化JPA的寫法,可以在幾乎不用寫實現的情況下,實現對數據的訪問和操作。除了CRUD外,還包括如分頁、排序等一些常用的功能。
筆者的ElasticSearch集群的版本是6.2.4,導入spring-data-elasticsearch的maven依賴也是3.1.5.RELEASE,不同版本的api可能會有差異
一:准備maven依賴
<!--elasticsearch核心依賴--> <dependency> <groupId>org.elasticsearch</groupId> <artifactId>elasticsearch</artifactId> <version>6.2.4</version> </dependency> <!--elasticsearch客戶端--> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>transport</artifactId> <version>6.2.4</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.21</version> </dependency> <dependency> <groupId>log4j</groupId> <artifactId>log4j</artifactId> <version>1.2.12</version> </dependency> <!--單元測試--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> <scope>compile</scope> </dependency> <!--實體類簡化工具--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.16.18</version> </dependency> <!--springframework--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.2.3.RELEASE</version> </dependency> <!--spring-data-elasticsearch--> <dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.1.5.RELEASE</version> <exclusions> <exclusion> <groupId>org.elasticsearch.plugin</groupId> <artifactId>transport‐netty4‐client</artifactId> </exclusion> </exclusions> </dependency>
二:創建實體
package com.yjc.entity; 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; //lombok相當於get,set,toSting等一系列方法 @Data //代表文檔對象(索引庫,類型) @Document(indexName = "myindex2",type = "article") public class Article { //文檔主鍵,唯一 @Id //store是否存儲(默認false),index是否分詞(默認true),type類型 @Field(store = true,index = false,type = FieldType.Integer) private Integer id; //下面配置代表 進行存儲並以ik_smart方式分詞,(默認開啟分詞)保持的類型為text,進行查詢的時候按照ik_smart方式進行分詞 @Field(store = true,analyzer ="ik_smart",searchAnalyzer = "ik_smart",type = FieldType.Text) private String title; //下面配置代表 進行存儲並以ik_smart方式分詞,保持的類型為text,進行查詢的時候按照ik_smart方式進行分詞 @Field(store = true,analyzer ="ik_smart",searchAnalyzer = "ik_smart",type = FieldType.Text) private String content; }
三:創建DAO和Service
ArticleDao
package com.yjc.dao; import com.yjc.entity.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.data.elasticsearch.repository.ElasticsearchRepository; import org.springframework.stereotype.Repository; import java.util.List; @Repository /** * ElasticsearchRepository<要操作的實體類,ID在實體類中類型> * */ public interface ArticleDao extends ElasticsearchRepository<Article,Integer> { //根據標題查詢 List<Article> findByTitle(String condition); //根據標題查詢(含分頁) Page<Article> findByTitle(String condition, Pageable pageable); //根據id List<Article> findById(String condition); }
ArticleService
package com.yjc.service; import com.yjc.entity.Article; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import java.util.List; public interface ArticleService { //添加 void save(Article article); //刪除 void delete(Article article); //查詢全部 Iterable<Article> findAll(); //分頁查詢 Page<Article> findAll(Pageable pageable); //根據標題查詢 List<Article> findByTitle(String condition); //根據標題查詢(含分頁) Page<Article> findByTitle(String condition, Pageable pageable); //根據內容搜索並分頁 List<Article> findById(String condition); }
ArticleServiceImpl
package com.yjc.service.impl; import com.yjc.dao.ArticleDao; import com.yjc.entity.Article; import com.yjc.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; @Service public class ArticleServiceImpl implements ArticleService { @Autowired ArticleDao articleDao; @Override public void save(Article article) { articleDao.save(article); } @Override public void delete(Article article) { articleDao.delete(article); } @Override public Iterable<Article> findAll() { return articleDao.findAll(); } @Override public Page<Article> findAll(Pageable pageable) { return articleDao.findAll(pageable); } @Override public List<Article> findByTitle(String condition) { return articleDao.findByTitle(condition); } @Override public Page<Article> findByTitle(String condition, Pageable pageable) { return articleDao.findByTitle(condition,pageable); } @Override public List<Article> findById(String condition) { return articleDao.findById(condition); } }
四:創建Spring的核心配置文件
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:context="http://www.springframework.org/schema/context"
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/data/elasticsearch
http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd "> <!--創建包掃描器,掃描Dao,自動創建實例--> <elasticsearch:repositories base-package="com.yjc.dao"/> <!--掃描Service--> <context:component-scan base-package="com.yjc.service"/> <!--配置es的連接--> <elasticsearch:transport-client id="client" cluster-nodes="192.168.118.3:9300" cluster-name="elasticsearch"/> <!--es模板對象--> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="client"/> </bean> </beans>
五:測試
package com.yjc.test; import com.yjc.entity.Article; import com.yjc.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.domain.Pageable; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import java.util.List; @RunWith(SpringJUnit4ClassRunner.class) //啟動單元測試的時候加載的配置文件 @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { @Autowired ArticleService articleService; //注入連接對象 @Autowired TransportClient client; //注入模板對象 @Autowired ElasticsearchTemplate elasticsearchTemplate; @Test //創建索引 public void createIndex() { //配置在實體類中已經配置好了,直接創建並添加映射 elasticsearchTemplate.createIndex(Article.class); elasticsearchTemplate.putMapping(Article.class); } @Test //保存文檔 public void saveArticle() { Article article = new Article(); article.setId(101); article.setTitle("測試SpringData ElasticSearch"); article.setContent("Spring Data ElasticSearch 基於 spring data API 簡化 elasticSearch操 作,將原始操作elasticSearch的客戶端API 進行封裝 \n" + " Spring Data為Elasticsearch Elasticsearch項目提供集成搜索引擎"); articleService.save(article); } @Test //修改文檔 public void update() { Article article = new Article(); article.setId(100); article.setTitle("修改SpringData ElasticSearch"); article.setContent("修改Spring Data ElasticSearch 基於 spring data API 簡化 elasticSearch操 作,將原始操作elasticSearch的客戶端API 進行封裝 \n" + " Spring Data為Elasticsearch Elasticsearch項目提供集成搜索引擎"); articleService.save(article); } @Test //刪除文檔 public void delete(){ Article article = new Article(); article.setId(100); articleService.delete(article); } @Test //分頁查詢 public void findAllPage(){ Pageable pageable=PageRequest.of(0,10); Page<Article> all = articleService.findAll(pageable); for (Article article:all.getContent()){ System.out.println(article); } } @Test //帶條件查詢 public void findByTitle(){ List<Article> articles = articleService.findByTitle("測試"); for (Article article:articles){ System.out.println(article); } } @Test //待條件查詢加分頁 public void findByTitlePage(){ Pageable pageable=PageRequest.of(0,1); Page<Article> articles = articleService.findByTitle("引擎", pageable); for (Article article:articles.getContent()){ System.out.println(article); } } @Test //根據id進行查詢 public void findByContent(){ List<Article> articles = articleService.findById("100"); for (Article article:articles){ System.out.println(article); } } }
Spring Data ElasticSearch中帶條件查詢是根據Dao層的方法名來判斷去哪個域中去搜索數據,常用的命名規范如下
使用elasticsearchTemplate對象將條件進行分詞,然后搜索
NativeSearchQueryBuilder builder=new NativeSearchQueryBuilder(); NativeSearchQuery build = builder.withQuery(QueryBuilders.queryStringQuery("人民更改").defaultField("title")).build(); List<Article> articles = elasticsearchTemplate.queryForList(build, Article.class); for (Article article:articles){ System.out.println(article); }