MySQL和MariaDB的向量搜索Neighbor二进制向量实战教程【免费下载链接】neighborNearest neighbor search for Rails项目地址: https://gitcode.com/gh_mirrors/ne/neighborNeighbor是一款专为Rails设计的最近邻搜索工具支持MySQL和MariaDB等多种数据库的向量搜索功能。本文将详细介绍如何使用Neighbor在MySQL和MariaDB中实现高效的二进制向量搜索帮助开发者轻松构建高性能的相似性检索系统。为什么选择二进制向量二进制向量Binary Vectors是一种高效的向量表示方式它将浮点数向量转换为二进制形式存储具有以下优势存储空间小二进制向量比传统浮点向量节省8-32倍存储空间计算速度快使用汉明距离Hamming Distance计算相似度运算效率更高索引效率高适合构建高效的向量索引提升查询性能Neighbor为MySQL和MariaDB提供了完整的二进制向量支持让开发者能够轻松集成这一强大功能。环境准备系统要求MariaDB 11.8 或 MySQL 9.7搜索功能需要HeatWave支持Rails 8.1Ruby 3.0安装Neighbor在Rails项目的Gemfile中添加gem neighbor然后运行bundle installMariaDB二进制向量实现创建二进制向量列MariaDB使用bigint类型存储二进制向量最多支持64个维度。创建迁移文件class AddEmbeddingToItems ActiveRecord::Migration[8.1] def change add_column :items, :embedding, :bigint end end运行迁移rails db:migrate配置模型在模型中添加Neighbor支持class Item ApplicationRecord has_neighbors :embedding end存储二进制向量将二进制向量表示为整数存储# 存储二进制向量 101对应整数5 item.update(embedding: 5)执行相似性搜索使用汉明距离查找最近邻# 查找与向量5二进制101最相似的5个项目 Item.nearest_neighbors(:embedding, 5, distance: hamming).first(5)创建向量索引为提升查询性能添加向量索引注意向量列必须设置null: falseclass CreateItems ActiveRecord::Migration[8.1] def change create_table :items do |t| t.vector :embedding, limit: 3, null: false t.index :embedding, type: :vector end end endMySQL二进制向量实现创建二进制向量列MySQL使用binary类型存储二进制向量创建迁移文件class AddEmbeddingToItems ActiveRecord::Migration[8.1] def change add_column :items, :embedding, :binary end end运行迁移rails db:migrate配置模型在模型中添加Neighbor支持class Item ApplicationRecord has_neighbors :embedding end存储二进制向量将二进制向量表示为字节字符串存储# 存储二进制向量 \x05对应二进制101 item.update(embedding: \x05)执行相似性搜索使用汉明距离查找最近邻# 查找与向量\x05最相似的5个项目 Item.nearest_neighbors(:embedding, \x05, distance: hamming).first(5)支持的距离度量Neighbor为MySQL和MariaDB提供了多种距离度量方式Euclidean欧氏距离适用于连续向量Cosine余弦相似度适用于方向相似性比较Hamming汉明距离专门用于二进制向量在查询时通过distance参数指定# 使用余弦相似度 Item.nearest_neighbors(:embedding, vector, distance: cosine).first(5)实际应用示例文本相似性搜索使用二进制向量实现文本相似性搜索将文本转换为二进制向量可使用Cohere等服务的ubinary嵌入存储二进制向量到数据库对查询文本进行同样处理然后搜索最近邻# 伪代码示例 def embed_text(text) # 调用嵌入API将文本转换为二进制向量 response Net::HTTP.post(URI(https://api.cohere.com/v2/embed), { texts: [text], model: embed-v4.0, embedding_types: [ubinary] }.to_json, headers) JSON.parse(response.body)[embeddings][ubinary].first end # 存储文本嵌入 document Document.create(content: The dog is barking, embedding: embed_text(The dog is barking)) # 搜索相似文本 query_embedding embed_text(A canine is making noise) similar_documents Document.nearest_neighbors(:embedding, query_embedding, distance: hamming).first(5)推荐系统使用二进制向量构建简单的推荐系统# 为产品创建嵌入 product Product.create(name: Wireless Headphones, embedding: product_embedding) # 查找相似产品 similar_products product.nearest_neighbors(:embedding, distance: hamming).first(5)性能优化技巧合理设置维度根据实际需求选择合适的向量维度避免维度灾难使用索引为向量列创建索引显著提升查询速度批量操作使用批量插入和更新减少数据库交互次数结果限制使用first(5)等限制返回结果数量减少数据传输常见问题解决向量维度不匹配确保所有向量具有相同的维度可在模型中指定class Item ApplicationRecord has_neighbors :embedding, dimensions: 32 # 指定32维 end索引创建失败MariaDB向量索引要求列设置null: falseadd_column :items, :embedding, :bigint, null: falseMySQL搜索功能不可用MySQL的DISTANCE()函数仅在HeatWave中可用确保你的MySQL环境已启用HeatWave。总结Neighbor为MySQL和MariaDB提供了强大而简洁的二进制向量搜索功能让开发者能够轻松构建高性能的相似性检索系统。通过本文介绍的方法你可以快速在Rails项目中集成二进制向量搜索为用户提供更智能、更高效的体验。无论是文本相似性搜索、推荐系统还是其他需要相似性比较的场景Neighbor的二进制向量功能都能为你提供出色的性能和易用性。现在就尝试在你的项目中使用Neighbor开启高效向量搜索之旅吧【免费下载链接】neighborNearest neighbor search for Rails项目地址: https://gitcode.com/gh_mirrors/ne/neighbor创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考