網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
Elasticsearch簡(jiǎn)介
Elasticsearch 是一個(gè)開源的搜索引擎,建立在一個(gè)全文搜索引擎庫(kù) Apache Lucene? 基礎(chǔ)之上。 Lucene 可以說是當(dāng)下最先進(jìn)、高性能、全功能的搜索引擎庫(kù)–無論是開源還是私有。
連接Elasticsearch
// 引入g~ ~~~~~-elasticsearch import ( es8 "github.com/elastic/go-elasticsearch/v8" ) // go-es配置 conf := es8.Config{ Addresses: "http://127.0.0.1:9200", Username:"elastic", Password:"jloMQ7ZCTlcZUr_hmDoB", } // 創(chuàng)建 client, err := es8.NewClient(conf); if err != nil{ fmt.Println("============= 創(chuàng)建 elasticsearch 失敗 =============") return } // 連接 _, err1 := client.Info() if err1 != nil{ fmt.Println("============= 連接 elasticsearch 失敗 =============") return }
創(chuàng)建索引
創(chuàng)建model結(jié)構(gòu)體
type Admin struct{ Id int `gorm:"<-" json:"id"` UserName string `gorm:"<-" json:"user_name"` RealName string `gorm:"<-" json:"real_name"` Mobile string `gorm:"<-" json:"mobile"` }
初始化model
admin := Admin{ Id: 1, UserName: "test", RealName: "測(cè)試", Mobile: "15222222222", }
創(chuàng)建索引
// 結(jié)構(gòu)體json序列化 str,err := json.Marshal(admin); if err != nil{ return ; } // 創(chuàng)建索引 res1, err1 := client.Index( "db.table", bytes.NewReader(str), client.Index.WithDocumentID("1"), // 索引ID client.Index.WithRefresh("true") //是否立即創(chuàng)建 );
搜索數(shù)據(jù)
創(chuàng)建返回結(jié)構(gòu)體
type EsResponse struct{ Hits struct{ Total struct{ Value int `json:"value"` } `json:"total"` Hits []struct{ Index string `json:"_index"` Id string `json:"_id"` Score float32 `json:"_score"` Source map[string]any `json:"_source"` } `json:"hits"` } `json:"hits"` } type EsData struct{ Total int `json:"total"` List []map[string]any `json:"list"` }
搜索數(shù)據(jù)
query := map[string]any{ "query":map[string]any{ "bool":map[string]any{ "must":[]map[string]any{ map[string]any{ "match_phrase":map[string]any{ "user_name": "test", }, }, map[string]any{ "match_phrase":map[string]any{ "mobile": "15222222222", }, }, }, }, }, } str,err := json.Marshal(query); if err != nil{ return ; } res1,err1 := client.Search(client.Search.WithBody(bytes.NewReader(str))); if err1 != nil{ return ; }
解析數(shù)據(jù)
var resData EsResponse; err2 := json.NewDecoder(res1.Body).Decode(&resData); if err2 != nil{ return ; } var esData EsData; esData.Total = resData.Hits.Total.Value; for _, v := range resData.Hits.Hits { cache := v.Source cache["_index"] = v.Index; cache["_id"] = v.Id; cache["_score"] = v.Score; esData.List = append(esData.List,cache) }
修改數(shù)據(jù)
單條修改
update := map[string]any{ "script": map[string]any{ "source":"ctx._source.user_name='test1';ctx._source.mobile='15211111111';", "lang": "painless", }, } str,err1 := json.Marshal(update) if err1 != nil{ return ; } res2,err2 := client.Update( "db.table", "1", bytes.NewReader(str), client.Update.WithRefresh("true") ) if err2 != nil{ return ; }
批量修改
update := map[string]any{ "query":map[string]any{ "bool":map[string]any{ "must":[]map[string]any{ map[string]any{ "match_phrase":map[string]any{ "user_name": "test", }, }, map[string]any{ "match_phrase":map[string]any{ "mobile": "15222222222", }, }, }, }, }, "script": map[string]any{ "source":"ctx._source.user_name='test1';ctx._source.mobile='15211111111';", "lang": "painless", }, } str,err1 := json.Marshal(update) if err1 != nil{ return ; } res2,err2 := client.UpdateByQuery( []string{ "db.table", }, client.UpdateByQuery.WithBody(bytes.NewReader(str)), client.UpdateByQuery.WithRefresh(true) ) if err2 != nil{ return ; }
刪除數(shù)據(jù)
單條刪除
res,err := client.Delete( "db.table", "1", client.Delete.WithRefresh("true") ) if err != nil{ return ; }
批量刪除
query := map[string]any{ "query":map[string]any{ "bool":map[string]any{ "must":[]map[string]any{ map[string]any{ "match_phrase":map[string]any{ "user_name": "test", }, }, map[string]any{ "match_phrase":map[string]any{ "mobile": "15222222222", }, }, }, }, }, } str,err := json.Marshal(query); if err != nil{ return ; } res,err := client.DeleteByQuery( []string{ "db.table", }, bytes.NewReader(str), client.DeleteByQuery.WithRefresh(true) )
原文鏈接:https://blog.51cto.com/lzcit/6010742
相關(guān)推薦
- 2022-04-14 Python中字典的相關(guān)操作介紹_python
- 2022-07-16 git查看和修改用戶名和郵箱
- 2022-03-31 C#實(shí)現(xiàn)單位換算器_C#教程
- 2022-07-25 Python?APScheduler?定時(shí)任務(wù)詳解_python
- 2022-11-19 VSstudio中scanf返回值被忽略的原因及解決方法(推薦)_C 語(yǔ)言
- 2022-03-29 帶你了解C++中vector的用法_C 語(yǔ)言
- 2022-10-01 使用301永久重定向和302臨時(shí)重定向作用區(qū)別詳解_相關(guān)技巧
- 2024-02-17 pytorch花式索引提取topk的張量
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支