1、什么是Spring Data
2、什么是Spring Data ElasticSearch
3.Spring Data ElasticSearch環境
1 環境搭建
步驟一:Spring-Data-ElasticSearch,Spring-test幫助你加載配置文件並且測試
ESTemplate模板,模板當中包含了一系列的方法
導入依賴:
<dependency> <groupId>org.springframework.data</groupId> <artifactId>spring-data-elasticsearch</artifactId> <version>3.1.9.RELEASE</version> <exclusions> <exclusion> <groupId>org.elasticsearch.plugin</groupId> <artifactId>transport‐netty4‐client</artifactId> </exclusion> </exclusions> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-test --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>5.1.5.RELEASE</version> <scope>test</scope> </dependency>
步驟二:創建Spring的核心配置文
<?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"> <!--開啟包掃描--> <context:component-scan base-package="com.wdksoft"/> <!--配置集群信息--> <elasticsearch:transport-client id="esClient" cluster-name="my-elasticsearch" cluster-nodes="127.0.0.1:9300,127.0.0.1:9301,127.0.0.1:9302"/> <!--注入ESTemplate模板--> <bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate"> <constructor-arg name="client" ref="esClient"/> </bean> <!--掃描Mapper--> <elasticsearch:repositories base-package="com.wdksoft.mapper"/> </beans>
2.SpringDataES案例
1 添加索引庫
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "classpath:applicationContext.xml") public class SpringDataESTest { //植入模板對象 @Resource private ElasticsearchTemplate elasticsearchTemplate; @Test public void createIndex(){ //創建空的索引庫 elasticsearchTemplate.createIndex(Hello.class); } }
2 添加索引庫並且指定Mapping信息
利用POJO映射Mapping信
@Document(indexName = "my-index",type = "hello") public class Hello { @Id @Field(type = FieldType.Long,index = false,store = true) private Long 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; }
@Test public void createIndex(){ //創建空的索引庫 elasticsearchTemplate.+(Hello.class); elasticsearchTemplate.putMapping(Hello.class); }
3 添加文檔數據
創建Mapper層接口:
/** * 數據訪問層接口 */ @Repository public interface HelloMapper extends ElasticsearchRepository<Hello,Long> { }
創建Service層接口
public interface HelloService { public void save(Hello hello); }
創建Service層接口實現類
@Service("helloService") public class HelloServiceImpl implements HelloService { //植入Mapper層對象 @Resource private HelloMapper helloMapper; @Override public void save(Hello hello) { helloMapper.save(hello); } }
測試:
/** * 添加文檔數據 */ @Test public void createDocument(){ for(long i=1l;i<=10l;i++){ Hello hello=new Hello(); hello.setId(i); hello.setTitle("新增的Title數據"+i); hello.setContent("新增的Content數據"+i); helloService.save(hello); }
4 刪除文檔數據
Service層接口
//根據文檔ID刪除 public void deleteById(long id); //刪除全部 public void deleteAll();
Service層接口實現類
@Override public void deleteById(long id) { helloMapper.deleteById(id); } @Override public void deleteAll() { helloMapper.deleteAll(); }
測試:
/** * 刪除文檔數據 */ @Test public void deleteDocument(){ //根據文檔ID刪除 helloService.deleteById(1l); //全部刪除 helloService.deleteAll(); }
5 修改文檔數據
修改也是調用save方法,如果id不存在則添加,id存在則先刪除再添加
/** * 修改文檔數據:先刪除再添加,調用的還是save方法 */ @Test public void updateDocument(){ Hello hello=new Hello(); hello.setId(1l); hello.setTitle("[修改]新增的Title數據"); hello.setContent("[修改]新增的Content數據"); helloService.save(hello); }
6 根據ID查詢
Service層接口
//根據文檔ID查詢數據 public Optional<Hello> findById(Long id);
Service層接口實現類
@Override public Optional<Hello> findById(Long id) { return helloMapper.findById(id); }
測試:
/** * 根據文檔ID查詢 */ @Test public void getDocumentById(){ Optional<Hello> optional = helloService.findById(2l); Hello hello = optional.get(); System.out.println(hello);
7 查詢全部文檔數據
Service層接口
//查詢所有數據 public Iterable<Hello> findAll(); //查詢所有數據包含分頁 public Page<Hello> findAll(Pageable pageable);
Service層接口實現類
@Override public Iterable<Hello> findAll() { return helloMapper.findAll(); } @Override public Page<Hello> findAll(Pageable pageable) { return helloMapper.findAll(pageable); }
測試:
/** * 查詢所有文檔數據 */ @Test public void getAllDocument(){ Iterable<Hello> iterable = helloService.findAll(); iterable.forEach(item-> System.out.println(item)); } /** * 查詢所有文檔數據加分頁 */ @Test public void getDocumentPage(){ //指定分頁規則 Page<Hello> page = helloService.findAll(PageRequest.of(0, 5)); for (Hello hello:page.getContent()) { System.out.println(hello); } }
8 根據查詢方法的自定義規則進行數據查詢
根據Title域進行查詢,並且加分頁
Mapper層接口:
/** * 數據訪問層接口 */ @Repository public interface HelloMapper extends ElasticsearchRepository<Hello,Long> { //根據Title域中的關鍵詞查找數據 public List<Hello> findByTitle(String str); //根據Title域中的關鍵詞查找數據 public List<Hello> findByTitle(String str, Pageable pageable); }
Service層接口
//根據Title域中的關鍵詞查找數據 public List<Hello> findByTitle(String str); //根據Title域中的關鍵詞查找數據 public List<Hello> findByTitle(String str, Pageable pageable);
Service層接口實現類
@Override public List<Hello> findByTitle(String str) { return helloMapper.findByTitle(str); } @Override public List<Hello> findByTitle(String str, Pageable pageable) { return helloMapper.findByTitle(str,pageable); }
測試:
/** * 根據條件查詢文檔數據加分頁 */ @Test public void getDocumentByTitle(){ /*List<Hello> helloLists = helloService.findByTitle("修改"); helloLists.stream().forEach(item-> System.out.println(item));*/ List<Hello> helloLists = helloService.findByTitle("新增", PageRequest.of(0, 5)); helloLists.stream().forEach(item-> System.out.println(item)); }
9 NativeSearchQuery
將輸入的查詢條件先分詞,再進行查詢
/** * 根據一段內容查詢數據:NativeSearchQuery * 先分詞再查詢 */ @Test public void getDocumentQuery(){ //創建NativeSearchQueryBuilder對象 NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder(); //指定查詢規則 NativeSearchQuery build = nativeSearchQueryBuilder.withQuery(QueryBuilders.queryStringQuery("搜索新增").defaultField("title")) .withPageable(PageRequest.of(0,5)).build(); //使用ESTemplates執行查詢 List<Hello> hellos = elasticsearchTemplate.queryForList(build, Hello.class); hellos.stream().forEach(item-> System.out.println(item)); }