Create objects
The examples on this page demonstrate how to create individual objects in Weaviate.
Create an object
Section titled “Create an object”This example creates an object in the JeopardyQuestion collection.
jeopardy = client.collections.use("JeopardyQuestion")uuid = jeopardy.data.insert({ "question": "This vector DB is OSS & supports automatic property type inference on import", # "answer": "Weaviate", # properties can be omitted "newProperty": 123, # will be automatically added as a number property})print(uuid) # the return value is the object's UUIDconst jeopardy = client.collections.use('JeopardyQuestion')w, err := client.Data().Creator().
WithClassName("JeopardyQuestion").
WithProperties(map[string]interface{}{
"question": "This vector DB is OSS and supports automatic property type inference on import",
// "answer": "Weaviate", // schema properties can be omitted
"newProperty": 123, // will be automatically added as a number property
}).
Do(ctx)var jeopardy = client.collections.use("JeopardyQuestion");var uuid = jeopardy.data.insert(Map.of( "question", "This vector DB is OSS & supports automatic property type inference on import", // "answer": "Weaviate", // properties can be omitted "newProperty", 123 // will be automatically added as a number property)).uuid();System.out.println(uuid); // the return value is the object's UUIDvar jeopardy = client.Collections.Use("JeopardyQuestion");var uuid = await jeopardy.Data.Insert( new { question = "This vector DB is OSS & supports automatic property type inference on import", // answer = "Weaviate", // properties can be omitted newProperty = 123, // will be automatically added as a number property });Console.WriteLine(uuid); // the return value is the object's UUIDAdditional information
To create an object, specify the following:
- The object data you want to add
- The target collection
- If multi-tenancy is enabled, specify the tenant
By default, auto-schema creates new collections and adds new properties.
Create an object with a specified vector
Section titled “Create an object with a specified vector”When you create an object, you can provide a vector. (For specifying multiple, named vectors, see below.)
jeopardy = client.collections.use("JeopardyQuestion")uuid = jeopardy.data.insert( properties={ "question": "This vector DB is OSS and supports automatic property type inference on import", "answer": "Weaviate", }, vector=[0.12345] * 1536)print(uuid) # the return value is the object's UUIDconst jeopardy = client.collections.use('JeopardyQuestion')vector := make([]float32, 1536)for i := 0; i < len(vector); i++ { vector[i] = 0.12345}w, err := client.Data().Creator(). WithClassName("JeopardyQuestion"). WithProperties(map[string]interface{}{ "question": "This vector DB is OSS and supports automatic property type inference on import", "answer": "Weaviate", }). WithVector(vector). Do(ctx)var jeopardy = client.collections.use("JeopardyQuestion");var uuid = jeopardy.data.insert(Map.of("question", "This vector DB is OSS and supports automatic property type inference on import", "answer", "Weaviate"), meta -> meta.vectors(Vectors.of(new float[384])) // Using a zero vector for demonstration).uuid();System.out.println(uuid); // the return value is the object's UUIDvar jeopardy = client.Collections.Use("JeopardyQuestion");var uuid = await jeopardy.Data.Insert( new { question = "This vector DB is OSS and supports automatic property type inference on import", answer = "Weaviate", }, vectors: new float[300] // Using a zero vector for demonstration);Console.WriteLine(uuid); // the return value is the object's UUIDCreate an object with named vectors
Section titled “Create an object with named vectors”When you create an object, you can specify named vectors (if configured in your collection).
reviews = client.collections.use("WineReviewNV") # This collection must have named vectors configureduuid = reviews.data.insert( properties={ "title": "A delicious Riesling", "review_body": "This wine is a delicious Riesling which pairs well with seafood.", "country": "Germany", }, # Specify the named vectors, following the collection definition vector={ "title": [0.12345] * 1536, "review_body": [0.31313] * 1536, "title_country": [0.05050] * 1536, })print(uuid) # the return value is the object's UUIDconst reviews = client.collections.use('WineReviewNV')var reviews = client.collections.use("WineReviewNV"); // This collection must have named vectors configuredvar uuid = reviews.data.insert( Map.of("title", "A delicious Riesling", "review_body", "This wine is a delicious Riesling which pairs well with seafood.", "country", "Germany"), // Specify the named vectors, following the collection definition meta -> meta.vectors(Vectors.of("title", new float[1536]), Vectors.of("review_body", new float[1536]), Vectors.of("title_country", new float[1536]))).uuid();System.out.println(uuid); // the return value is the object's UUIDvar reviews = client.Collections.Use("WineReviewNV"); // This collection must have named vectors configuredvar uuid = await reviews.Data.Insert( new { title = "A delicious Riesling", review_body = "This wine is a delicious Riesling which pairs well with seafood.", country = "Germany", }, // Specify the named vectors, following the collection definition vectors: new Vectors { { "title", new float[1536] }, { "review_body", new float[1536] }, { "title_country", new float[1536] }, });Console.WriteLine(uuid); // the return value is the object's UUIDCreate an object with a specified ID
Section titled “Create an object with a specified ID”When you create an object, you can specify an ID.
properties = { "question": "This vector DB is OSS and supports automatic property type inference on import", "answer": "Weaviate",}jeopardy = client.collections.use("JeopardyQuestion")uuid = jeopardy.data.insert( properties=properties, uuid="12345678-e64f-5d94-90db-c8cfa3fc1234")print(uuid) # the return value is the object's UUIDconst jeopardy = client.collections.use('JeopardyQuestion')vector := make([]float32, 1536)for i := 0; i < len(vector); i++ { vector[i] = 0.12345}w, err := client.Data().Creator(). WithClassName("JeopardyQuestion"). WithProperties(map[string]interface{}{ "question": "This vector DB is OSS and supports automatic property type inference on import", "answer": "Weaviate", }). WithID("12345678-e64f-5d94-90db-c8cfa3fc1234"). Do(ctx)Map<String, Object> properties = new HashMap<>();properties.put("question", "This vector DB is OSS and supports automatic property type inference on import");properties.put("answer", "Weaviate");var jeopardy = client.collections.use("JeopardyQuestion");var uuid = jeopardy.data.insert(properties, meta -> meta.uuid("12345678-e64f-5d94-90db-c8cfa3fc1234")).uuid();System.out.println(uuid); // the return value is the object's UUIDvar jeopardy = client.Collections.Use("JeopardyQuestion");var uuid = await jeopardy.Data.Insert( new { question = "This vector DB is OSS and supports automatic property type inference on import", answer = "Weaviate", }, uuid: Guid.Parse("12345678-e64f-5d94-90db-c8cfa3fc1234"));Console.WriteLine(uuid); // the return value is the object's UUIDGenerate deterministic IDs
Section titled “Generate deterministic IDs”You can generate an ID based on your data object.
from weaviate.util import generate_uuid5 # Generate a deterministic IDdata_object = { "question": "This vector DB is OSS and supports automatic property type inference on import", "answer": "Weaviate",}jeopardy = client.collections.use("JeopardyQuestion")uuid = jeopardy.data.insert( properties=data_object, uuid=generate_uuid5(data_object),)import { generateUuid5 } from 'weaviate-client';// This feature is under development// In Java, you can generate a deterministic UUID from a string or bytes.// This helper function uses UUID.nameUUIDFromBytes for this purpose.Map<String, Object> dataObject = new HashMap<>();dataObject.put("question", "This vector DB is OSS and supports automatic property type inference on import");dataObject.put("answer", "Weaviate");var jeopardy = client.collections.use("JeopardyQuestion");var uuid = jeopardy.data.insert(dataObject, meta -> meta.uuid(generateUuid5(dataObject.toString()).toString())).uuid();// In C#, you can generate a deterministic UUID from a string or bytes.// This helper function creates a UUID v5 for this purpose.var dataObject = new{ question = "This vector DB is OSS and supports automatic property type inference on import", answer = "Weaviate",};var dataObjectString = JsonSerializer.Serialize(dataObject);var jeopardy = client.Collections.Use("JeopardyQuestion");var uuid = await jeopardy.Data.Insert( dataObject, uuid: GenerateUuid5(dataObjectString));Additional information
To generate deterministic IDs, use one of these methods:
generate_uuid5(Python)generateUuid5(TypeScript)
Create an object with cross-references
Section titled “Create an object with cross-references”You can create an object with cross-references to other objects.
questions = client.collections.use("JeopardyQuestion")questions.data.insert( properties=properties, # A dictionary with the properties of the object uuid=obj_uuid, # The UUID of the object references={"hasCategory": category_uuid}, # e.g. {"hasCategory": "583876f3-e293-5b5b-9839-03f455f14575"})const category = client.collections.use('JeopardyCategory')const dataObject = {'name': 'Science'}const response = await category.data.insert({ properties: dataObject, references: { 'hasCategory': categoryId // e.g. {'hasCategory': '583876f3-e293-5b5b-9839-03f455f14575'} }})console.log('UUID: ', response)var questions = client.collections.use("JeopardyQuestion");var result = questions.data.insert(properties, // A map with the properties of the object opt -> opt .reference("hasCategory", ObjectReference.uuids(categoryUuid)) // e.g. {"hasCategory":// "583876f3-e293-5b5b-9839-03f455f14575"});Create an object with geoCoordinates
Section titled “Create an object with geoCoordinates”If you want to supply a geoCoordinates property, you need to specify the latitude and longitude as floating point decimal degrees:
publications = client.collections.use("Publication")
publications.data.insert(
properties={
"headquartersGeoLocation": {
"latitude": 52.3932696,
"longitude": 4.8374263
}
},
)const publication = client.collections.use('Publication')
uuid = await publication.data.insert({
properties: {
name: 'Elsevier',
headquartersGeoLocation: {
'latitude': 52.3932696,
'longitude': 4.8374263,
},
},
id: 'df48b9f6-ba48-470c-bf6a-57657cb07390'
})
console.log('UUID: ', uuid)package main
import (
"context"
"fmt"
"github.com/weaviate/weaviate-go-client/v5/weaviate"
)
func main() {
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client, err := weaviate.NewClient(cfg)
if err != nil {
panic(err)
}
dataSchema := map[string]interface{}{
"name": "Elsevier",
"headquartersGeoLocation": map[string]float32{
"latitude": 52.3932696,
"longitude": 4.8374263,
},
}
created, err := client.Data().Creator().
WithClassName("Publication").
WithID("df48b9f6-ba48-470c-bf6a-57657cb07390").
WithProperties(dataSchema).
Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("%v", created)
}var publications = client.collections.use("Publication");
var uuid = publications.data
.insert(Map.of("headquartersGeoLocation",
Map.of("latitude", 52.3932696, "longitude", 4.8374263)))
.uuid();var publications = client.Collections.Use("Publication");
var uuid = await publications.Data.Insert(
new { headquartersGeoLocation = new GeoCoordinate(52.3932696f, 4.8374263f) }
);Validate objects before creation
Section titled “Validate objects before creation”Before you create an object, you can validate it against the collection definition.
# Validate is currently not supported with the Weaviate Python client v4// Validate is currently not supported with the Weaviate TypeScript client v3err := client.Data().Validator().
WithClassName("JeopardyQuestion").
WithProperties(map[string]interface{}{
"question": "This vector DB is OSS and supports automatic property type inference on import",
"answer": "Weaviate",
"thisPropShouldNotEndUpInTheSchema": -1,
}).
WithID("12345678-1234-1234-1234-123456789012").
Do(ctx)// Coming soon// Coming soonMultiple vector embeddings (named vectors)
Section titled “Multiple vector embeddings (named vectors)”Collections can have multiple named vectors.
The vectors in a collection can have their own configurations. Each vector space can set its own index, its own compression algorithm, and its own vectorizer. This means you can use different vectorization models, and apply different distance metrics, to the same object.
To work with named vectors, adjust your queries to specify a target vector for vector search or hybrid search queries.
Related pages
Section titled “Related pages”Questions and feedback
Section titled “Questions and feedback”Have a question or feedback? Here's how to reach us.