Skip to content

Commit e9930cc

Browse files
authored
RCBC-489: Support for base64 encoded vector types (#146)
1 parent 6ec4ada commit e9930cc

File tree

3 files changed

+25
-5
lines changed

3 files changed

+25
-5
lines changed

ext/couchbase

lib/couchbase/search_options.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1071,13 +1071,23 @@ class VectorQuery
10711071

10721072
# Constructs a +VectorQuery+ instance
10731073
#
1074-
# @param [String] vector_field_name the document field that contains the vector.
1075-
# @param [Array<Float>] vector_query the vector query to run.
1074+
# @overload initialize(vector_field_name, vector_query)
1075+
# @param [String] vector_field_name the document field that contains the vector.
1076+
# @param [Array<Float>] vector_query the vector query.
10761077
#
1077-
# @yieldparam [MatchPhraseQuery] self
1078+
# @overload initialize(vector_field_name, base64_vector_query)
1079+
# @param [String] vector_field_name the document field that contains the vector.
1080+
# @param [String] base64_vector_query the vector query represented as a base64-encoded sequence of little-endian IEEE 754 floats.
1081+
#
1082+
# @yieldparam [VectorQuery] self
10781083
def initialize(vector_field_name, vector_query)
10791084
@vector_field_name = vector_field_name
1080-
@vector_query = vector_query
1085+
1086+
if vector_query.respond_to?(:to_str)
1087+
@base64_vector_query = vector_query.to_str
1088+
else
1089+
@vector_query = vector_query
1090+
end
10811091

10821092
yield self if block_given?
10831093
end
@@ -1092,6 +1102,7 @@ def to_h
10921102
{
10931103
field: @vector_field_name,
10941104
vector: @vector_query,
1105+
vector_base64: @base64_vector_query,
10951106
k: num_candidates || 3,
10961107
boost: boost,
10971108
}.compact

test/search_test.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,15 @@ def test_vector_search_query_defaults_to_match_none
237237
assert_equal SearchQuery.match_none.to_json, enc_query
238238
end
239239

240+
def test_vector_search_query_base64
241+
base64_query = "aOeYBEXJ4kI="
242+
enc_vector_query = VectorQuery.new("foo", base64_query).to_h
243+
244+
refute enc_vector_query.key?(:vector)
245+
assert enc_vector_query.key?(:vector_base64)
246+
assert_equal enc_vector_query[:vector_base64], base64_query
247+
end
248+
240249
def test_vector_search_not_supported
241250
skip("#{name}: Server supports vector search") if !env.protostellar? && env.server_version.supports_vector_search?
242251

0 commit comments

Comments
 (0)