Spring Data ElasticSearch的使用
1、什么是Spring Data
2、什么是Spring Data ElasticSearch
3.Spring Data ElasticSearch環(huán)境
1.1 環(huán)境搭建
步驟一: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>
步驟二:創(chuàng)建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案例
2.1 添加索引庫
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:applicationContext.xml")
public class SpringDataESTest {
//植入模板對象
@Resource
private ElasticsearchTemplate elasticsearchTemplate;
@Test
public void createIndex(){
//創(chuàng)建空的索引庫
elasticsearchTemplate.createIndex(Hello.class);
}
}
2.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(){
//創(chuàng)建空的索引庫
elasticsearchTemplate.+(Hello.class);
elasticsearchTemplate.putMapping(Hello.class);
}
2.3 添加文檔數(shù)據(jù)
創(chuàng)建Mapper層接口:
/**
* 數(shù)據(jù)訪問層接口
*/
@Repository
public interface HelloMapper extends ElasticsearchRepository<Hello,Long> {
}
創(chuàng)建Service層接口
public interface HelloService {
public void save(Hello hello);
}
創(chuàng)建Service層接口實現(xiàn)類
@Service("helloService")
public class HelloServiceImpl implements HelloService {
//植入Mapper層對象
@Resource
private HelloMapper helloMapper;
@Override
public void save(Hello hello) {
helloMapper.save(hello);
}
}
測試:
/**
* 添加文檔數(shù)據(jù)
*/
@Test
public void createDocument(){
for(long i=1l;i<=10l;i++){
Hello hello=new Hello();
hello.setId(i);
hello.setTitle("新增的Title數(shù)據(jù)"+i);
hello.setContent("新增的Content數(shù)據(jù)"+i);
helloService.save(hello);
}
2.4 刪除文檔數(shù)據(jù)
Service層接口
//根據(jù)文檔ID刪除
public void deleteById(long id);
//刪除全部
public void deleteAll();
Service層接口實現(xiàn)類
@Override
public void deleteById(long id) {
helloMapper.deleteById(id);
}
@Override
public void deleteAll() {
helloMapper.deleteAll();
}
測試:
/**
* 刪除文檔數(shù)據(jù)
*/
@Test
public void deleteDocument(){
//根據(jù)文檔ID刪除
helloService.deleteById(1l);
//全部刪除
helloService.deleteAll();
}
2.5 修改文檔數(shù)據(jù)
修改也是調(diào)用save方法,如果id不存在則添加,id存在則先刪除再添加
/**
* 修改文檔數(shù)據(jù):先刪除再添加,調(diào)用的還是save方法
*/
@Test
public void updateDocument(){
Hello hello=new Hello();
hello.setId(1l);
hello.setTitle("[修改]新增的Title數(shù)據(jù)");
hello.setContent("[修改]新增的Content數(shù)據(jù)");
helloService.save(hello);
}
2.6 根據(jù)ID查詢
Service層接口
//根據(jù)文檔ID查詢數(shù)據(jù)
public Optional<Hello> findById(Long id);
Service層接口實現(xiàn)類
@Override
public Optional<Hello> findById(Long id) {
return helloMapper.findById(id);
}
測試:
/**
* 根據(jù)文檔ID查詢
*/
@Test
public void getDocumentById(){
Optional<Hello> optional = helloService.findById(2l);
Hello hello = optional.get();
System.out.println(hello);
2.7 查詢?nèi)课臋n數(shù)據(jù)
Service層接口
//查詢所有數(shù)據(jù)
public Iterable<Hello> findAll();
//查詢所有數(shù)據(jù)包含分頁
public Page<Hello> findAll(Pageable pageable);
Service層接口實現(xiàn)類
@Override
public Iterable<Hello> findAll() {
return helloMapper.findAll();
}
@Override
public Page<Hello> findAll(Pageable pageable) {
return helloMapper.findAll(pageable);
}
測試:
/**
* 查詢所有文檔數(shù)據(jù)
*/
@Test
public void getAllDocument(){
Iterable<Hello> iterable = helloService.findAll();
iterable.forEach(item-> System.out.println(item));
}
/**
* 查詢所有文檔數(shù)據(jù)加分頁
*/
@Test
public void getDocumentPage(){
//指定分頁規(guī)則
Page<Hello> page = helloService.findAll(PageRequest.of(0, 5));
for (Hello hello:page.getContent()) {
System.out.println(hello);
}
}
2.8 根據(jù)查詢方法的自定義規(guī)則進行數(shù)據(jù)查詢
根據(jù)Title域進行查詢,并且加分頁
Mapper層接口:
/**
* 數(shù)據(jù)訪問層接口
*/
@Repository
public interface HelloMapper extends ElasticsearchRepository<Hello,Long> {
//根據(jù)Title域中的關(guān)鍵詞查找數(shù)據(jù)
public List<Hello> findByTitle(String str);
//根據(jù)Title域中的關(guān)鍵詞查找數(shù)據(jù)
public List<Hello> findByTitle(String str, Pageable pageable);
}
Service層接口
//根據(jù)Title域中的關(guān)鍵詞查找數(shù)據(jù)
public List<Hello> findByTitle(String str);
//根據(jù)Title域中的關(guān)鍵詞查找數(shù)據(jù)
public List<Hello> findByTitle(String str, Pageable pageable);
Service層接口實現(xiàn)類
@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);
}
測試:
/**
* 根據(jù)條件查詢文檔數(shù)據(jù)加分頁
*/
@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));
}
2.9 NativeSearchQuery
將輸入的查詢條件先分詞,再進行查詢
/**
* 根據(jù)一段內(nèi)容查詢數(shù)據(jù):NativeSearchQuery
* 先分詞再查詢
*/
@Test
public void getDocumentQuery(){
//創(chuàng)建NativeSearchQueryBuilder對象
NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();
//指定查詢規(guī)則
NativeSearchQuery build = nativeSearchQueryBuilder.withQuery(QueryBuilders.queryStringQuery("搜索新增").defaultField("title"))
.withPageable(PageRequest.of(0,5)).build();
//使用ESTemplates執(zhí)行查詢
List<Hello> hellos = elasticsearchTemplate.queryForList(build, Hello.class);
hellos.stream().forEach(item-> System.out.println(item));
}

浙公網(wǎng)安備 33010602011771號