@
目錄
- 寫在前面
- [install]
- [connection]
- [low-level]
- index
- [create index]
- [update index]
- [indexing documents]
- [delete index]
- document
- [insert documents]
- [search documents]
- [update document]
- [delete document]
- index
- [full-type]
- [connection]
- [common]
- index
- [create index]
- [delete index]
- [update index]
- [get index]
- document
- [indexing documents]
- [get document]
- [search documents]
- [update document]
- [delete document]
- 參考資料
基礎/標準庫/第三方庫
golang 導航
程式設計規範
演算法|面試
專案
寫在前面
- 相關博文
- 個人部落格首頁
- 免責宣告:僅供學習交流使用!開源框架可能存在的風險和相關後果將完全由使用者自行承擔,本人不承擔任何法律責任。
[install]
https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/getting-started-go.html#_installation
go get github.com/elastic/go-elasticsearch/v8@latest
[connection]
- elastic cloud
client, err := elasticsearch.NewClient(elasticsearch.Config{
CloudID: "<CloudID>",
APIKey: "<ApiKey>",
})
- base authentication
cfg := elasticsearch.Config{
Addresses: []string{
"https://localhost:9200",
"https://localhost:9201",
},
Username: "foo",
Password: "bar",
}
- ca cert
cert, _ := os.ReadFile("/path/to/http_ca.crt")
cfg := elasticsearch.Config{
Addresses: []string{
"https://localhost:9200",
},
Username: "elastic",
Password: ELASTIC_PASSWORD
CACert: cert
}
es, err := elasticsearch.NewClient(cfg)
[low-level]
https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/getting-started-go.html
es, err := elasticsearch.NewClient(cfg)
index
[create index]
client.Indices.Create("test")
[update index]
client.Indices.PutSettings(strings.NewReader(`{ "settings": { "number_of_replicas": 3 }}`))
[indexing documents]
client.Indices.Delete([]string{"test"})
[delete index]
client.Indices.Delete([]string{"test"})
document
[insert documents]
document := struct {
Name string `json:"name"`
}{
"go-elasticsearch",
}
data, _ := json.Marshal(document)
client.Index("my_index", bytes.NewReader(data))
[search documents]
s := client.API.Search.WithIndex("test")
q := client.API.Search.WithQuery("info=info")
client.Search(s, q)
[update document]
client.Update("test", UUID, strings.NewReader(`{"doc":{ "id":111, "name": "Go", "info":"info"} }`))
[delete document]
client.Delete("test", UUID)
[full-type]
https://www.elastic.co/guide/en/elasticsearch/client/go-api/current/getting-started-go.html
[connection]
typedClient, err := elasticsearch.NewTypedClient(elasticsearch.Config{})
[common]
type User struct {
Id int `json:"id"`
Name string `json:"name"`
Age int `json:"age"`
}
type EsResponse struct {
Id string `json:"_id"`
Result string `json:"result"`
}
index
[create index]
typedClient.Indices.Create("my_index").Do(context.TODO())
[delete index]
typedClient.Indices.Delete("my_index").Do(context.TODO())
[update index]
typedClient.Indices.PutSettings().Indices("typed").NumberOfReplicas("3").Do(context.TODO())
[get index]
typedClient.GetSettings().Index("typed").Do(context.TODO())
document
[indexing documents]
document := struct {
Name string `json:"name"`
}{
"go-elasticsearch",
}
typedClient.Index("my_index").
Id("1").
Request(document).
Do(context.TODO())
---
*user := User{1, "name1", 11}*
*typedClient.Index("typed").Request(user).Do(context.TODO())*
[get document]
typedClient.Get("my_index", "id").Do(context.TODO())
[search documents]
typedClient.Search().
Index("my_index").
Request(&search.Request{
Query: &types.Query{MatchAll: &types.MatchAllQuery{}},
}).
Do(context.TODO())
*typedClient.Search().Index("typed").Query(&types.Query{QueryString: &types.QueryStringQuery{Query: "3"}}).Do(context.TODO())*
[update document]
typedClient.Update("my_index", "id").
Request(&update.Request{
Doc: json.RawMessage(`{ language: "Go" }`),
}).Do(context.TODO())
*user := User{2, "name1", 11}*
*typedClient.Update("typed", UUID).Doc(user).Do(context.TODO())*
[delete document]
typedClient.Delete("my_index", "id").Do(context.TODO())
參考資料
基礎/標準庫/第三方庫
- 地鼠文件:比較多資料
- topgoer
- go awesome
- golang 文件學習
- golang 標準庫
- go 檔案常用操作
golang 導航
- golang 收集
- go-guide
- golang 導航
- go-concurrency-guide
- go-advice
- golang 知識路線
程式設計規範
- golang 程式設計規範
- golang 規範示例
演算法|面試
- cs 面試
- 面試網站
- Golang後端研發崗位相關面試題和簡歷
- 路人張的面試筆記
- golang 演算法
專案
- golang 專案推薦
- 7天系列
- go專案推薦
- go高效能程式設計