Class: Couchbase::VectorQuery

Inherits:
Object
  • Object
show all
Defined in:
lib/couchbase/search_options.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(vector_field_name, vector_query) ⇒ VectorQuery #initialize(vector_field_name, base64_vector_query) ⇒ VectorQuery

Constructs a VectorQuery instance

Overloads:

  • #initialize(vector_field_name, vector_query) ⇒ VectorQuery

    Parameters:

    • the document field that contains the vector.

    • the vector query.

  • #initialize(vector_field_name, base64_vector_query) ⇒ VectorQuery

    Parameters:

    • the document field that contains the vector.

    • the vector query represented as a base64-encoded sequence of little-endian IEEE 754 floats.

Yield Parameters:



1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
# File 'lib/couchbase/search_options.rb', line 1090

def initialize(vector_field_name, vector_query)
  @vector_field_name = vector_field_name

  if vector_query.respond_to?(:to_str)
    @base64_vector_query = vector_query.to_str
  else
    @vector_query = vector_query
  end

  yield self if block_given?
end

Instance Attribute Details

#boostFloat?

Returns:



1074
1075
1076
# File 'lib/couchbase/search_options.rb', line 1074

def boost
  @boost
end

#num_candidatesInteger?

Returns:



1071
1072
1073
# File 'lib/couchbase/search_options.rb', line 1071

def num_candidates
  @num_candidates
end

#prefilterSearchQuery?

Returns:



1077
1078
1079
# File 'lib/couchbase/search_options.rb', line 1077

def prefilter
  @prefilter
end

Instance Method Details

#to_hObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Raises:

API:

  • private



1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
# File 'lib/couchbase/search_options.rb', line 1103

def to_h
  if !num_candidates.nil? && num_candidates < 1
    raise Error::InvalidArgument,
          "Number of candidates must be at least 1, #{num_candidates} given"
  end

  h = {
    field: @vector_field_name,
    vector: @vector_query,
    vector_base64: @base64_vector_query,
    k: num_candidates || 3,
    boost: boost,
  }.compact

  h[:filter] = prefilter.to_h unless prefilter.nil?

  raise Error::InvalidArgument, "The vector cannot be nil" if !h.include?(:vector) && !h.include?(:vector_base64)
  raise Error::InvalidArgument, "The vector query cannot be an empty array" if h.include?(:vector) && h[:vector].empty?

  if h.include?(:vector_base64) && h[:vector_base64].empty?
    raise Error::InvalidArgument,
          "The base64-encoded vector query cannot be empty"
  end

  h
end

#to_jsonObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

API:

  • private



1131
1132
1133
# File 'lib/couchbase/search_options.rb', line 1131

def to_json(*)
  to_h.to_json(*)
end