Read objects
Overview
This page will show you how to read individual objects in Weaviate.
Requirements
To read a specific object from a collection, you'll need its id and class name.
Why specify the class name?
Prior to Weaviate v1.14
it was possible to manipulate objects using only their ID without specifying the class name. This way is still supported for backward compatibility, but is considered deprecated now. Avoid using the deprecated endpoints, as they will be removed with Weaviate v2.0.0
.
The reason for the deprecation is that classes generally act like namespaces, therefore duplicate IDs are possible.
Additionally, where multi-tenancy (available from v1.20.0
) is enabled, the tenant name is required.
It is thus recommended to always include the class name when manipulating objects.
For classes where multi-tenancy is enabled, you will also need to specify the tenant name. See Manage data: multi-tenancy operations for details on how.
Get object by id
To retrieve an object by id, make a GET
request to the /v1/objects
REST API endpoint, or use the client code below. If the id doesn't exist in the specified class, a 404 error will be returned.
- Python
- JavaScript/TypeScript
- Java
- Go
data_object = client.data_object.get_by_id(
'00ff6900-e64f-5d94-90db-c8cfa3fc851b',
class_name='JeopardyQuestion',
)
print(json.dumps(data_object, indent=2))
result = await client.data
.getterById()
.withClassName('JeopardyQuestion')
.withId('00ff6900-e64f-5d94-90db-c8cfa3fc851b')
.do();
console.log(JSON.stringify(result, null, 2));
Result<List<WeaviateObject>> result = client.data().objectsGetter()
.withClassName("JeopardyQuestion")
.withID("00ff6900-e64f-5d94-90db-c8cfa3fc851b")
.run();
System.out.println(result.getResult());
objects, err := client.Data().ObjectsGetter().
WithClassName("JeopardyQuestion").
WithID("00ff6900-e64f-5d94-90db-c8cfa3fc851b").
Do(ctx)
if err != nil {
// handle error
panic(err)
}
for i, obj := range objects {
fmt.Printf("object[%v]: %+v\n", i, *obj)
}
Retrieve the object vector
To retrieve the object vector, set the vector
parameter:
- Python
- JavaScript/TypeScript
- Java
- Go
data_object = client.data_object.get_by_id(
'00ff6900-e64f-5d94-90db-c8cfa3fc851b',
class_name='JeopardyQuestion',
with_vector=True
)
print(json.dumps(data_object, indent=2))
result = await client.data
.getterById()
.withClassName('JeopardyQuestion')
.withId('00ff6900-e64f-5d94-90db-c8cfa3fc851b')
.withVector()
.do();
console.log(JSON.stringify(result, null, 2));
Result<List<WeaviateObject>> result = client.data().objectsGetter()
.withClassName("JeopardyQuestion")
.withID("00ff6900-e64f-5d94-90db-c8cfa3fc851b")
.withVector()
// handle error
.run();
System.out.println(result.getResult());
objects, err := client.Data().ObjectsGetter().
WithClassName("JeopardyQuestion").
WithID("00ff6900-e64f-5d94-90db-c8cfa3fc851b").
WithVector().
Do(ctx)
if err != nil {
// handle error
panic(err)
}
for i, obj := range objects {
fmt.Printf("object[%v]: %+v\n", i, *obj)
}
More Resources
For additional information, try these sources.