Couchbase Lite C++
Couchbase Lite C++ API
Loading...
Searching...
No Matches
VectorIndex.hh
Go to the documentation of this file.
1//
2// VectorIndex.hh
3//
4// Copyright (c) 2024 Couchbase, Inc All rights reserved.
5//
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17//
18
19
20#ifdef COUCHBASE_ENTERPRISE
21
22#pragma once
23#include "cbl++/Base.hh"
24#include "cbl++/Collection.hh"
25#include "cbl/CBLQueryIndexTypes.h"
26
28
29namespace cbl {
33
38 public:
44
51
56 static VectorEncoding productQuantizer(unsigned int subquantizers, unsigned int bits) {
58 }
59
62 VectorEncoding() = delete;
63
64 protected:
66
67 CBLVectorEncoding* ref() const {return _ref.get();}
68
69 private:
71 _ref = std::shared_ptr<CBLVectorEncoding>(ref, [](auto r) {
73 });
74 }
75
76 std::shared_ptr<CBLVectorEncoding> _ref;
77 };
78
83 public:
99 unsigned dimensions, unsigned centroids)
100 :_exprLang(expressionLanguage)
101 ,_expr(slice(expression))
102 ,_dimensions(dimensions)
103 ,_centroids(centroids)
104 { }
105
106 //-- Accessors:
107
109 QueryLanguage expressionLanguage() const {return _exprLang;}
110
112 slice expression() const {return _expr;}
113
115 unsigned dimensions() const {return _dimensions;}
116
118 unsigned centroids() const {return _centroids;}
119
130 bool isLazy = false;
131
134
137
151 unsigned minTrainingSize = 0;
152
156 unsigned maxTrainingSize = 0;
157
161 unsigned numProbes = 0;
162
163 protected:
165
166 private:
169 template <typename F>
170 decltype(auto) withCConfig(F&& f) const {
172 _exprLang, slice(_expr), _dimensions, _centroids
173 };
174 cConfig.isLazy = isLazy;
175 cConfig.encoding = encoding.ref();
176 cConfig.metric = metric;
179 cConfig.numProbes = numProbes;
180 return std::forward<F>(f)(cConfig);
181 }
182
183 QueryLanguage _exprLang;
184 std::string _expr;
185 unsigned _dimensions;
186 unsigned _centroids;
187 };
188
190 config.withCConfig([this, name](const CBLVectorIndexConfiguration& cConfig) {
191 CBLError error;
192 internal::check(
193 CBLCollection_CreateVectorIndex(ref(), slice(name), cConfig, &error),
194 error);
195 });
196 }
197}
198
200
201#endif
#define CBL_ASSUME_NONNULL_END
#define CBL_ASSUME_NONNULL_BEGIN
CBLCollection *_Nullable ref() const
Returns a pointer to the underlying C object (CBLCollection), or NULL if this is a null reference.
Definition Collection.hh:373
std::string name() const
The collection's name.
Definition Collection.hh:136
void createVectorIndex(std::string_view name, VectorIndexConfiguration config)
Creates a vector index in the collection.
Definition VectorIndex.hh:189
Vector encoding type to use in a VectorIndexConfiguration, for reducing the size of the stored vector...
Definition VectorIndex.hh:37
friend class VectorIndexConfiguration
Definition VectorIndex.hh:65
VectorEncoding()=delete
Deleted: a VectorEncoding must be constructed via one of the named factories (none,...
CBLVectorEncoding * ref() const
Definition VectorIndex.hh:67
static VectorEncoding scalarQuantizer(CBLScalarQuantizerType type)
Creates a Scalar Quantizer encoding type to use in VectorIndexConfiguration.
Definition VectorIndex.hh:48
static VectorEncoding productQuantizer(unsigned int subquantizers, unsigned int bits)
Creates a Product Quantizer encoding type to use in VectorIndexConfiguration.
Definition VectorIndex.hh:56
static VectorEncoding none()
Creates a no-encoding type to use in VectorIndexConfiguration; 4 bytes per dimension,...
Definition VectorIndex.hh:41
Configuration for creating a vector index, which enables searching documents by vector similarity.
Definition VectorIndex.hh:82
unsigned centroids() const
The number of centroids.
Definition VectorIndex.hh:118
VectorIndexConfiguration(CBLQueryLanguage expressionLanguage, std::string_view expression, unsigned dimensions, unsigned centroids)
Creates the VectorIndexConfiguration.
Definition VectorIndex.hh:98
unsigned dimensions() const
The number of vector dimensions.
Definition VectorIndex.hh:115
bool isLazy
The boolean flag indicating that index is lazy or not.
Definition VectorIndex.hh:130
DistanceMetric metric
Distance Metric type.
Definition VectorIndex.hh:136
VectorEncoding encoding
Vector encoding type.
Definition VectorIndex.hh:133
QueryLanguage expressionLanguage() const
The language used in the expressions.
Definition VectorIndex.hh:109
unsigned minTrainingSize
The minimum number of vectors for training the index.
Definition VectorIndex.hh:151
slice expression() const
The expression.
Definition VectorIndex.hh:112
unsigned numProbes
The number of centroids that will be scanned during a query.
Definition VectorIndex.hh:161
unsigned maxTrainingSize
The maximum number of vectors used for training the index.
Definition VectorIndex.hh:156
friend Collection
Definition VectorIndex.hh:164
bool CBLCollection_CreateVectorIndex(CBLCollection *collection, FLString name, CBLVectorIndexConfiguration config, CBLError *_cbl_nullable outError)
_cbl_warn_unused CBLVectorEncoding * CBLVectorEncoding_CreateNone(void)
void CBLVectorEncoding_Free(CBLVectorEncoding *_cbl_nullable)
struct CBLVectorEncoding CBLVectorEncoding
_cbl_warn_unused CBLVectorEncoding * CBLVectorEncoding_CreateScalarQuantizer(CBLScalarQuantizerType type)
_cbl_warn_unused CBLVectorEncoding * CBLVectorEncoding_CreateProductQuantizer(unsigned subquantizers, unsigned bits)
CBLScalarQuantizerType
CBLDistanceMetric
kCBLSQ8
kCBLDistanceMetricEuclideanSquared
CBLQueryLanguage
Definition Base.hh:46
fleece::slice slice
Convenience alias for fleece::slice, a non-owning view of a byte range.
Definition Base.hh:49
CBLDistanceMetric DistanceMetric
The distance metric used by a vector index to measure the similarity of vectors.
Definition VectorIndex.hh:32
CBLQueryLanguage QueryLanguage
Definition Base.hh:53
CBLDistanceMetric metric
CBLVectorEncoding * encoding