forked from DataIntelligenceCrew/go-faiss
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex_ivf.go
61 lines (52 loc) · 1.35 KB
/
index_ivf.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package faiss
/*
#include <faiss/c_api/IndexIVFFlat_c.h>
#include <faiss/c_api/MetaIndexes_c.h>
#include <faiss/c_api/Index_c.h>
#include <faiss/c_api/IndexIVF_c.h>
#include <faiss/c_api/IndexIVF_c_ex.h>
*/
import "C"
import (
"fmt"
)
func (idx *IndexImpl) SetDirectMap(mapType int) (err error) {
ivfPtr := C.faiss_IndexIVF_cast(idx.cPtr())
if ivfPtr == nil {
return fmt.Errorf("index is not of ivf type")
}
if c := C.faiss_IndexIVF_set_direct_map(
ivfPtr,
C.int(mapType),
); c != 0 {
err = getLastError()
}
return err
}
func (idx *IndexImpl) GetSubIndex() (*IndexImpl, error) {
ptr := C.faiss_IndexIDMap2_cast(idx.cPtr())
if ptr == nil {
return nil, fmt.Errorf("index is not a id map")
}
subIdx := C.faiss_IndexIDMap2_sub_index(ptr)
if subIdx == nil {
return nil, fmt.Errorf("couldn't retrieve the sub index")
}
return &IndexImpl{&faissIndex{subIdx}}, nil
}
// pass nprobe to be set as index time option for IVF indexes only.
// varying nprobe impacts recall but with an increase in latency.
func (idx *IndexImpl) SetNProbe(nprobe int32) {
ivfPtr := C.faiss_IndexIVF_cast(idx.cPtr())
if ivfPtr == nil {
return
}
C.faiss_IndexIVF_set_nprobe(ivfPtr, C.size_t(nprobe))
}
func (idx *IndexImpl) GetNProbe() int32 {
ivfPtr := C.faiss_IndexIVF_cast(idx.cPtr())
if ivfPtr == nil {
return 0
}
return int32(C.faiss_IndexIVF_nprobe(ivfPtr))
}