news 2026/7/10 5:24:20

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/7/9 17:24:38

Redis 主从搭建笔记

Redis 主从搭建笔记学习视频:Redis入门到精通: https://url90.ctfile.com/d/51188890-157896900-8b14ca?p3489 (访问密码: 3489)一、主从架构核心说明Redis 主从复制(Master-Slave)是实现数据冗余、读写分离的基础架构:主库&…

作者头像 李华
网站建设 2026/7/10 7:50:05

17、DB2 pureXML 操作指南

DB2 pureXML 操作指南 1. XQuery 示例 可以运行使用 FLWOR 表达式的 XQuery 语句,示例如下: xquery for $d in db2-fn:xmlcolumn(dept.deptdoc)/dept let $emp := $d//employee/name where $d/@bldg > 95 order by $d/@bldg return<EmpList>{$d/@bldg, $emp}<…

作者头像 李华
网站建设 2026/7/10 7:59:18

18、深入探索 DB2 pureXML 技术:从基础操作到故障排查

深入探索 DB2 pureXML 技术:从基础操作到故障排查 在当今数据驱动的时代,XML 数据的处理和管理变得愈发重要。DB2 pureXML 技术为我们提供了强大的功能,能够高效地存储、查询和操作 XML 数据。本文将深入探讨 DB2 pureXML 的相关知识,包括 XML 文档的更新、删除、索引创建…

作者头像 李华