news 2026/2/22 14:08:43

golang调用 elasticsearch 8,向量检索

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
golang调用 elasticsearch 8,向量检索

go get github.com/elastic/go-elasticsearch/v8

文档:https://www.elastic.co/docs/reference/elasticsearch/clients/go/getting-started#_indexing_documents

下载解压 elasticsearch-8.10.4

D:\dev\php\magook\trunk\server\elasticsearch-8.10.4\bin elasticsearch.bat
D:\dev\php\magook\trunk\server\elasticsearch-headnpmrun start

访问:http://127.0.0.1:9200/

访问:http://localhost:9100/

如果有向量类型的字段,需要先定义 mappings。

还需要特别注意的是 embedding 维度要匹配,elasticsearch中的dense_vector类型,在版本8.0 – 8.11中,默认的最高维度是2048,在 **8.12+**之后是4096,当然,这个值越高计算越慢。数据的维度必须小于es能存储的维度,否则会报错。

此处采用火山引擎的模型doubao-embedding-large-text-250515,其维度是2048,参考 模型列表

packagemainimport("bytes""context""encoding/json""fmt""log""os""strings""github.com/cloudwego/eino-ext/components/embedding/ark""github.com/elastic/go-elasticsearch/v8""github.com/elastic/go-elasticsearch/v8/esapi")funcmain(){client,err:=elasticsearch.NewClient(elasticsearch.Config{Addresses:[]string{"http://localhost:9200"},})iferr!=nil{log.Panicf("connect es8 failed, err=%v",err)}////////////////////////////////////////////createIndex(client,"my_index_vector")////////////////////////////////////////////ctx:=context.Background()ebs,err:=ark.NewEmbedder(ctx,&ark.EmbeddingConfig{BaseURL:os.Getenv("ARK_BASE_URL"),APIKey:os.Getenv("ARK_API_KEY"),Model:os.Getenv("ARK_EMBEDDING_MODEL"),})iferr!=nil{panic(err)}content:="Eino 旨在提供 Golang 语言的 AI 应用开发框架。 Eino 参考了开源社区中诸多优秀的 AI 应用开发框架,例如 LangChain、LangGraph、LlamaIndex 等,提供了更符合 Golang 编程习惯的 AI 应用开发框架。"res,err:=ebs.EmbedStrings(ctx,[]string{content})iferr!=nil{panic(err)}fmt.Println("info: ",len(res),len(res[0]))indexDocument(client,"my_index_vector","1",Document{Title:"Eino是什么",Content:content,Embedding:res[0],})}funccreateIndex(es*elasticsearch.Client,indexNamestring){mapping:=`{ "mappings": { "properties": { "title": { "type": "text" }, "content": { "type": "text" }, "embedding": { "type": "dense_vector", "dims": 2048, "index": true, "similarity": "cosine" } } } }`req:=esapi.IndicesCreateRequest{Index:indexName,Body:strings.NewReader(mapping),}res,err:=req.Do(context.Background(),es)iferr!=nil{log.Fatalf("Error creating index: %s",err)}deferres.Body.Close()ifres.IsError(){log.Printf("Error: %s",res.String())}else{fmt.Println("Index created successfully")}}typeDocumentstruct{Titlestring`json:"title"`Contentstring`json:"content"`Embedding[]float64`json:"embedding"`}funcindexDocument(es*elasticsearch.Client,indexName,idstring,doc Document){data,_:=json.Marshal(doc)req:=esapi.IndexRequest{Index:indexName,DocumentID:id,Body:strings.NewReader(string(data)),Refresh:"true",}res,err:=req.Do(context.Background(),es)iferr!=nil{log.Fatalf("Error indexing document: %s",err)}deferres.Body.Close()ifres.IsError(){log.Printf("Error: %s",res.String())}else{fmt.Printf("Document %s indexed\n",id)}}

输出

Index created successfully info:12048Document1indexed

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/2/20 17:30:23

海外网红营销:超越促销,用“圣诞故事”绑定品牌情感

每到圣诞季,全球品牌都会涌入折扣大战,但真正能在用户心里留下印记的,往往不是促销力度,而是能让消费者“感同身受”的情绪触点。圣诞节承载着人们对家庭、传统、温暖、陪伴与仪式感的期待,而海外网红营销真正的价值&a…

作者头像 李华
网站建设 2026/2/20 7:54:52

Qwen3-32B双模式大模型:重构企业AI效率的范式革命

Qwen3-32B双模式大模型:重构企业AI效率的范式革命 【免费下载链接】Qwen3-32B Qwen3-32B具有以下特点: 类型:因果语言模型 训练阶段:训练前和训练后 参数数量:32.8B 参数数量(非嵌入)&#xff1…

作者头像 李华
网站建设 2026/2/21 8:00:18

9、深入探索AppStack:创建、分配、测试与管理全流程

深入探索AppStack:创建、分配、测试与管理全流程 1. AppStack分配与测试 AppStack分配 为已创建的AppStack完成分配后,重复此过程,将其他AppStack分配给示例实验室中的不同Active Directory组: Evernote和VLC媒体播放器AppStack分配给销售组。 OpenOffice AppStack分配给…

作者头像 李华
网站建设 2026/2/18 19:03:43

12、ThinApp与App Volumes结合应用及Horizon View集成指南

ThinApp与App Volumes结合应用及Horizon View集成指南 1. 利用ThinApp创建AppStack 在使用ThinApp创建AppStack时,首先可以查看ThinApp创建的文件,为方便后续操作,需将这些文件复制到文件服务器的共享文件夹,以便供预配虚拟桌面机访问。 创建ThinApp AppStack的步骤如下…

作者头像 李华
网站建设 2026/2/21 6:49:14

16、在 Citrix XenApp 环境中部署 App Volumes

在 Citrix XenApp 环境中部署 App Volumes 1. 准备工作与初始配置 在重启服务器后,会出现一个对话框,点击“OK”关闭该对话框,即可完成预配。此时你将拥有一个包含 Office 2013 的 AppStack,它可以附加到 RDSH 服务器上。接下来,我们将把这个新创建的 AppStack 附加到用…

作者头像 李华