1.SpringDataES環境搭建
Spring Data ElasticSearch 基於 spring data API 簡化 elasticSearch操作,將原始操作elasticSearch的客戶端API
進行封裝 。Spring Data為Elasticsearch項目提供集成搜索引擎。Spring Data Elasticsearch POJO的關鍵功能區域
為中心的模型與Elastichsearch交互文檔和輕松地編寫一個存儲庫數據訪問層。
1.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的核心配置文件
<!--開啟包掃描--> <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>
2.SpringDataES案例
2.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.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); }
2.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(){ Hello hello=new Hello(); hello.setId(1l); hello.setTitle("新增的Title數據"); hello.setContent("新增的Content數據"); helloService.save(hello); }
2.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(); }
2.5 修改文檔數據
/** * 修改文檔數據:先刪除再修改,調用的還是save方法 */ @Test public void updateDocument(){ Hello hello=new Hello(); hello.setId(1l); hello.setTitle("[修改]新增的Title數據"); hello.setContent("[修改]新增的Content數據"); helloService.save(hello); }
2.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); }
2.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); } }
2.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)); }
2.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)); }