.Net使用Elastic.Clients.Elasticsearch在Elasticsearch8中实现向量存储和相似度检索

server/2024/9/23 3:52:32/

文章目录

      • 一、测试环境
      • 二、代码
        • 1、创建包含DenseVector的索引
        • 2、索引文档
        • 3、对向量字段进行近似knn检索
      • 三、参考

一、测试环境

Elastic.Clients.Elasticsearch版本:8.13.0
Elasticsearch版本:8.13.0

二、代码

1、创建包含DenseVector的索引
public static bool InitIndex()
{// 定义索引配置var faceVectorproperties = new Properties{{ "Id" ,new KeywordProperty()},{ "FileID" ,new KeywordProperty()},{ "FileGUID" ,new KeywordProperty()},{ "ResourceID" ,new KeywordProperty()},{ "FileName" ,new TextProperty()},{ "Embedding" ,new DenseVectorProperty{Dims = 3 } }};// 定义索引配置var indexConfig = new IndexState{Settings = new IndexSettings{NumberOfShards = 1, // 设置分片数NumberOfReplicas = 1 // 设置副本数},Mappings = new TypeMapping{Properties = faceVectorproperties}};//判断是否已经存在该索引var existFaceVectorIndexResponse = _client.Indices.ExistsAsync("FaceVector").Result;if (!existFaceVectorIndexResponse.IsValidResponse){// 创建索引请求var createIndexRequest = new CreateIndexRequest("FaceVector"){Settings = indexConfig.Settings,Mappings = indexConfig.Mappings};var createFaceVectorIndexResponse = _client.Indices.CreateAsync(createIndexRequest).Result;if (createFaceVectorIndexResponse.Acknowledged){//添加一条测试数据ES_FaceVector temp = new ES_FaceVector{FileID = 0,FileGUID = Guid.NewGuid(),ResourceID = 0,FileName = "测试",Embedding = new float[] {1.2f,1.1f,1.3f }};var addDocResult = AddDoc<ES_FaceVector>(temp, ElasticIndexEnum.FaceVector);}else{return false;}}return true;
}
2、索引文档
//批量索引文档
public static bool AddDocs<T>(List<T> data, string indexName) where T : class
{var bulkIndexResponse = _client.BulkAsync(b => b.Index(indexName).IndexMany(data)).Result;return bulkIndexResponse.IsValidResponse;
}
//单个索引文档
public static bool AddDoc<T>(T data, string indexName) where T : class
{var response = _client.IndexAsync(data, indexName).Result;return response.IsValidResponse;
}
3、对向量字段进行近似knn检索
public static void SearchKnn()
{// 构建KNN查询var doubleArr = new[] { -0.04604065, 0.054946236, 0.057453074};var arrLen = doubleArr.Length;var knnQuery = new KnnQuery(){k = 2,NumCandidates = 1000,Field = "embedding",QueryVector= doubleArr.Select(s=>(float)s).ToArray()};// 构建Elasticsearch查询var searchRequest = new SearchRequest<ES_FaceVector>(ElasticIndexEnum.FaceVector){Knn = new KnnQuery[] { knnQuery },MinScore = 0.90,SourceIncludes = new [] { "fileName", "embedding" }};var searchResponse = _client.Search<ES_FaceVector>(searchRequest);if (searchResponse.IsValidResponse){foreach (var hit in searchResponse.Hits){// 处理每个文档的结果var fileNameTemp = hit.Source.FileName;var embeddingTemp = hit.Source.Embedding;}}else{Console.WriteLine($"Error: {searchResponse.DebugInformation}");}
}

三、参考

.Net使用Elastic.Clients.Elasticsearch连接Elasticsearch8

https://www.elastic.co/guide/en/elasticsearch/client/net-api/8.13/connecting.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/knn-search.html



http://www.ppmy.cn/server/10078.html

相关文章

【xhs爬虫软件】把小红书博主发布笔记API接口user_posted接口封装成GUI采集工具!

用Python开发的爬虫采集软件&#xff0c;可自动抓取小红书博主的已发布笔记。 小红书的已发布笔记接口URL&#xff1a; # 请求地址 posted_url https://edith.xiaohongshu.com/api/sns/web/v1/user_posted开发者模式分析过程&#xff1a; 进而封装成GUI界面软件&#xff0c;…

微服务:Eureka原理实践:@EnableEurekaServer、@LoadBalanced

个人简介&#xff1a;Java领域新星创作者&#xff1b;阿里云技术博主、星级博主、专家博主&#xff1b;正在Java学习的路上摸爬滚打&#xff0c;记录学习的过程~ 个人主页&#xff1a;.29.的博客 学习社区&#xff1a;进去逛一逛~ 微服务&#xff1a;Eureka原理实践&#xff1a…

双核MCU中的数据共享与DPRAM的工作机制对比分析

目录 一、同构双核MCU中数据共享的机制 1.缓存一致性&#xff1a; 2.互斥锁&#xff1a; 3.缓存同步&#xff1a; 4.信号量/原子操作&#xff1a; 5.直接内存访问&#xff1a; 6.消息传递&#xff1a; 二、DPRAM中数据共享的机制及两者的区别 1.DPRAM中数据共享的机制…

Flume在大数据集群下的配置以及监控工具Ganglia的部署安装

前提&#xff1a;需要有三台虚拟机&#xff08;hadoop102,103,104&#xff09;配置好相关基础环境 安装 将安装包上传到/opt/software中 tar -zxf /opt/software/apache-flume-1.9.0-bin.tar.gz -C /opt/module/修改 apache-flume-1.9.0-bin 的名称为 flume mv /opt/module/…

【第六章】集合类:List、Iterator迭代器

目录 1.集合类 1.1 什么是集合类 1.2 集合类的分类 2.Collection接口 3.List集合 3.1 ArrayList集合 3.2 LinkedList集合 3.3 ArrayList和LinkedList对比 3.4 创建List对象 4 Iterator接口 5 foreach循环 6 代码练习 1.集合类 1.1 什么是集合类 集合类就像一个容器&a…

Leetcode 1047:删除字符串中的所有相邻重复项

给出由小写字母组成的字符串 S&#xff0c;重复项删除操作会选择两个相邻且相同的字母&#xff0c;并删除它们。 在 S 上反复执行重复项删除操作&#xff0c;直到无法继续删除。 在完成所有重复项删除操作后返回最终的字符串。答案保证唯一。 import java.util.Stack;public…

代码随想录算法训练营Day6 | 242.有效的字母异位词 ●349. 两个数组的交集 ● 202. 快乐数● 1. 两数之和

基础&#xff1a; 1.哈希表是根据关键值进行直接访问的数据结构&#xff0c;时间复杂度是O(1)&#xff0c;也就是通过数组的索引下标&#xff0c;直接访问数组中的元素哈希表的作用就是用来快速判断一个元素是否出现在集合里。 2.常见的哈希结构&#xff1a; 数组set &#…

【LLM】向量知识库

文章目录 认识向量知识库向量Embeddings向量数据库向量数据库的作用向量数据库与传统数据库的区别 Embedding API使用公有Embedding API自定义一个Embeedding API 常见文本数据的预处理搭建并使用向量数据库思考向量数据库在LLM中的价值体现向量的妙用&#xff0c;可行&#xf…