package filter

import "github.com/datastax/astra-db-go/v2/astra/filter"

Package filter defines filtering options for Astra DB queries.

Index

Types

type A

type A []any

A represents a slice/array of filters to be applied to an Astra DB query. Use this in conjunction with F if you want to pass filters as they appear in JSON data.

Example:

filters := filter.F{
	"$and": filter.A{
		filter.F{"$or": filter.A{
			filter.F{"is_checked_out": false},
			filter.F{"number_of_pages": filter.F{"$lt": 300}},
		}},
		filter.F{"$or": filter.A{
			filter.F{"genres": filter.F{"$in": filter.A{"Fantasy", "Romance"}}},
			filter.F{"publication_year": filter.F{"$gte": 2002}},
		}},
	},
}

type CollFilter

type CollFilter struct{}

CollFilter is a namespace for collection-specific filter operators. Obtain one via Coll. The returned Filter values compose with And, Or, and [Not] like any other filter.

Example:

filter.And(
	filter.Eq("genre", "fantasy"),
	filter.Coll.Size("tags", 3),
	filter.Coll.LexicalMatch("dragon"),
)
var Coll CollFilter

Coll is a CollFilter that provides access to collection-specific filter operators

func (CollFilter) Exists
func (CollFilter) Exists(key string, val bool) Filter

Exists matches documents that have the specified field, even if the field value is null. Collection-only.

func (CollFilter) LexicalMatch
func (CollFilter) LexicalMatch(val string) Filter

LexicalMatch creates a filter that matches documents against the collection's reserved $lexical field. Only available for collections with lexical enabled.

func (CollFilter) Not
func (CollFilter) Not(child Filterable) Filter

Not negates the given filter. All Filterable types (Filter, F, A) are accepted. Collection-only.

func (CollFilter) Size
func (CollFilter) Size(key string, val int) Filter

Size filters documents where the array field has the given number of elements. Collection-only.

type F

type F map[string]any

F represents a map of filters to be applied to an Astra DB query. Use this in conjunction with A if you want to pass filters as they appear in JSON data.

Example:

filters := filter.F{
	"$and": filter.A{
		filter.F{"$or": filter.A{
			filter.F{"is_checked_out": false},
			filter.F{"number_of_pages": filter.F{"$lt": 300}},
		}},
		filter.F{"$or": filter.A{
			filter.F{"genres": filter.F{"$in": filter.A{"Fantasy", "Romance"}}},
			filter.F{"publication_year": filter.F{"$gte": 2002}},
		}},
	},
}

See [FilterOperator] for available operators.

type Filter

type Filter struct {
	// contains filtered or unexported fields
}

Filter represents a filter clause. Compose filters with package-level functions like Eq, Gt, And, etc. Example:

filters := filter.And(
	filter.Or(
		filter.Eq("is_checked_out", false),
		filter.Lt("number_of_pages", 300),
	),
	filter.Or(
		filter.In("genres", "Fantasy", "Romance"),
		filter.Gte("publication_year", 2002),
	),
)

For collection-specific operators (e.g. CollFilter.Size, [CollFilter.All], CollFilter.LexicalMatch), use Coll to access them. For table-specific operators, use Table.

func All
func All(key string, vals ...any) Filter
func And
func And(children ...Filterable) Filter

And combines the given filters with a logical AND. All Filterable types (Filter, F, A) are accepted.

func Eq
func Eq(key string, val any) Filter
func Gt
func Gt(key string, val any) Filter
func Gte
func Gte(key string, val any) Filter
func In
func In(key string, vals ...any) Filter
func Lt
func Lt(key string, val any) Filter
func Lte
func Lte(key string, val any) Filter
func Ne
func Ne(key string, val any) Filter
func Nin
func Nin(key string, vals ...any) Filter
func Or
func Or(children ...Filterable) Filter

Or combines the given filters with a logical OR. All Filterable types (Filter, F, A) are accepted.

func (Filter) MarshalAstraRaw
func (f Filter) MarshalAstraRaw(ctx serdes.EncodeCtx, dst []byte) ([]byte, error)

type Filterable

type Filterable interface {
	// contains filtered or unexported methods
}

Filterable is implemented by types that can be used as query filters.

type Operator

type Operator string

Operator represents the operation type (Eq, Gt, etc.)

const (
	OpAnd              Operator = "$and"
	OpOr               Operator = "$or"
	OpNot              Operator = "$not"
	OpGreaterThan      Operator = "$gt"
	OpGreaterThanEqual Operator = "$gte"
	OpLessThan         Operator = "$lt"
	OpLessThanEqual    Operator = "$lte"
	OpEqual            Operator = "$eq"
	OpNotEqual         Operator = "$ne"
	OpIn               Operator = "$in"
	OpNotIn            Operator = "$nin"
	OpExists           Operator = "$exists"
	OpAll              Operator = "$all"
	OpKeys             Operator = "$keys"
	OpValues           Operator = "$values"
	OpSize             Operator = "$size"
	OpLexical          Operator = "$lexical"
	OpMatch            Operator = "$match"
)

type TableFilter

type TableFilter struct{}

TableFilter is a namespace for table-specific filter operators. Obtain one via Table. The returned Filter values compose with And, Or, and [Not] like any other filter.

Example:

filter.And(
	filter.Eq("genre", "fantasy"),
	filter.Table.Keys(filter.In("metadata", "Language", "Edition")),
	filter.Table.LexicalMatch("dragon"),
)
var Table TableFilter

Table is a TableFilter that provides access to table-specific filter operators.

func (TableFilter) Keys
func (TableFilter) Keys(f Filter) Filter

Keys works with In, All, and Nin to filter on map columns Table-only.

func (TableFilter) LexicalMatch
func (TableFilter) LexicalMatch(key string, val string) Filter

LexicalMatch creates a filter that matches documents against a table column with a text index associated.

func (TableFilter) Values
func (TableFilter) Values(f Filter) Filter

Values works with In, All, and Nin to filter on map columns Table-only.