14
14
#include " Base.hh"
15
15
#include < optional>
16
16
#include < string>
17
+ #include < variant>
17
18
18
19
namespace fleece ::impl {
19
20
class Array ;
@@ -28,22 +29,46 @@ namespace litecore {
28
29
};
29
30
30
31
struct IndexSpec {
32
+ // / The types of indexes.
31
33
enum Type {
32
34
kValue , // /< Regular index of property value
33
- kFullText , // /< Full-text index, for MATCH queries
35
+ kFullText , // /< Full-text index, for MATCH queries. Uses IndexSpec::FTSOptions.
34
36
kArray , // /< Index of array values, for UNNEST queries
35
37
kPredictive , // /< Index of prediction results
38
+ kVector , // /< Index of ML vector similarity. Uses IndexSpec::VectorOptions.
36
39
};
37
40
38
- struct Options {
39
- const char * language; // /< NULL or an ISO language code ("en", etc)
40
- bool ignoreDiacritics; // /< True to strip diacritical marks/accents from letters
41
- bool disableStemming; // /< Disables stemming
42
- const char * stopWords; // /< NULL for default, or comma-delimited string, or empty
41
+ // / Options for a full-text index.
42
+ struct FTSOptions {
43
+ const char * language{}; // /< NULL or an ISO language code ("en", etc)
44
+ bool ignoreDiacritics{}; // /< True to strip diacritical marks/accents from letters
45
+ bool disableStemming{}; // /< Disables stemming
46
+ const char * stopWords{}; // /< NULL for default, or comma-delimited string, or empty
43
47
};
44
48
49
+ // / Options for a vector index.
50
+ struct VectorOptions {
51
+ enum Encoding {
52
+ DefaultEncoding, // /< Use default encoding, which is currently SQ
53
+ NoEncoding, // /< No encoding; 4 bytes per dimension, no data loss
54
+ SQEncoding, // /< Scalar Quantizer; 1 byte per dimension (recommended)
55
+ }; // Note: values must match C4VectorEncoding in c4IndexTypes.h
56
+
57
+ unsigned numCentroids{2048 }; // /< Number of centroids/buckets to divide the index into
58
+ Encoding encoding{DefaultEncoding}; // /< Vector encoding/compression
59
+ };
60
+
61
+ // / Index options. If not empty (the first state), must match the index type.
62
+ using Options = std::variant<std::monostate, FTSOptions, VectorOptions>;
63
+
64
+ // / Constructs an index spec.
65
+ // / @param name_ Name of the index (must be unique in its collection.)
66
+ // / @param type_ Type of the index.
67
+ // / @param expression_ The value(s) to be indexed.
68
+ // / @param queryLanguage Language used for `expression_`; either JSON or N1QL.
69
+ // / @param options_ Options; if given, its type must match the index type.
45
70
IndexSpec (std::string name_, Type type_, alloc_slice expression_,
46
- QueryLanguage queryLanguage = QueryLanguage::kJSON , const Options* opt = nullptr );
71
+ QueryLanguage queryLanguage = QueryLanguage::kJSON , Options options_ = {} );
47
72
48
73
IndexSpec (const IndexSpec&) = delete ;
49
74
IndexSpec (IndexSpec&&);
@@ -53,23 +78,25 @@ namespace litecore {
53
78
void validateName () const ;
54
79
55
80
const char * typeName () const {
56
- static const char * kTypeName [] = {" value" , " full-text" , " array" , " predictive" };
81
+ static const char * kTypeName [] = {" value" , " full-text" , " array" , " predictive" , " vector " };
57
82
return kTypeName [type];
58
83
}
59
84
60
- const Options* optionsPtr () const { return options ? &*options : nullptr ; }
85
+ const FTSOptions* ftsOptions () const { return std::get_if<FTSOptions>(&options); }
86
+
87
+ const VectorOptions* vectorOptions () const { return std::get_if<VectorOptions>(&options); }
61
88
62
89
/* * The required WHAT clause: the list of expressions to index */
63
90
const fleece::impl::Array* NONNULL what () const ;
64
91
65
92
/* * The optional WHERE clause: the condition for a partial index */
66
93
const fleece::impl::Array* where () const ;
67
94
68
- std::string const name;
69
- Type const type;
70
- alloc_slice const expression;
71
- QueryLanguage queryLanguage;
72
- std::optional< Options> const options;
95
+ std::string const name; // /< Name of index
96
+ Type const type; // /< Type of index
97
+ alloc_slice const expression; // /< The query expression
98
+ QueryLanguage queryLanguage; // /< Is expression JSON or N1QL?
99
+ Options const options; // /< Options for FTS and vector indexes
73
100
74
101
private:
75
102
fleece::impl::Doc* doc () const ;
0 commit comments