Skip to content

Commit 2a8cb6e

Browse files
committed
GOCBC-1716: Add support for vector search fts like pre-filters
Motivation ----------- The server has now added support for fts like pre-filters to vector search. We need to expose this. Changes -------- Add Prefilter function to vector query options and pass down into JSON when specified. Change-Id: I6a1898ece90d0f585ead0faa8fd37633f8f4d041 Reviewed-on: https://review.couchbase.org/c/gocb/+/230121 Tested-by: Build Bot <[email protected]> Reviewed-by: Dimitris Christodoulou <[email protected]>
1 parent 323fc96 commit 2a8cb6e

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

vector/query.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package vector
33
import (
44
"encoding/json"
55
"errors"
6+
"github.com/couchbase/gocb/v2/search"
67
)
78

89
// Query specifies a vector Query.
@@ -13,6 +14,7 @@ type Query struct {
1314

1415
numCandidates *uint32
1516
boost *float32
17+
prefilter search.Query
1618
}
1719

1820
// NewQuery constructs a new vector Query.
@@ -44,6 +46,12 @@ func (q *Query) Boost(boost float32) *Query {
4446
return q
4547
}
4648

49+
// Prefilter specifies a search.Query to filter by before executing the query.
50+
func (q *Query) Prefilter(query search.Query) *Query {
51+
q.prefilter = &query
52+
return q
53+
}
54+
4755
// InternalQuery is used for internal functionality.
4856
// Internal: This should never be used and is not supported.
4957
type InternalQuery struct {
@@ -53,6 +61,7 @@ type InternalQuery struct {
5361

5462
NumCandidates *uint32
5563
Boost *float32
64+
Prefilter search.Query
5665
}
5766

5867
// Internal is used for internal functionality.
@@ -64,6 +73,7 @@ func (q *Query) Internal() InternalQuery {
6473
Base64Vector: q.base64Vector,
6574
NumCandidates: q.numCandidates,
6675
Boost: q.boost,
76+
Prefilter: q.prefilter,
6777
}
6878
}
6979

@@ -85,17 +95,19 @@ func (q InternalQuery) Validate() error {
8595
// MarshalJSON marshal's this query to JSON for the search REST API.
8696
func (q InternalQuery) MarshalJSON() ([]byte, error) {
8797
outStruct := &struct {
88-
Field string `json:"field"`
89-
Vector []float32 `json:"vector,omitempty"`
90-
Base64Vector string `json:"vector_base64,omitempty"`
91-
NumCandidates *uint32 `json:"k,omitempty"`
92-
Boost *float32 `json:"boost,omitempty"`
98+
Field string `json:"field"`
99+
Vector []float32 `json:"vector,omitempty"`
100+
Base64Vector string `json:"vector_base64,omitempty"`
101+
NumCandidates *uint32 `json:"k,omitempty"`
102+
Boost *float32 `json:"boost,omitempty"`
103+
PreFilter search.Query `json:"filter,omitempty"`
93104
}{
94105
Field: q.Field,
95106
Vector: q.Vector,
96107
Base64Vector: q.Base64Vector,
97108
NumCandidates: q.NumCandidates,
98109
Boost: q.Boost,
110+
PreFilter: q.Prefilter,
99111
}
100112

101113
return json.Marshal(outStruct)

0 commit comments

Comments
 (0)