$vector in collections (Go)
$vector is a reserved field in documents.
It stores a vector embedding that is used for vector search
Only vector-enabled collections support the $vector field.
For more information, see Create a collection that can store vector embeddings.
Insert and update a document’s $vector field
When you insert or update a document, you can use the $vector field to store a vector embedding for the document.
For an example, see Insert documents with vector embeddings.
All vector embeddings in a collection should be generated by the same model with the same dimensions. Using mismatched embeddings produces unreliable and incorrect results in vector searches. The Data API only checks that the dimensions are the same; it doesn’t check whether the embeddings are from different models.
Return the $vector field
By default, the Data API excludes the $vector field from returned documents.
If you want the Data API to return the $vector field, you must use a projection to explicitly include the $vector field in the response.
Binary encoding of vector embeddings
When inserting or updating documents, you can specify the $vector field as a slice of floats or use the datatypes.Vector struct to represent and binary-encode the vector embedding.
Similarly, for vector searches, you can provide the search vector as a slice of floats or use the datatypes.Vector struct.
Use datatypes.NewVector to create a datatypes.Vector struct:
package main
import (
"github.com/datastax/astra-db-go/v2/astra/datatypes"
)
func main() {
vector := datatypes.NewVector(
[]float32{0.08, -0.62, 0.39},
)
}
For collections and documents, regardless of whether you use a slice of floats or the datatypes.Vector struct, vector embeddings are binary-encoded by default, which improves performance.
When you read the value of the $vector field, the client always returns a datatypes.Vector struct when you decode the results into astra.Document or map[string]any.
To decode the vector as a slice of floats, decode into a custom struct that defines the $vector field as []float32.