1 module faiss.index;
2 
3 import faiss.common;
4 
5 /**
6  * Copyright (c) Facebook, Inc. and its affiliates.
7  *
8  * This source code is licensed under the MIT license found in the
9  * LICENSE file in the root directory of this source tree.
10  */
11 
12 // Copyright 2004-present Facebook. All Rights Reserved
13 // -*- c -*-
14 
15 extern (C):
16 
17 // forward declaration required here
18 struct FaissRangeSearchResult_H;
19 alias FaissRangeSearchResult = FaissRangeSearchResult_H;
20 
21 // typedef struct FaissRangeSearchResult_H FaissRangeSearchResult;
22 struct FaissIDSelector_H;
23 alias FaissIDSelector = FaissIDSelector_H;
24 
25 /// Some algorithms support both an inner product version and a L2 search
26 /// version.
27 enum FaissMetricType
28 {
29     METRIC_INNER_PRODUCT = 0, ///< maximum inner product search
30     METRIC_L2 = 1, ///< squared L2 search
31     METRIC_L1 = 2, ///< L1 (aka cityblock)
32     METRIC_Linf = 3, ///< infinity distance
33     METRIC_Lp = 4, ///< L_p distance, p is given by metric_arg
34 
35     /// some additional metrics defined in scipy.spatial.distance
36     METRIC_Canberra = 20,
37     METRIC_BrayCurtis = 21,
38     METRIC_JensenShannon = 22
39 }
40 
41 /// Opaque type for referencing to an index object
42 struct FaissIndex_H;
43 alias FaissIndex = FaissIndex_H;
44 void faiss_Index_free (FaissIndex* obj);
45 
46 /// Getter for d
47 int faiss_Index_d (const(FaissIndex)*);
48 
49 /// Getter for is_trained
50 int faiss_Index_is_trained (const(FaissIndex)*);
51 
52 /// Getter for ntotal
53 idx_t faiss_Index_ntotal (const(FaissIndex)*);
54 
55 /// Getter for metric_type
56 FaissMetricType faiss_Index_metric_type (const(FaissIndex)*);
57 
58 int faiss_Index_verbose (const(FaissIndex)*);
59 void faiss_Index_set_verbose (FaissIndex*, int);
60 
61 /** Perform training on a representative set of vectors
62  *
63  * @param index  opaque pointer to index object
64  * @param n      nb of training vectors
65  * @param x      training vectors, size n * d
66  */
67 int faiss_Index_train (FaissIndex* index, idx_t n, const(float)* x);
68 
69 /** Add n vectors of dimension d to the index.
70  *
71  * Vectors are implicitly assigned labels ntotal .. ntotal + n - 1
72  * This function slices the input vectors in chunks smaller than
73  * blocksize_add and calls add_core.
74  * @param index  opaque pointer to index object
75  * @param x      input matrix, size n * d
76  */
77 int faiss_Index_add (FaissIndex* index, idx_t n, const(float)* x);
78 
79 /** Same as add, but stores xids instead of sequential ids.
80  *
81  * The default implementation fails with an assertion, as it is
82  * not supported by all indexes.
83  *
84  * @param index  opaque pointer to index object
85  * @param xids   if non-null, ids to store for the vectors (size n)
86  */
87 int faiss_Index_add_with_ids (
88     FaissIndex* index,
89     idx_t n,
90     const(float)* x,
91     const(idx_t)* xids);
92 
93 /** query n vectors of dimension d to the index.
94  *
95  * return at most k vectors. If there are not enough results for a
96  * query, the result array is padded with -1s.
97  *
98  * @param index       opaque pointer to index object
99  * @param x           input vectors to search, size n * d
100  * @param labels      output labels of the NNs, size n*k
101  * @param distances   output pairwise distances, size n*k
102  */
103 int faiss_Index_search (
104     const(FaissIndex)* index,
105     idx_t n,
106     const(float)* x,
107     idx_t k,
108     float* distances,
109     idx_t* labels);
110 
111 /** query n vectors of dimension d to the index.
112  *
113  * return all vectors with distance < radius. Note that many
114  * indexes do not implement the range_search (only the k-NN search
115  * is mandatory).
116  *
117  * @param index       opaque pointer to index object
118  * @param x           input vectors to search, size n * d
119  * @param radius      search radius
120  * @param result      result table
121  */
122 int faiss_Index_range_search (
123     const(FaissIndex)* index,
124     idx_t n,
125     const(float)* x,
126     float radius,
127     FaissRangeSearchResult* result);
128 
129 /** return the indexes of the k vectors closest to the query x.
130  *
131  * This function is identical as search but only return labels of neighbors.
132  * @param index       opaque pointer to index object
133  * @param x           input vectors to search, size n * d
134  * @param labels      output labels of the NNs, size n*k
135  */
136 int faiss_Index_assign (
137     FaissIndex* index,
138     idx_t n,
139     const(float)* x,
140     idx_t* labels,
141     idx_t k);
142 
143 /** removes all elements from the database.
144  * @param index       opaque pointer to index object
145  */
146 int faiss_Index_reset (FaissIndex* index);
147 
148 /** removes IDs from the index. Not supported by all indexes
149  * @param index       opaque pointer to index object
150  * @param nremove     output for the number of IDs removed
151  */
152 int faiss_Index_remove_ids (
153     FaissIndex* index,
154     const(FaissIDSelector)* sel,
155     size_t* n_removed);
156 
157 /** Reconstruct a stored vector (or an approximation if lossy coding)
158  *
159  * this function may not be defined for some indexes
160  * @param index       opaque pointer to index object
161  * @param key         id of the vector to reconstruct
162  * @param recons      reconstructed vector (size d)
163  */
164 int faiss_Index_reconstruct (const(FaissIndex)* index, idx_t key, float* recons);
165 
166 /** Reconstruct vectors i0 to i0 + ni - 1
167  *
168  * this function may not be defined for some indexes
169  * @param index       opaque pointer to index object
170  * @param recons      reconstructed vector (size ni * d)
171  */
172 int faiss_Index_reconstruct_n (
173     const(FaissIndex)* index,
174     idx_t i0,
175     idx_t ni,
176     float* recons);
177 
178 /** Computes a residual vector after indexing encoding.
179  *
180  * The residual vector is the difference between a vector and the
181  * reconstruction that can be decoded from its representation in
182  * the index. The residual can be used for multiple-stage indexing
183  * methods, like IndexIVF's methods.
184  *
185  * @param index       opaque pointer to index object
186  * @param x           input vector, size d
187  * @param residual    output residual vector, size d
188  * @param key         encoded index, as returned by search and assign
189  */
190 int faiss_Index_compute_residual (
191     const(FaissIndex)* index,
192     const(float)* x,
193     float* residual,
194     idx_t key);
195 
196 /** Computes a residual vector after indexing encoding.
197  *
198  * The residual vector is the difference between a vector and the
199  * reconstruction that can be decoded from its representation in
200  * the index. The residual can be used for multiple-stage indexing
201  * methods, like IndexIVF's methods.
202  *
203  * @param index       opaque pointer to index object
204  * @param n           number of vectors
205  * @param x           input vector, size (n x d)
206  * @param residuals    output residual vectors, size (n x d)
207  * @param keys         encoded index, as returned by search and assign
208  */
209 int faiss_Index_compute_residual_n (
210     const(FaissIndex)* index,
211     idx_t n,
212     const(float)* x,
213     float* residuals,
214     const(idx_t)* keys);
215