10 #include <string_view>
12 #include <unordered_map>
15 #include <nlohmann/json.hpp>
19 using json = nlohmann::json;
21 namespace pinecone::types
37 NLOHMANN_JSON_SERIALIZE_ENUM(
binary_operator, {{binary_operator::unknown,
nullptr},
38 {binary_operator::eq,
"$eq"},
39 {binary_operator::ne,
"$ne"},
40 {binary_operator::gt,
"$gt"},
41 {binary_operator::gte,
"$gte"},
42 {binary_operator::lt,
"$lt"},
43 {binary_operator::lte,
"$lte"}})
48 enum class array_operator {
55 NLOHMANN_JSON_SERIALIZE_ENUM(array_operator, {{array_operator::unknown,
nullptr},
56 {array_operator::in,
"$in"},
57 {array_operator::nin,
"$nin"}})
62 enum class combination_operator {
69 NLOHMANN_JSON_SERIALIZE_ENUM(combination_operator, {{combination_operator::unknown,
nullptr},
70 {combination_operator::and_,
"$and"},
71 {combination_operator::or_,
"$or"}})
76 struct metadata_value {
77 using value_type = std::variant<bool, int64_t, double, std::string>;
79 constexpr metadata_value(
char const* value) noexcept : _var(value) {}
81 metadata_value(
std::string value) noexcept : _var(value) {}
83 constexpr metadata_value(
bool value) noexcept : _var(value) {}
85 constexpr metadata_value(int64_t value) noexcept : _var(value) {}
87 constexpr metadata_value(
double value) noexcept : _var(value) {}
89 [[nodiscard]]
auto var() const noexcept -> value_type const& {
return _var; }
91 friend void to_json(nlohmann ::json& nlohmann_json_j,
const metadata_value& nlohmann_json_t)
93 std::visit([&nlohmann_json_j](
auto const& v) { nlohmann_json_j = v; }, nlohmann_json_t._var);
119 [[nodiscard]]
auto values() const noexcept
120 ->
std::unordered_map<
std::
string, metadata_value> const&
125 friend void to_json(nlohmann ::json& nlohmann_json_j,
const metadata& nlohmann_json_t)
127 for (
auto const& [key, value] : nlohmann_json_t._values) {
128 std::visit([&, k = key](
auto const& v) { nlohmann_json_j[k] = v; }, value.var());
132 friend void from_json(
const nlohmann ::json& nlohmann_json_j, metadata& nlohmann_json_t)
135 for (
auto const& [key, value] : nlohmann_json_j.items()) {
136 if (value.is_boolean()) {
137 values.emplace(key, value.get<
bool>());
138 }
else if (value.is_number_integer()) {
139 values.emplace(key, value.get<int64_t>());
140 }
else if (value.is_number_float()) {
141 values.emplace(key, value.get<
double>());
142 }
else if (value.is_string()) {
162 template <
typename Derived>
164 friend void to_json(nlohmann ::json& nlohmann_json_j,
const filter_base& nlohmann_json_t)
166 nlohmann_json_j[
"filter"] = json::object();
167 to_json(nlohmann_json_j[
"filter"],
static_cast<Derived const&
>(nlohmann_json_t));
176 friend void to_json(nlohmann ::json& nlohmann_json_j,
const binary_filter& nlohmann_json_t)
178 json j = nlohmann_json_t._op;
179 nlohmann_json_j[nlohmann_json_t._key][j] = nlohmann_json_t._value;
190 metadata_value _value;
199 template <
typename iter>
201 friend void to_json(nlohmann ::json& nlohmann_json_j,
const array_filter& nlohmann_json_t)
203 json j = nlohmann_json_t._op;
204 nlohmann_json_j[nlohmann_json_t._key][j] = json::array();
205 auto& arr = nlohmann_json_j[nlohmann_json_t._key][j];
206 for (
auto const& v : nlohmann_json_t._values) {
222 template <
typename filter>
223 auto serialize_expand(json& arr, filter
const& f) ->
void
225 to_json(arr.emplace_back(), f);
228 template <
typename filter,
typename... filters>
229 auto serialize_expand(json& arr, filter
const& f, filters
const&... fs) ->
void
231 serialize_expand(arr, f);
232 serialize_expand(arr, fs...);
241 template <
typename... ts>
243 friend void to_json(nlohmann ::json& nlohmann_json_j,
const combination_filter& nlohmann_json_t)
245 json j = nlohmann_json_t._op;
246 nlohmann_json_j[j] = json::array();
247 auto& arr = nlohmann_json_j[j];
248 std::apply([&arr](
auto const&... filter) ->
void { serialize_expand(arr, filter...); },
249 nlohmann_json_t._filters);
254 : _op(op), _filters(
std::move(filters)...)
259 combination_operator _op;
267 friend void to_json(nlohmann ::json& nlohmann_json_j,
const no_filter& )
269 nlohmann_json_j = json::object({});
Models the possibility of failure for Pinecone operations.
Array filters test a single metadata value against multiple operands.
Binary filters are simple predicates; they compare a single metadata value to a provided operand.
Combination filters apply boolean logic to the other filter types.
Common operations shared for all filter types.
No filter applied; always returns true.