Introduction
Data is added through the RESTful API. Python and JavaScript clients are available. The syntax of a data object is as follows:
{
"class": "<class name>", // as defined during schema creation
"id": "<UUID>", // optional, should be in UUID format.
"properties": {
"<property name>": "<property value>", // specified in dataType defined during schema creation
}
}
Prerequisites
- Connect to a Weaviate instance.
If you haven’t set up a Weaviate instance yet, check the Getting started guide. In this guide we assume your instance is running athttp://localhost:8080
with text2vec-contextionary as vectorization module. - Upload a schema.
Learn how to create and upload a schema here. In this guide we assume to have a similar schema uploaded with the classesPublication
,Article
andAuthor
.
Add a data object
Let’s add a Publication
with the name New York Times
to your Weaviate instance. Not all properties have to be filled when adding a data object, so we will skip the hasArticles
property for now, since we don’t have any Article
objects yet. Note that the UUID
is given in the id
parameter now, this is optional.
import weaviate
client = weaviate.Client("http://localhost:8080")
example_data = {
"name": "New York Times"
}
client.data_object.create(example_data, "Publication", "f81bfe5e-16ba-4615-a516-46c2ae2e5a80")
const weaviate = require("weaviate-client");
const client = weaviate.client({
scheme: 'http',
host: 'localhost:8080',
});
client.data
.creator()
.withClassName('Publication')
.withId("f81bfe5e-16ba-4615-a516-46c2ae2e5a80")
.withProperties({
"name": "New York Times"
})
.do()
.then(res => {
console.log(res)
})
.catch(err => {
console.error(err)
});
package main
import (
"context"
"fmt"
"github.com/semi-technologies/weaviate-go-client/v4/weaviate"
)
func main() {
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client := weaviate.New(cfg)
dataSchema := map[string]string{
"name": "New York Times",
}
created, err := client.Data().Creator().
WithClassName("Publication").
WithID("f81bfe5e-16ba-4615-a516-46c2ae2e5a80").
WithProperties(dataSchema).
Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("%v", created)
}
package technology.semi.weaviate;
import java.util.HashMap;
import java.util.Map;
import technology.semi.weaviate.client.Config;
import technology.semi.weaviate.client.WeaviateClient;
import technology.semi.weaviate.client.base.Result;
import technology.semi.weaviate.client.v1.data.model.WeaviateObject;
public class App {
public static void main(String[] args) {
Config config = new Config("http", "localhost:8080");
WeaviateClient client = new WeaviateClient(config);
Map<String, Object> dataSchema = new HashMap<>();
dataSchema.put("name", "New York Times");
Result<WeaviateObject> result = client.data().creator()
.withClassName("Publication")
.withID("f81bfe5e-16ba-4615-a516-46c2ae2e5a80")
.withProperties(dataSchema)
.run();
if (result.hasErrors()) {
System.out.println(result.getError());
return;
}
System.out.println(result.getResult());
}
}
$ curl \
-X POST \
-H "Content-Type: application/json" \
-d '{
"class": "Publication",
"id": "f81bfe5e-16ba-4615-a516-46c2ae2e5a80",
"properties": {
"name": "New York Times"
}
}' \
http://localhost:8080/v1/objects
Add a data object with reference
If you want to add data object with a reference in a property, you need to use the UUID
of the reference data object. Let’s add the Author
named Jodi Kantor
, who writes for the New York Times
:
import weaviate
client = weaviate.Client("http://localhost:8080")
example_data = {
"name": "Jodi Kantor",
"writesFor": [{
"beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
}]
}
client.data_object.create(example_data, "Author", "36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
const weaviate = require("weaviate-client");
const client = weaviate.client({
scheme: 'http',
host: 'localhost:8080',
});
client.data
.creator()
.withClassName('Author')
.withId("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
.withProperties({
"name": "Jodi Kantor",
"writesFor": [{
"beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
}]
})
.do()
.then(res => {
console.log(res)
})
.catch(err => {
console.error(err)
});
package main
import (
"context"
"fmt"
"github.com/semi-technologies/weaviate-go-client/v4/weaviate"
)
func main() {
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client := weaviate.New(cfg)
dataSchema := map[string]interface{}{
"name": "Jodi Kantor",
"writesFor": map[string]string{
"beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80",
},
}
created, err := client.Data().Creator().
WithClassName("Author").
WithID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303").
WithProperties(dataSchema).
Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("%v", created)
}
package technology.semi.weaviate;
import java.util.HashMap;
import java.util.Map;
import technology.semi.weaviate.client.Config;
import technology.semi.weaviate.client.WeaviateClient;
import technology.semi.weaviate.client.base.Result;
import technology.semi.weaviate.client.v1.data.model.WeaviateObject;
public class App {
public static void main(String[] args) {
Config config = new Config("http", "localhost:8080");
WeaviateClient client = new WeaviateClient(config);
Map<String, Object> dataSchema = new HashMap<>();
dataSchema.put("name", "Jodi Kantor");
dataSchema.put("writesFor", new HashMap() {
{
put("beacon", "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80");
}
});
Result<WeaviateObject> result = client.data().creator()
.withClassName("Author")
.withID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
.withProperties(dataSchema)
.run();
if (result.hasErrors()) {
System.out.println(result.getError());
return;
}
System.out.println(result.getResult());
}
}
$ curl \
-X POST \
-H "Content-Type: application/json" \
-d '{
"class": "Author",
"id": "36ddd591-2dee-4e7e-a3cc-eb86d30a4303",
"properties": {
"name": "Jodi Kantor",
"writesFor": [{
"beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
}]
}
}' \
http://localhost:8080/v1/objects
You can also add references later, when the data object is already created. The following example first creates the Author
with name
, and later adds the reference to a Publication
. This comes in handy when you need to create data objects first before you can add references.
import weaviate
import time
client = weaviate.Client("http://localhost:8080")
example_data = {
"name": "Jodi Kantor"
}
client.data_object.create(example_data, "Author", "36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
time.sleep(1)
client.data_object.reference.add("36ddd591-2dee-4e7e-a3cc-eb86d30a4303", "writesFor", "f81bfe5e-16ba-4615-a516-46c2ae2e5a80")
const weaviate = require("weaviate-client");
const client = weaviate.client({
scheme: 'http',
host: 'localhost:8080',
});
client.data
.creator()
.withClassName('Author')
.withId("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
.withProperties({
"name": "Jodi Kantor"
})
.do()
.then(res => {
console.log(res)
})
.catch(err => {
console.error(err)
});
client.data
.referenceCreator()
.withId("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
.withReferenceProperty('writesFor')
.withReference(
client.data
.referencePayloadBuilder()
.withId("f81bfe5e-16ba-4615-a516-46c2ae2e5a80")
.payload(),
)
.do()
.then(res => {
console.log(res)
})
.catch(err => {
console.error(err)
});
package main
import (
"context"
"github.com/semi-technologies/weaviate-go-client/v4/weaviate"
)
func main() {
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client := weaviate.New(cfg)
dataSchema := map[string]interface{}{
"name": "Jodi Kantor",
}
_, err := client.Data().Creator().WithClassName("Author").WithID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303").WithProperties(dataSchema).Do(context.Background())
if err != nil {
panic(err)
}
reference := client.Data().ReferencePayloadBuilder().WithID("f81bfe5e-16ba-4615-a516-46c2ae2e5a80").Payload()
err = client.Data().ReferenceCreator().
WithID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303").
WithReferenceProperty("writesFor").
WithReference(reference).
Do(context.Background())
if err != nil {
panic(err)
}
}
package technology.semi.weaviate;
import java.util.HashMap;
import java.util.Map;
import technology.semi.weaviate.client.Config;
import technology.semi.weaviate.client.WeaviateClient;
import technology.semi.weaviate.client.base.Result;
import technology.semi.weaviate.client.v1.data.model.SingleRef;
import technology.semi.weaviate.client.v1.data.model.WeaviateObject;
public class App {
public static void main(String[] args) {
Config config = new Config("http", "localhost:8080");
WeaviateClient client = new WeaviateClient(config);
Map<String, Object> dataSchema = new HashMap<>();
dataSchema.put("name", "Jodi Kantor");
Result<WeaviateObject> result = client.data().creator()
.withClassName("Author")
.withID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
.withProperties(dataSchema)
.run();
if (result.hasErrors()) {
System.out.println(result.getError());
return;
}
System.out.println(result.getResult());
SingleRef reference = client.data().referencePayloadBuilder().withID("f81bfe5e-16ba-4615-a516-46c2ae2e5a80").payload();
Result<Boolean> result_ref = client.data().referenceCreator()
.withID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
.withReferenceProperty("writesFor")
.withReference(reference)
.run();
if (result_ref.hasErrors()) {
System.out.println(result_ref.getError());
return;
}
System.out.println(result_ref.getResult());
}
}
$ curl \
-X POST \
-H "Content-Type: application/json" \
-d '{
"class": "Author",
"id": "36ddd591-2dee-4e7e-a3cc-eb86d30a4303",
"properties": {
"name": "Jodi Kantor"
}
}' \
http://localhost:8080/v1/objects
$ curl \
-X POST \
-H "Content-Type: application/json" \
-d '{
"beacon": "weaviate://localhost/f81bfe5e-16ba-4615-a516-46c2ae2e5a80"
}' \
http://localhost:8080/v1/36ddd591-2dee-4e7e-a3cc-eb86d30a4303/references/writesFor
Next steps
- Take a look at How to query data to learn how to interact with the data you just added.
- See the RESTful API reference pages for all API operations to add, modify and delete data.
More Resources
If you can’t find the answer to your question here, please look at the:
- Frequently Asked Questions. Or,
- Knowledge base of old issues. Or,
- For questions: Stackoverflow. Or,
- For issues: Github. Or,
- Ask your question in the Slack channel: Slack.