【go】【Elasticsearch】
@
写在前面
[install]
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())
参考资料
基础/标准库/第三方库
golang 导航
编程规范
算法|面试
项目
免责声明:
本站提供的资源,都来自网络,版权争议与本站无关,所有内容及软件的文章仅限用于学习和研究目的。不得将上述内容用于商业或者非法用途,否则,一切后果请用户自负,我们不保证内容的长久可用性,通过使用本站内容随之而来的风险与本站无关,您必须在下载后的24个小时之内,从您的电脑/手机中彻底删除上述内容。如果您喜欢该程序,请支持正版软件,购买注册,得到更好的正版服务。侵删请致信