首页 > 其他分享 >【go】【Elasticsearch】

【go】【Elasticsearch】

时间:2024-05-06 17:00:53浏览次数:22  
标签:index typedClient client Elasticsearch go elasticsearch document

@

目录


写在前面

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

[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 导航


编程规范


算法|面试


项目


标签:index,typedClient,client,Elasticsearch,go,elasticsearch,document
From: https://www.cnblogs.com/nones/p/18175129

相关文章

  • django 表单
    实现方式一:将具体的search操作放到views首先,在app下面新建一个forms.py文件,用于定义表单。创建了一个名为"SearchCaseForm"的表单,并定义了一个名为“case_name”的CharFieldfromdjangoimportformsclassSearchCaseForm(forms.Form): case_name=forms.CharField()创建......
  • golang 获得一个结构体的字节大小
    golang的内存占用是如何构成的呢?unsafe.SizeOf()转载:如何在Go中获取变量的内存大小?--CSDN问答如果传递一个未初始化的变量,unsafe.Sizeof()与reflect.Type.Size()将只返回传递的变量的类型的大小,并不递归地遍历数据结构并增加所指向变量的大小。切片是一个相对简单的结构体st......
  • Elasticsearch query查询语法
    Elasticsearchquery查询语法本章介绍ES的query子句的语法,query子句主要用于编写查询条件,类似SQL中的where语句。1.匹配单个字段通过match实现全文搜索,全文搜索的后面有单独的章节讲解,这里大家只要知道简单的用法就可以。语法:GET/{索引名}/_search{"query":{"......
  • Elasticsearch 全文搜索
    Elasticsearch全文搜索全文搜索是ES的关键特性之一,平时我们使用SQL的like语句,搜索一些文本、字符串是否包含指定的关键词,但是如果两篇文章,都包含我们的关键词,具体那篇文章内容的相关度更高?这个SQL的like语句是做不到的,更别说like语句的性能问题了。ES通过分词处理、相关度计......
  • 我用 GitHub 9.8k 的 Go 语言 2D 游戏引擎写了个游戏
    前言hi,大家好,这里是白泽。今天给大家分享一个GitHub......
  • [992] Remove holes within polygons in a shapefile
    Toremoveholeswithinpolygonsinashapefile,youcanusethegeopandaslibraryinPython.Here'showyoucandoit:importgeopandasasgpd#Readtheshapefilegdf=gpd.read_file('path_to_shapefile.shp')#Removeholeswithinpolygon......
  • uwsgi+nginx启动Django静态文件设置
    总体思路:设置好STATIC_ROOT后使用pythonmanager.pycollectstatic命令将django下所有的静态文件搜集到STATIC_ROOT下,然后让nginx的static路由指向这个目录1.Django的settings.py文件中设置如下STATIC_URL='/static/'STATIC_ROOT=os.path.join(BASE_DIR,'staticfiles'......
  • 用Golang做一个永久阻塞,有哪些小技巧 ?
    用Golang做一个永久阻塞,有哪些小技巧?磊丰 Go语言圈 2024-05-0608:30 广东 听全文Go语言圈Go语言开发者的学习好助手,分享Go语言知识,技术技巧,学习与交流Go语言开发经验,互动才有助于技术的提升,每天5分钟,助你GO语言技术快乐成长159篇原创内容公众号......
  • [ARC159F] Good Division
    题意给定一个长度为\(2\timesn\)的数列\(S\)。称一个数列是好的,当且仅当数列中的数可以由每次删除相邻两个不同的数的操作删空。求划分该数列为若干好的字串的方案数。Sol集中注意力。首先显然长度为奇数的序列是没法做的。若序列存在绝对众数,则该序列一定无法删除......
  • 深入学习和理解Django模板层:构建动态页面
    title:深入学习和理解Django模板层:构建动态页面date:2024/5/520:53:51updated:2024/5/520:53:51categories:后端开发tags:Django模板表单处理静态文件国际化性能优化安全防护部署实践第一章:模板语法基础Django模板语法介绍Django模板语法是一种简洁而......