【go】【Elasticsearch】

@


写在前面

  • 相关博文
  • 个人博客首页
  • 免责声明:仅供学习交流使用!开源框架可能存在的风险和相关后果将完全由用户自行承担,本人不承担任何法律责任。

[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())

参考资料

基础/标准库/第三方库


golang 导航


编程规范


算法|面试


项目


posted @ 2024-05-06 16:56  Nones  阅读(8)  评论(0编辑  收藏  举报