Skip to content

Commit 5096c7a

Browse files
authored
Small backlog of changes (#102)
* filter $not fix * update options gen * update embdding/reranking provider related stuff * add SourceModel to VectorOptions * a * bump version
1 parent 32105d8 commit 5096c7a

9 files changed

Lines changed: 181 additions & 121 deletions

File tree

astra/filter/filter.go

Lines changed: 30 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -69,26 +69,26 @@ func (F) isFilter() {}
6969
// }
7070
type A []any
7171

72-
// FilterOperator represents the operation type (Eq, Gt, etc.)
73-
type FilterOperator string
72+
// Operator represents the operation type (Eq, Gt, etc.)
73+
type Operator string
7474

7575
const (
76-
OpAnd FilterOperator = "$and"
77-
OpOr FilterOperator = "$or"
78-
OpNot FilterOperator = "$not"
79-
OpGreaterThan FilterOperator = "$gt"
80-
OpGreaterThanEqual FilterOperator = "$gte"
81-
OpLessThan FilterOperator = "$lt"
82-
OpLessThanEqual FilterOperator = "$lte"
83-
OpEqual FilterOperator = "$eq"
84-
OpNotEqual FilterOperator = "$ne"
85-
OpIn FilterOperator = "$in"
86-
OpNotIn FilterOperator = "$nin"
87-
OpExists FilterOperator = "$exists"
88-
OpAll FilterOperator = "$all"
89-
OpSize FilterOperator = "$size"
90-
OpLexical FilterOperator = "$lexical"
91-
OpMatch FilterOperator = "$match"
76+
OpAnd Operator = "$and"
77+
OpOr Operator = "$or"
78+
OpNot Operator = "$not"
79+
OpGreaterThan Operator = "$gt"
80+
OpGreaterThanEqual Operator = "$gte"
81+
OpLessThan Operator = "$lt"
82+
OpLessThanEqual Operator = "$lte"
83+
OpEqual Operator = "$eq"
84+
OpNotEqual Operator = "$ne"
85+
OpIn Operator = "$in"
86+
OpNotIn Operator = "$nin"
87+
OpExists Operator = "$exists"
88+
OpAll Operator = "$all"
89+
OpSize Operator = "$size"
90+
OpLexical Operator = "$lexical"
91+
OpMatch Operator = "$match"
9292
)
9393

9494
// Filter represents a collection of filters. Compose filters with
@@ -106,33 +106,33 @@ const (
106106
// )
107107
type Filter struct {
108108
// The operator. Such as "$or"
109-
op FilterOperator
109+
op Operator
110110
// The field to perform an operation on. Example: "_id".
111111
field string
112112
// The value to filter for based on `op`.
113113
value any
114114
// Child filters. Should never be populated if field/value are also populated.
115-
children []Filter
115+
children any
116116
}
117117

118118
// Satisfy interface to allow Filter to be used as a filter.
119119
func (Filter) isFilter() {}
120120

121121
// Construct a field filter operator. Used to reduce boilerplate.
122-
func fieldOp(op FilterOperator, field string, value any) Filter {
122+
func fieldOp(op Operator, field string, value any) Filter {
123123
return Filter{op: op, field: field, value: value}
124124
}
125125

126126
// Construct a slice filter operator. Used to reduce boilerplate.
127-
func sliceOp(op FilterOperator, field string, vals []any) Filter {
127+
func sliceOp(op Operator, field string, vals []any) Filter {
128128
return Filter{op: op, field: field, value: vals}
129129
}
130130

131131
func (f Filter) MarshalAstraRaw(ctx serdes.EncodeCtx, dst []byte) ([]byte, error) {
132-
if len(f.children) > 0 {
132+
if f.children != nil {
133133
// We have child commands. Create a map and marshal them like this:
134134
// "$or": [...]
135-
filters := make(map[FilterOperator][]Filter)
135+
filters := make(map[Operator]any)
136136
filters[f.op] = f.children
137137
return serdes.SerializeInto(filters, ctx.Target, dst, ctx.Flags)
138138
}
@@ -146,8 +146,8 @@ func (f Filter) MarshalAstraRaw(ctx serdes.EncodeCtx, dst []byte) ([]byte, error
146146
}
147147
// We have another op. Marshal it into something like:
148148
// "number_of_pages": { "$lt": 300 }
149-
filters := make(map[string]map[FilterOperator]any)
150-
filters[f.field] = map[FilterOperator]any{f.op: f.value}
149+
filters := make(map[string]map[Operator]any)
150+
filters[f.field] = map[Operator]any{f.op: f.value}
151151
return serdes.SerializeInto(filters, ctx.Target, dst, ctx.Flags)
152152
}
153153
return append(dst, "null"...), nil
@@ -182,7 +182,10 @@ func Or(children ...Filter) Filter {
182182
}
183183

184184
func Not(child Filter) Filter {
185-
return Filter{op: OpNot, children: []Filter{child}}
185+
return Filter{
186+
op: OpNot,
187+
children: child,
188+
}
186189
}
187190

188191
// LexicalMatch creates a filter that matches documents against the collection's

astra/filter/filter_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,18 @@ func TestOrSingleChild(t *testing.T) {
129129
}
130130
}
131131

132+
func TestNotSingleChild(t *testing.T) {
133+
f := filter.Not(filter.Eq("x", 1))
134+
got, err := serdes.Serialize(f, serdes.TargetCollection)
135+
if err != nil {
136+
t.Fatal(err)
137+
}
138+
expected := `{"$not":{"x":1}}`
139+
if string(got) != expected {
140+
notExpected(t, expected, string(got))
141+
}
142+
}
143+
132144
func TestEmptyFilterMarshal(t *testing.T) {
133145
f := filter.Filter{}
134146
got, err := serdes.Serialize(f, serdes.TargetCollection)

astra/internal/constants/version.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package constants
1616

1717
// LibVersion is the version of the library.
18-
const LibVersion = "2.0.0-preview.3"
18+
const LibVersion = "2.0.0-preview.4"
1919

2020
// LibName is the name of the library.
2121
const LibName = "astra-db-go"

0 commit comments

Comments
 (0)