網站首頁 編程語言 正文
Elasticsearch簡介
Elasticsearch 是一個開源的搜索引擎,建立在一個全文搜索引擎庫 Apache Lucene? 基礎之上。 Lucene 可以說是當下最先進、高性能、全功能的搜索引擎庫–無論是開源還是私有。
連接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", } // 創建 client, err := es8.NewClient(conf); if err != nil{ fmt.Println("============= 創建 elasticsearch 失敗 =============") return } // 連接 _, err1 := client.Info() if err1 != nil{ fmt.Println("============= 連接 elasticsearch 失敗 =============") return }
創建索引
創建model結構體
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: "測試", Mobile: "15222222222", }
創建索引
// 結構體json序列化 str,err := json.Marshal(admin); if err != nil{ return ; } // 創建索引 res1, err1 := client.Index( "db.table", bytes.NewReader(str), client.Index.WithDocumentID("1"), // 索引ID client.Index.WithRefresh("true") //是否立即創建 );
搜索數據
創建返回結構體
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"` }
搜索數據
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 ; }
解析數據
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) }
修改數據
單條修改
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 ; }
刪除數據
單條刪除
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
相關推薦
- 2022-12-09 python淺拷貝與深拷貝使用方法詳解_python
- 2022-04-22 git push時出現403,443
- 2022-07-26 pyspark中udf寫法及其使用
- 2022-05-29 C#實現文本轉語音功能_C#教程
- 2022-07-11 UVM中uvm_config_db在sequence中的使用
- 2022-04-07 WPF實現數據綁定_實用技巧
- 2022-04-15 C語言的程序環境與預處理你真的了解嗎_C 語言
- 2022-11-02 python函數和python匿名函數lambda詳解_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支