Read objects
Instead of querying your database, you can use an ID to retrieve individual objects.
Additional information
Collections act like namespaces, so two different collections could have duplicate IDs between them.
Prior to Weaviate v1.14
you can manipulate objects without specifying the collection name. This method is deprecated. It will be removed in Weaviate v2.0.0
.
Starting in v1.20
, you can have multi-tenant datasets. When multi-tenancy
is enabled, the tenant name is required.
Always include the collection name, and, when enabled, the tenant name.
Get an object by id
Use an ID to retrieve an object. If the id doesn't exist, Weaviate returns a 404 error.
- Python (v4)
- Python (v3)
- JS/TS (Beta)
- JS/TS
- Java
- Go
jeopardy = client.collections.get("JeopardyQuestion")
data_object = jeopardy.query.fetch_object_by_id("00ff6900-e64f-5d94-90db-c8cfa3fc851b")
print(data_object.properties)
data_object = client.data_object.get_by_id(
"00ff6900-e64f-5d94-90db-c8cfa3fc851b",
class_name="JeopardyQuestion",
)
print(json.dumps(data_object, indent=2))
const myCollection = client.collections.get('JeopardyQuestion')
const response = await myCollection.query.fetchObjectById('ed89d9e7-4c9d-4a6a-8d20-095cb0026f54')
console.log(response?.properties)
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's vector
Object vectors can be retrieved by specifying its return.
- Python (v4)
- Python (v3)
- JS/TS (Beta)
- JS/TS
- Java
- Go
jeopardy = client.collections.get("JeopardyQuestion")
data_object = jeopardy.query.fetch_object_by_id(
"00ff6900-e64f-5d94-90db-c8cfa3fc851b",
include_vector=True
)
print(data_object.vector["default"])
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))
const myCollection = client.collections.get('JeopardyQuestion')
const response = await myCollection.query.fetchObjectById('ed89d9e7-4c9d-4a6a-8d20-095cb0026f54',{
includeVector: true
})
console.log(response?.properties)
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)
}
Retrieve named vectors
Where named vectors are used, you can retrieve one or more of them by specifying their names.
- Python (v4)
- Python (v3)
- JS/TS (Beta)
- JS/TS
reviews = client.collections.get("WineReviewNV") # Collection with named vectors
vector_names = ["title", "review_body"]
data_object = reviews.query.fetch_object_by_id(
uuid=obj_uuid, # Object UUID
include_vector=vector_names # Specify names of the vectors to include
)
# The vectors are returned in the `vector` property as a dictionary
for n in vector_names:
print(f"Vector '{n}': {data_object.vector[n][:5]}...")
# Unfortunately, named vectors are not suppored in the v3 API / Python client.
# Please upgrade to the v4 API / Python client to use named vectors.
// Example coming soon
// Example coming soon
Check object existence
To efficiently check if an object with a given id exists without retrieving it, make a HEAD
request to the /v1/objects/
REST endpoint, or use the following client code:
- Python (v4)
- Python (v3)
- JavaScript/TypeScript
- Go
- Java
# Note - The V4 Python client uses gRPC for object-related operations.
# The snippets shown are for the same functionality as the REST call.
import weaviate
client = weaviate.connect_to_local()
try:
object_uuid = generate_uuid5("SomeUUIDSeed")
authors = client.collections.get("Author")
# authors = authors.with_consistency_level(wvc.config.ConsistencyLevel.ALL) # If you want to set the consistency level
fetched_obj = authors.query.fetch_object_by_id(uuid=object_uuid) # If it does not exist, it will return None
if fetched_obj is None:
print("Object does not exist")
else:
print(fetched_obj.properties)
finally:
client.close()
import weaviate
client = weaviate.Client("http://localhost:8080")
exists = client.data_object.exists(
"36ddd591-2dee-4e7e-a3cc-eb86d30a4303",
class_name="MyClass",
consistency_level=weaviate.data.replication.ConsistencyLevel.ONE,
)
# The parameter "consistency_level" can be one of ConsistencyLevel.ALL,
# ConsistencyLevel.QUORUM (default), or ConsistencyLevel.ONE. Determines how many
# replicas must acknowledge a request before it is considered successful.
print(exists)
import weaviate from 'weaviate-ts-client';
const client = weaviate.client({
scheme: 'http',
host: 'localhost:8080',
});
const response = await client.data
.checker()
.withClassName('MyClass')
.withId('df48b9f6-ba48-470c-bf6a-57657cb07390')
// .withConsistencyLevel('QUORUM') // may not work yet - https://github.com/weaviate/typescript-client/issues/79
.do();
console.log(response);
// The parameter passed to `withConsistencyLevel` can be one of:
// * 'ALL',
// * 'QUORUM' (default), or
// * 'ONE'.
//
// It determines how many replicas must acknowledge a request
// before it is considered successful.
package main
import (
"context"
"fmt"
"github.com/weaviate/weaviate-go-client/v4/weaviate"
"github.com/weaviate/weaviate-go-client/v4/weaviate/data/replication" // for consistency levels
)
func main() {
cfg := weaviate.Config{
Host: "localhost:8080",
Scheme: "http",
}
client, err := weaviate.NewClient(cfg)
if err != nil {
panic(err)
}
exists, err := client.Data().Checker().
WithClassName("MyClass").
WithID("36ddd591-2dee-4e7e-a3cc-eb86d30a0923").
WithConsistencyLevel(replication.ConsistencyLevel.ONE). // default QUORUM
Do(context.Background())
if err != nil {
panic(err)
}
fmt.Printf("%v", exists)
}
// The parameter passed to "WithConsistencyLevel" can be one of:
// * replication.ConsistencyLevel.ALL,
// * replication.ConsistencyLevel.QUORUM, or
// * replication.ConsistencyLevel.ONE.
//
// It determines how many replicas must acknowledge a request
// before it is considered successful.
package io.weaviate;
import io.weaviate.client.Config;
import io.weaviate.client.WeaviateClient;
import io.weaviate.client.base.Result;
import io.weaviate.client.v1.data.replication.model.ConsistencyLevel;
public class App {
public static void main(String[] args) {
Config config = new Config("http", "localhost:8080");
WeaviateClient client = new WeaviateClient(config);
Result<Boolean> result = client.data().checker()
.withClassName("MyClass")
.withID("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
.withConsistencyLevel(ConsistencyLevel.ONE) // default QUORUM
.run();
if (result.hasErrors()) {
System.out.println(result.getError());
return;
}
System.out.println(result.getResult());
}
}
// The parameter passed to `withConsistencyLevel` can be one of:
// * ConsistencyLevel.ALL,
// * ConsistencyLevel.QUORUM, or
// * ConsistencyLevel.ONE.
//
// It determines how many replicas must acknowledge a request
// before it is considered successful.
Related pages
Questions and feedback
If you have any questions or feedback, let us know in our user forum.