java.lang.Object
org.springframework.data.mongodb.core.mapping.MongoVector
All Implemented Interfaces:
Vector

public class MongoVector extends Object implements Vector
MongoDB-specific extension to Vector based on Mongo's BinaryVector. Note that only float32 and int8 variants can be represented as floating-point numbers. int1 throws UnsupportedOperationException when calling toFloatArray() and toDoubleArray().
Since:
4.5
Author:
Mark Paluch
  • Method Details

    • of

      public static MongoVector of(org.bson.BinaryVector v)
      Creates a new binary MongoVector using the given BinaryVector.
      Parameters:
      v - binary vector representation.
      Returns:
      the MongoVector wrapping BinaryVector.
    • ofInt8

      public static MongoVector ofInt8(byte[] data)
      Creates a new binary MongoVector using the given data.

      A BinaryVector.DataType.INT8 vector is a vector of 8-bit signed integers where each byte in the vector represents an element of a vector, with values in the range [-128, 127].

      NOTE: The byte array is not copied; changes to the provided array will be referenced in the created MongoVector instance.

      Parameters:
      data - the byte array representing the BinaryVector.DataType.INT8 vector data.
      Returns:
      the MongoVector containing the given vector values to be represented as binary int8.
    • ofFloat

      public static MongoVector ofFloat(float... data)
      Creates a new binary MongoVector using the given data.

      A BinaryVector.DataType.FLOAT32 vector is a vector of floating-point numbers, where each element in the vector is a float.

      NOTE: The float array is not copied; changes to the provided array will be referenced in the created MongoVector instance.

      Parameters:
      data - the float array representing the BinaryVector.DataType.FLOAT32 vector data.
      Returns:
      the MongoVector containing the given vector values to be represented as binary float32.
    • fromFloat

      public static MongoVector fromFloat(Vector v)
      Creates a new binary MongoVector from the given Vector.

      A BinaryVector.DataType.FLOAT32 vector is a vector of floating-point numbers, where each element in the vector is a float. The given Vector must be able to return a float array.

      NOTE: The float array is not copied; changes to the provided array will be referenced in the created MongoVector instance.

      Parameters:
      v - the
      Returns:
      the MongoVector using vector values from the given Vector to be represented as binary float32.
    • getType

      public Class<? extends Number> getType()
      Specified by:
      getType in interface Vector
    • getSource

      public org.bson.BinaryVector getSource()
      Specified by:
      getSource in interface Vector
    • size

      public int size()
      Specified by:
      size in interface Vector
    • toFloatArray

      public float[] toFloatArray()
      Specified by:
      toFloatArray in interface Vector
      Throws:
      UnsupportedOperationException - if the underlying data type is int1 PackedBitBinaryVector.
    • toDoubleArray

      public double[] toDoubleArray()
      Specified by:
      toDoubleArray in interface Vector
      Throws:
      UnsupportedOperationException - if the underlying data type is int1 PackedBitBinaryVector.
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object