更多有關博主寫的往期Elasticsearch文章
標題 | 地址 |
---|---|
【ElasticSearch 集群】Linux安裝ElasticSearch集群(圖文解說詳細版) | https://masiyi.blog.csdn.net/article/details/131109454 |
基于SpringBoot+ElasticSearch 的Java底層框架的實現(xiàn) | https://masiyi.blog.csdn.net/article/details/121534307 |
ElasticSearch對標Mysql,誰能拔得頭籌? | https://masiyi.blog.csdn.net/article/details/122661822 |
同事說關鍵字查詢用Mysql,我上去就是一個高壓鍋,用ElasticSearch不香嗎? | https://masiyi.blog.csdn.net/article/details/122701654 |
新年第一天,老板讓升級ElasticSearch版本,我說得加錢 | https://masiyi.blog.csdn.net/article/details/122819455 |
使用了ElasticSearch之后,公司系統(tǒng)查詢速度快了50倍,工資直接翻一倍 | https://masiyi.blog.csdn.net/article/details/122819455 |
ElasticSearch實戰(zhàn)教程PostMan版(超級詳細版) | https://masiyi.blog.csdn.net/article/details/123048119 |
Linux安裝ElasticSearch以及Ik分詞器(圖文解說詳細版) | https://masiyi.blog.csdn.net/article/details/121509681 |
導語
發(fā)現(xiàn)很多公司都是自己導入原生的jar包自己去封裝一套Elasticsearch的框架,這樣雖然可以自定義業(yè)務,但是需要花費很多的時間,其實spring官方有一套springboot的starter,幫助大家快速入手官網(wǎng)的starter,這篇博客也會介紹使用該starter連接Elasticsearch的集群。該starter有點像mybatisplus+Jpa,如果熟悉這兩個框架的同學應該很快就會上手。
官網(wǎng)地址:
https://docs.spring.io/spring-data/elasticsearch/docs/4.0.x/reference/html/#preface
本文所有的代碼都已經(jīng)提交到git倉庫中,倉庫地址:
https://gitee.com/WangFuGui-Ma/spring-boot-elasticSearch
現(xiàn)在我們開始按照步驟進行spring-boot-starter-data-elasticsearch的使用,本文中使用的spring boot版本為2.7.x
對于的elasticsearch客戶端版本為7.17.x
??第一步,創(chuàng)建一個springboot項目
??第二步,導入spring-boot-starter-data-elasticsearch依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>
其他依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--json-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<!--json-->
??第三步,配置yml文件
spring:
elasticsearch:
uris:
- 192.168.75.128:9200
- 192.168.75.129:9200
- 192.168.75.130:9200
server:
port: 8889
uris這里填的是集群的地址,如果你的es是單機版的,直接填一個就行了
??第四步,編寫es索引對應的實體類
import lombok.Data;
import org.springframework.data.elasticsearch.annotations.Document;
import org.springframework.data.elasticsearch.annotations.WriteTypeHint;
/**
* @Author masiyi
* @Date 2023/6/14 13:46
* @PackageName:com.masiyi.springbootstarterdataelasticsearch.doman
* @ClassName: ElasticTest
* @Description: TODO
* @Version 1.0
*/
@Data
@Document(indexName = "elastic_test",writeTypeHint = WriteTypeHint.FALSE)
public class ElasticTest {
private Long id;
private String name;
private Integer age;
private Boolean isMan;
}
注:
- id這個類型一定要寫,否則會報錯
- indexName對應es中的索引
- writeTypeHint如果為false則不會自動創(chuàng)建索引
??第五步,編寫實體類對應的mapper
類似mybatis一樣,我們需要創(chuàng)建對應的mapper
public interface ElasticTestMapper extends ElasticsearchRepository<ElasticTest,Long> {
}
繼承ElasticsearchRepository
類,第一個泛型添es對應的實體類,第二個泛型添id的類型
??第六步,使用框架自帶的增刪改查
我們這次用單元測試的方法跟大家演示框架的用法
注入剛剛創(chuàng)建的mapper
和框架的ElasticsearchRestTemplate
??增
??使用ElasticsearchRestTemplate
@Test
void save() {
ElasticTest elasticTest = new ElasticTest();
elasticTest.setName("李四");
elasticTest.setAge(23);
elasticTest.setIsMan(true);
elasticsearchTemplate.save(elasticTest);
}
??使用mapper
@Test
void insert() {
ElasticTest elasticTest = new ElasticTest();
elasticTest.setName("李四");
elasticTest.setAge(23);
elasticTest.setIsMan(true);
elasticTestMapper.save(elasticTest);
}
??設置指定的id
@Test
void insertId() {
ElasticTest elasticTest = new ElasticTest();
elasticTest.setName("掉頭發(fā)的王富貴");
elasticTest.setAge(25);
elasticTest.setIsMan(true);
elasticTest.setId(2434235L);
elasticTestMapper.save(elasticTest);
}
??批量保存
@Test
void saveAll() {
ElasticTest elasticTest = new ElasticTest();
elasticTest.setName("李四");
elasticTest.setAge(24);
elasticTest.setIsMan(true);
elasticTestMapper.saveAll(Arrays.asList(elasticTest));
}
??刪
??傳入實體類刪
@Test
void delete() {
ElasticTest elasticTest = new ElasticTest();
elasticTest.setId(2342342L);
elasticTestMapper.delete(elasticTest);
}
??傳入id刪
@Test
void deleteById() {
elasticTestMapper.deleteById(2342342L);
}
??刪除索引里面所有的數(shù)據(jù)(慎用)
@Test
void deleteAll() {
elasticTestMapper.deleteAll();
}
??改
??實體改
@Test
void update() {
ElasticTest elasticTest = new ElasticTest();
elasticTest.setName("掉頭發(fā)的王富貴hh");
elasticTest.setId(2434235L);
elasticTestMapper.save(elasticTest);
}
這里改會把其他的不在實體里面為null的數(shù)據(jù)清空
??實體改全部
@Test
void updateAll() {
ElasticTest elasticTest = new ElasticTest();
elasticTest.setName("掉頭發(fā)的王富貴");
elasticTest.setAge(24);
elasticTest.setIsMan(true);
elasticTest.setId(2434234L);
elasticTestMapper.save(elasticTest);
}
如果我們只需要局部更新,可以使用下面的兩種方法
??先查再update
@Test
void updateNow() {
ElasticTest elasticTest = elasticTestMapper.findById(2434234L).get();
elasticTest.setName("不掉頭發(fā)的王富貴");
elasticTestMapper.save(elasticTest);
}
但是這個方法會消耗性能,所以推薦用下面的方法
??直接使用局部更新
@Test
void updateNow2() {
ElasticTest elasticTest = new ElasticTest();
elasticTest.setName("掉頭發(fā)的王富貴h");
Map map = JSONObject.parseObject(JSONObject.toJSONString(elasticTest), Map.class);
UpdateQuery updateQuery = UpdateQuery.builder("0ZUv7okBcQy9f7u_tXkH").withDocument(Document.from(map)).build();
elasticsearchTemplate.update(updateQuery, IndexCoordinates.of("elastic_test"));
}
??查
??根據(jù)id查
@Test
void select() {
Optional<ElasticTest> byId = elasticTestMapper.findById(2434234L);
byId.ifPresent(System.out::println);
}
??根據(jù)id列表查
@Test
void findAllById() {
Iterable<ElasticTest> allById = elasticTestMapper.findAllById(Arrays.asList(2434234L));
allById.forEach(System.out::println);
}
??查詢?nèi)繑?shù)據(jù)
@Test
void findAll() {
Iterable<ElasticTest> allById = elasticTestMapper.findAll();
allById.forEach(System.out::println);
}
??排序查-正序
@Test
void findAllSort() {
Sort age = Sort.by("age").ascending();
Iterable<ElasticTest> all = elasticTestMapper.findAll(age);
all.forEach(System.out::println);
}
??排序查-倒序
@Test
void findAllSortDE() {
Sort age = Sort.by("age").descending();
Iterable<ElasticTest> all = elasticTestMapper.findAll(age);
all.forEach(System.out::println);
}
??分頁查
@Test
void findAllPage() {
PageRequest pageRequest = PageRequest.of(0, 10);
Page<ElasticTest> all = elasticTestMapper.findAll(pageRequest);
all.forEach(System.out::println);
System.out.println(JSON.toJSONString(all));
}
??自定義復雜的查詢
@Test
void findMyStyle() {
TermQueryBuilder termQueryBuilder = new TermQueryBuilder("name.keyword", "掉頭發(fā)的王富貴");
NativeSearchQuery nativeSearchQuery = new NativeSearchQuery(termQueryBuilder);
SearchHits<ElasticTest> search = elasticsearchTemplate.search(nativeSearchQuery, ElasticTest.class);
List<SearchHit<ElasticTest>> hitList = search.getSearchHits();
for (SearchHit<ElasticTest> hit : hitList) {
ElasticTest entity = hit.getContent(); // 獲取實體對象
System.out.println(entity);
String index = hit.getIndex(); // 獲取索引名
System.out.println(index);
}
}
??第七步,使用JPA風格的查詢方式
如果會用jpa的同學看到這個可能會非常得熟悉
可以自定義查詢方法find...
??根據(jù)age查詢
ElasticTest findByAge(Integer age);
??查詢所有符合age的數(shù)據(jù)
List<ElasticTest> findAllByAge(Integer age);
??查詢最top的數(shù)據(jù)
ElasticTest findTopByAge(Integer age);
如果你使用的是idea這種高級的編輯器,你在mapper寫方法的時候會自動提示你。
通過本文的學習,我們探索了在Spring Boot應用中使用Elasticsearch的方法以及如何連接到Elasticsearch集群。Elasticsearch作為一款強大的搜索和分析引擎,在現(xiàn)代應用開發(fā)中扮演著至關重要的角色。借助于Spring Boot和spring-boot-starter-data-elasticsearch,我們能夠以更加便捷的方式將Elasticsearch集成到我們的項目中,實現(xiàn)高效的數(shù)據(jù)搜索與分析。
通過配置簡單明了的屬性,我們能夠快速地將Spring Boot應用連接到Elasticsearch集群,實現(xiàn)數(shù)據(jù)的索引、搜索和分析。借助于Spring Data Elasticsearch提供的強大功能,我們能夠輕松地定義實體類、進行CRUD操作,并且利用Elasticsearch的全文搜索和分詞等特性,讓我們的應用具備更高效的查詢和檢索能力。文章來源:http://www.zghlxwxcb.cn/news/detail-665615.html
文章來源地址http://www.zghlxwxcb.cn/news/detail-665615.html
到了這里,關于【Elasticsearch】spring-boot-starter-data-elasticsearch的使用以及Elasticsearch集群的連接的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!