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:

    • vector_field_name (String)

      the document field that contains the vector.

    • vector_query (Array<Float>)

      the vector query.

  • #initialize(vector_field_name, base64_vector_query) ⇒ VectorQuery

    Parameters:

    • vector_field_name (String)

      the document field that contains the vector.

    • base64_vector_query (String)

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

Yield Parameters:



1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
# File 'lib/couchbase/search_options.rb', line 1087

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:

  • (Float, nil)


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

def boost
  @boost
end

#num_candidatesInteger?

Returns:

  • (Integer, nil)


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

def num_candidates
  @num_candidates
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.



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

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

  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_json(*args) ⇒ Object

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.



1126
1127
1128
# File 'lib/couchbase/search_options.rb', line 1126

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