SpringBoot整合Spring Data Elasticsearch的過(guò)程詳解
Spring Data Elasticsearch提供了ElasticsearchTemplate工具類(lèi),實(shí)現(xiàn)了POJO與elasticsearch文檔之間的映射
elasticsearch本質(zhì)也是存儲(chǔ)數(shù)據(jù),它不支持事物,但是它的速度遠(yuǎn)比數(shù)據(jù)庫(kù)快得多,
可以這樣來(lái)對(duì)比elasticsearch和數(shù)據(jù)庫(kù)
- 索引(indices)--------數(shù)據(jù)庫(kù)(databases)
- 類(lèi)型(type)------------數(shù)據(jù)表(table)
- 文檔(Document)---------------- 行(row)
- 字段(Field)-------------------列(Columns )
整合:
1,在SprinBoot工程中引入jar包
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-elasticsearch</artifactId> </dependency>
2,配置文件
spring.data.elasticsearch.cluster-name=elasticsearch //名字必須和elasticsearch.yml里面的cluster.name相同 spring.data.elasticsearch.cluster-nodes=127.0.0.1:9300 spring.data.elasticsearch.repositories.enabled=true
3,創(chuàng)建實(shí)體,并對(duì)類(lèi)和屬性進(jìn)行標(biāo)注
@Document(indexName = "item",type = "docs", shards = 1, replicas = 0)//標(biāo)記為文檔類(lèi)型,ndexName:對(duì)應(yīng)索引庫(kù)名稱type:對(duì)應(yīng)在索引庫(kù)中的類(lèi)型,shards:分片數(shù)量,默認(rèn)5,replicas:副本數(shù)量,默認(rèn)1 public class Item { @Id //主鍵 private Long id; @Field(type = FieldType.Text, analyzer = "ik_max_word") //標(biāo)記為成員變量 FieldType,可以是text、long、short、date、integer等 text:存儲(chǔ)數(shù)據(jù)時(shí)候,會(huì)自動(dòng)分詞,并生成索引 keyword:存儲(chǔ)數(shù)據(jù)時(shí)候,不會(huì)分詞建立索引 analyzer:分詞器名稱 private String title; //標(biāo)題 @Field(type = FieldType.Keyword) private String category;// 分類(lèi) @Field(type = FieldType.Keyword) private String brand; // 品牌 @Field(type = FieldType.Double) private Double price; // 價(jià)格 @Field(index = false, type = FieldType.Keyword)//index:是否索引 private String images; // 圖片地址
4.引入模板ElasticsearchTemplate
@Autowired private ElasticsearchTemplate elasticsearchTemplate;
5.創(chuàng)建一個(gè)索引
//添加索引 @Test public void addIndex() { elasticsearchTemplate.createIndex(Item.class); }
6.刪除索引
//刪除索引 @Test public void delete(){ elasticsearchTemplate.deleteIndex("item"); }
7.新增對(duì)象
繼承Repository提供的一些子接口,就能具備各種基本的CRUD功能,這里繼承ElasticsearchCrudRepository
首先定義一個(gè)對(duì)象的接口
public interface ItemRepository extends ElasticsearchCrudRepository<Item,Long> { }
然后注入ItemRepository
@Autowired private ItemRepository itemRepository;
新增對(duì)象
//新增一個(gè)對(duì)象 @Test public void insert(){ Item item = new Item(2L,"堅(jiān)果R1","手機(jī)","錘子",2500.00,"http://image.baidu.com/13123.jpg"); //Order order = new Order(20180020,"菜單"); itemRepository.save(item); }
批量新增
//批量新增 @Test public void insertList(){ List<Item> list = new LinkedList<>(); list.add(new Item(9L,"華為p20","手機(jī)","華為",3500.00,"http://image.baidu.com/13123.jpg")); list.add(new Item(10L,"華為p30","手機(jī)","華為",5450.00,"http://image.baidu.com/13123.jpg")); list.add(new Item(11L,"華為p30 pro","手機(jī)","華為",6980.00,"http://image.baidu.com/13123.jpg")); itemRepository.saveAll(list); }
8.查詢
//根據(jù)字段查詢所有 @Test public void queryAll(){ //升序,相應(yīng)降序?yàn)閐scending Iterable<Item> items = this.itemRepository.findAll(Sort.by("price").ascending()); for (Item item : items){ System.out.println(item); } }
9.自定義查詢方法
Spring Data 的另一個(gè)強(qiáng)大功能,是根據(jù)方法名稱自動(dòng)實(shí)現(xiàn)功能,你的方法名叫做:findByTitle,那么它就知道你是根據(jù)title查詢,然后自動(dòng)幫你完成,無(wú)需寫(xiě)實(shí)現(xiàn)類(lèi)。當(dāng)然,方法名稱要符合一定的約定:
上圖是截取csdn上博主我要取一個(gè)響亮的昵稱的圖
根據(jù)手機(jī)名查找手機(jī)
//自定義方法,根據(jù)Title查詢 @Test public void findByTitle(){ Item item = this.itemRepository.findByTitle("堅(jiān)果pro"); System.out.println(item); }
區(qū)間查詢
//根據(jù)區(qū)間查詢 @Test public void queryByPriceBetween(){ List<Item> list = this.itemRepository.findByPriceBetween(2000.00, 3500.00); for (Item item : list) { System.out.println("item = " + item); } }
模糊查詢
//模糊查詢 @Test public void queryLikeTitle(){ List<Item> list = this.itemRepository.findByTitleLike("R2"); for (Item item : list){ System.out.println(item); } }
使用自定義方法需要在接口里面申明方法
public interface ItemRepository extends ElasticsearchCrudRepository<Item,Long> { Item findByTitle(String title); List<Item> findByPriceBetween(double price1, double price2); List<Item> findByTitleLike(String title); }
10.自定義查詢
//自定義查詢,查詢數(shù)目等 @Test public void matchQuery(){ // 構(gòu)建查詢條件 NativeSearchQueryBuilder queryBuilder = new NativeSearchQueryBuilder(); // 添加基本分詞查詢 queryBuilder.withQuery(QueryBuilders.matchQuery("title","堅(jiān)果")); //獲取結(jié)果 Page<Item> items = (Page<Item>) this.itemRepository.findAll(); //條數(shù) long total = items.getTotalElements(); System.out.println("total = "+total); for (Item item : items){ System.out.println(item); } }關(guān)鍵的是NativeSearchQueryBuilder這個(gè)類(lèi)
分頁(yè)查詢
//分頁(yè)查詢 @Test public void queryByPage(){ NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder(); nativeSearchQueryBuilder.withQuery(QueryBuilders.termQuery("category","手機(jī)")); int page = 0; int size = 2; nativeSearchQueryBuilder.withPageable(PageRequest.of(page,size)); Page<Item> items = (Page<Item>) this.itemRepository.findAll(); long total = items.getTotalElements(); int totalPage = items.getTotalPages(); int nowPage = items.getNumber(); int pageSize = items.getSize(); System.out.println("總條數(shù) = "+total); System.out.println("總頁(yè)數(shù) = "+totalPage); System.out.println("當(dāng)前頁(yè) = "+nowPage); System.out.println("每頁(yè)大小 = "+pageSize); for (Item item : items){ System.out.println(item); } }
還有很多,就不意義列舉
在elasticsearch-head上查看數(shù)據(jù)
關(guān)于安裝elasticsearch-head,參考地址
Spring Data Elasticsearch文檔:https://docs.spring.io/spring-data/elasticsearch/docs/3.1.10.RELEASE/reference/html/
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解決SpringBoot整合ElasticSearch遇到的連接問(wèn)題
- SpringBoot整合Elasticsearch游標(biāo)查詢的示例代碼(scroll)
- es(elasticsearch)整合SpringCloud(SpringBoot)搭建教程詳解
- SpringBoot整合Elasticsearch7.2.0的實(shí)現(xiàn)方法
- SpringBoot整合Elasticsearch并實(shí)現(xiàn)CRUD操作
- 解決springboot無(wú)法注入JpaRepository的問(wèn)題
- SpringBoot3整合 Elasticsearch 8.x 使用Repository構(gòu)建增刪改查示例應(yīng)用
相關(guān)文章
Java數(shù)據(jù)結(jié)構(gòu)與算法入門(mén)實(shí)例詳解
這篇文章主要介紹了Java數(shù)據(jù)結(jié)構(gòu)與算法入門(mén)實(shí)例詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03深入dom4j使用selectSingleNode方法報(bào)錯(cuò)分析
本篇文章是對(duì)dom4j使用selectSingleNode方法報(bào)錯(cuò)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Spring Boot集成Druid數(shù)據(jù)庫(kù)連接池
這篇文章主要介紹了Spring Boot集成Druid數(shù)據(jù)庫(kù)連接池,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04MyBatis查詢數(shù)據(jù)庫(kù)語(yǔ)句總結(jié)
MyBatis是一種持久化框架,可以與許多不同類(lèi)型的關(guān)系型數(shù)據(jù)庫(kù)連接,下面這篇文章主要給大家介紹了關(guān)于MyBatis查詢數(shù)據(jù)庫(kù)語(yǔ)句的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-06-06JavaEE簡(jiǎn)介_(kāi)動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
這篇文章主要介紹了JavaEE簡(jiǎn)介,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-07-07使用Java將字節(jié)數(shù)組轉(zhuǎn)成16進(jìn)制形式的代碼實(shí)現(xiàn)
在很多場(chǎng)景下,需要進(jìn)行分析字節(jié)數(shù)據(jù),但是我們存起來(lái)的字節(jié)數(shù)據(jù)一般都是二進(jìn)制的,這時(shí)候就需要我們將其轉(zhuǎn)成16進(jìn)制的方式方便分析,本文主要介紹如何使用Java將字節(jié)數(shù)組格式化成16進(jìn)制的格式并輸出,需要的朋友可以參考下2024-05-05