Flutter中Firestore Vector搜索相关API支持情况咨询
Firestore向量搜索的Dart SDK支持情况
向量存储支持
是的,Dart的cloud_firestore库(从版本4.10.0起)已经支持向量字段存储,使用FieldValue.vector(List<double>)方法即可。示例代码:
import 'package:cloud_firestore/cloud_firestore.dart'; // 新增带向量的文档 await FirebaseFirestore.instance.collection('products').add({ 'title': '无线耳机', 'embedding': FieldValue.vector([0.2, 0.5, 0.1, 0.8]) });
近邻查询支持
Dart SDK没有提供和Python/Node.js类似的find_nearest方法,而是通过Query.where的isNearestTo参数实现近邻检索。示例:
// 构造查询向量 final queryVec = [0.22, 0.51, 0.13, 0.78]; // 获取最近匹配的8个文档 final querySnapshot = await FirebaseFirestore.instance .collection('products') .where( 'embedding', isNearestTo: queryVec, limit: 8, ) .get(); // 遍历结果 for (var doc in querySnapshot.docs) { print('${doc.data()['title']} (匹配度由近到远排序)'); }
必要前提
- 确保
pubspec.yaml中cloud_firestore的版本≥4.10.0; - 需在Firestore控制台将目标字段配置为向量类型,并设置与你的向量维度一致的值。
内容的提问来源于stack exchange,提问作者Apoleo
相关产品推荐
相关产品推荐

