结构体 faiss::IndexBinary
-
struct IndexBinary
用于二进制索引的抽象结构。
支持添加顶点和搜索它们。
所有查询都是对称的,因为代码和向量之间没有区别。
由 faiss::IndexBinaryFlat, faiss::IndexBinaryFromFloat, faiss::IndexBinaryHNSW, faiss::IndexBinaryHash, faiss::IndexBinaryIVF, faiss::IndexBinaryMultiHash, faiss::gpu::GpuIndexBinaryFlat 继承。
公共类型
-
using component_t = uint8_t
-
using distance_t = int32_t
公共函数
-
explicit IndexBinary(idx_t d = 0, MetricType metric = METRIC_L2)
-
virtual ~IndexBinary()
-
virtual void train(idx_t n, const uint8_t *x)
在代表性向量集上执行训练。
- 参数:
n – 训练向量的数量
x – 训练向量,大小为 n * d / 8
-
virtual void add(idx_t n, const uint8_t *x) = 0
将维度为 d 的 n 个向量添加到索引中。
向量被隐式分配标签 ntotal .. ntotal + n - 1
- 参数:
x – 输入矩阵,大小为 n * d / 8
-
virtual void add_with_ids(idx_t n, const uint8_t *x, const idx_t *xids)
与 add 相同,但存储 xids 而不是顺序 ids。
默认实现会因断言而失败,因为它并非所有索引都支持。
- 参数:
xids – 如果非空,则为向量存储的 ids(大小为 n)
-
virtual void search(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels, const SearchParameters *params = nullptr) const = 0
向索引查询维度为 d 的 n 个向量。
最多返回 k 个向量。 如果查询的结果不足,则结果数组将填充 -1。
- 参数:
x – 要搜索的输入向量,大小为 n * d / 8
labels – NNs 的输出标签,大小为 n*k
distances – 输出成对距离,大小为 n*k
-
virtual void range_search(idx_t n, const uint8_t *x, int radius, RangeSearchResult *result, const SearchParameters *params = nullptr) const
向索引查询维度为 d 的 n 个向量。
返回所有距离 < radius 的向量。 请注意,许多索引不实现 range_search(只有 k-NN 搜索是强制性的)。 距离转换为 float 以重用 RangeSearchResult 结构,但它们是整数。 按照惯例,仅返回距离 < radius(严格比较)的距离,即 radius = 0 不返回任何结果,1 仅返回完全相同的向量。
- 参数:
x – 要搜索的输入向量,大小为 n * d / 8
radius – 搜索半径
result – 结果表
-
void assign(idx_t n, const uint8_t *x, idx_t *labels, idx_t k = 1) const
返回与查询向量 x 最接近的 k 个向量的索引。
此函数与 search 相同,但仅返回邻居的标签。
- 参数:
x – 要搜索的输入向量,大小为 n * d / 8
labels – NNs 的输出标签,大小为 n*k
-
virtual void reset() = 0
从数据库中删除所有元素。
-
virtual size_t remove_ids(const IDSelector &sel)
从索引中删除 ID。并非所有索引都支持。
-
virtual void reconstruct(idx_t key, uint8_t *recons) const
重建存储的向量。
对于某些索引,可能未定义此函数。
- 参数:
key – 要重建的向量的 id
recons – 重建的向量(大小 d / 8)
-
virtual void reconstruct_n(idx_t i0, idx_t ni, uint8_t *recons) const
重建向量 i0 到 i0 + ni - 1。
对于某些索引,可能未定义此函数。
- 参数:
recons – 重建的向量(大小 ni * d / 8)
-
virtual void search_and_reconstruct(idx_t n, const uint8_t *x, idx_t k, int32_t *distances, idx_t *labels, uint8_t *recons, const SearchParameters *params = nullptr) const
类似于 search,但也会重建存储的向量(或者在有损编码的情况下进行近似),用于搜索结果。
如果查询的结果不足,则生成的数组将填充 -1。
- 参数:
recons – 重建的向量大小 (n, k, d)
-
void display() const
显示实际的类名和更多信息。
-
virtual void merge_from(IndexBinary &otherIndex, idx_t add_id = 0)
将另一个数据集中的条目移动到自身。在输出时,other 为空。 add_id 被添加到所有移动的 id(对于顺序 id,这将是 this->ntotal)
-
virtual void check_compatible_for_merge(const IndexBinary &otherIndex) const
检查两个索引是否兼容(即,它们以相同的方式进行训练并具有相同的参数)。否则抛出异常。
-
virtual size_t sa_code_size() const
生成的代码的大小(以字节为单位)
-
virtual void add_sa_codes(idx_t n, const uint8_t *codes, const idx_t *xids)
与 IndexBinary 的 add_with_ids 相同。
公共成员
-
int d = 0
向量维度
-
int code_size = 0
每个向量的字节数( = d / 8 )
-
idx_t ntotal = 0
索引向量的总数
-
bool verbose = false
详细级别
-
bool is_trained = true
如果 Index 不需要训练,或者已经完成训练,则设置此项
-
MetricType metric_type = METRIC_L2
此索引用于搜索的度量类型
-
using component_t = uint8_t