SpringBoot集成Elasticseach
目錄
一、Elasticseach介紹
1.簡單介紹
2.對(duì)比關(guān)系:
3.詳細(xì)說明:
4.查出數(shù)據(jù)的解釋
二、SpringBoot集成Elasticseach
1.引入依賴
2.添加配置
3.創(chuàng)建pojo類與索引對(duì)應(yīng)
4.SpringData封裝了基礎(chǔ)的增刪改查,自定義增刪改查
5.測試方法--增刪改查
如果本篇博客對(duì)您有一定的幫助,大家記得留言++哦。
一、Elasticseach介紹
1.簡單介紹
官網(wǎng):開源搜索:Elasticsearch、ELK Stack 和 Kibana 的開發(fā)者
ElasticSeach詳細(xì)安裝教程--圖文介紹超詳細(xì):ElasticSeach詳細(xì)安裝教程
令人記憶深刻的口號(hào):能夠發(fā)現(xiàn)意料之中以及意料之外的情況
Elasticsearch也是基于Lucene的全文檢索庫,本質(zhì)也是存儲(chǔ)數(shù)據(jù),很多概念與MySQL類似的。是一種全文檢索技術(shù)。
Elasticsearch 是位于 Elastic Stack 核心的分布式搜索和分析引擎。Logstash 和 Beats 有助于收集、聚合和豐富您的數(shù)據(jù)并將其存儲(chǔ)在 Elasticsearch 中。Kibana 使您能夠以交互方式探索、可視化和分享對(duì)數(shù)據(jù)的見解,并管理和監(jiān)控堆棧。Elasticsearch 是索引、搜索和分析魔法發(fā)生的地方。
Elasticsearch 是一個(gè)基于JSON的分布式搜索和分析引擎。
Elasticsearch是用Java語言開發(fā)的,并作為Apache許可條款下的開放源碼發(fā)布,是一種流行的企業(yè)級(jí)搜索引擎。Elasticsearch用于云計(jì)算中,能夠達(dá)到實(shí)時(shí)搜索,穩(wěn)定,可靠,快速,安裝使用方便。
2.對(duì)比關(guān)系:
索引(indices)--------------------------------Databases 數(shù)據(jù)庫 類型(type)-----------------------------Table 數(shù)據(jù)表 文檔(Document)----------------Row 行 字段(Field)-------------------Columns 列
3.詳細(xì)說明:
概念
說明
索引庫(indices)
indices是index的復(fù)數(shù),代表許多的索引,
類型(type)
類型是模擬mysql中的table概念,一個(gè)索引庫下可以有不同類型的索引,比如商品索引,訂單索引,其數(shù)據(jù)格式不同。不過這會(huì)導(dǎo)致索引庫混亂,因此未來版本中會(huì)移除這個(gè)概念
文檔(document)
存入索引庫原始的數(shù)據(jù)。比如每一條商品信息,就是一個(gè)文檔
字段(field)
文檔中的屬性
映射配置(mappings)
字段的數(shù)據(jù)類型、屬性、是否索引、是否存儲(chǔ)等特性
4.查出數(shù)據(jù)的解釋
took:查詢花費(fèi)時(shí)間,單位是毫秒
time_out:是否超時(shí)
_shards:分片信息
hits:搜索結(jié)果總覽對(duì)象
total:搜索到的總條數(shù)
max_score:所有結(jié)果中文檔得分的最高分
hits:搜索結(jié)果的文檔對(duì)象數(shù)組,每個(gè)元素是一條搜索到的文檔信息
_index:索引庫
_type:文檔類型
_id:文檔id
_score:文檔得分
_source:文檔的源數(shù)據(jù)
二、SpringBoot集成Elasticseach
1.引入依賴
2.添加配置
spring: data: elasticsearch: cluster-name: elasticsearch cluster-nodes: 192.168.7.132:9300
3.創(chuàng)建pojo類與索引對(duì)應(yīng)
package com.leyou.elasticsearch.pojo; import org.springframework.data.annotation.Id; import org.springframework.data.elasticsearch.annotations.Document; import org.springframework.data.elasticsearch.annotations.Field; import org.springframework.data.elasticsearch.annotations.FieldType; /** * 創(chuàng)建pojo類與索引對(duì)應(yīng) * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/1/26 - 20:35 */ @Document(indexName = "item", type = "docs", shards = 1, replicas = 0) public class Item { @Id private Long id; /** * 標(biāo)題 */ @Field(type = FieldType.Text,analyzer = "ik_max_word") private String title; /** * 分類 */ @Field(type = FieldType.Keyword) private String category; /** * 品牌 */ @Field(type = FieldType.Keyword) private String brand; /** * 價(jià)格 */ @Field(type = FieldType.Double) private Double price; /** * 圖片地址 */ @Field(type = FieldType.Keyword) private String images; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public Double getPrice() { return price; } public void setPrice(Double price) { this.price = price; } public String getImages() { return images; } public void setImages(String images) { this.images = images; } public Item() { } public Item(Long id, String title, String category, String brand, Double price, String images) { this.id = id; this.title = title; this.category = category; this.brand = brand; this.price = price; this.images = images; } @Override public String toString() { return "Item{" + "id=" + id + ", title='" + title + '\'' + ", category='" + category + '\'' + ", brand='" + brand + '\'' + ", price=" + price + ", images='" + images + '\'' + '}'; } }
4.SpringData封裝了基礎(chǔ)的增刪改查,自定義增刪改查
這里需要繼承接口-ItemRepository
/** * 自定義的增刪改查接口 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/1/27 - 15:10 */ public interface ItemRepository extends ElasticsearchRepository
5.測試方法--增刪改查
package com.leyou.elasticsearch; import com.leyou.elasticsearch.dao.ItemRepository; import com.leyou.elasticsearch.pojo.Item; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.sort.SortBuilders; import org.elasticsearch.search.sort.SortOrder; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.domain.Sort; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.test.context.junit4.SpringRunner; import java.util.ArrayList; import java.util.List; import java.util.Optional; /** * 測試ES的增刪改查 * * @author Promsing(張有博) * @version 1.0.0 * @since 2022/1/26 - 20:57 */ @SpringBootTest(classes = ElasticsearchApplication.class) @RunWith(SpringRunner.class) public class ElasticsearchTest { @Autowired private ElasticsearchTemplate elasticsearchTemplate;//ES的模板類 @Autowired private ItemRepository itemRepository;//Item的增刪改查 /** * 創(chuàng)建索引庫 */ @Test public void testIndex(){ this.elasticsearchTemplate.createIndex(Item.class); this.elasticsearchTemplate.putMapping(Item.class); //this.elasticsearchTemplate.deleteIndex(); } /** * 插入與更新 */ @Test public void testCreate(){ Item item = new Item(1L,"小米手機(jī)9","手機(jī)","小米",3999.00,"https:www.baidu.com"); Object save = this.itemRepository.save(item); List
如果本篇博客對(duì)您有一定的幫助,大家記得留言++哦。
Elasticsearch 實(shí)時(shí)流計(jì)算服務(wù) CS
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。