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 Client v4
- Python Client v3
- JS/TS Client v3
- JS/TS Client v2
- 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 jeopardy = client.collections.get('JeopardyQuestion')
const response = await jeopardy.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 Client v4
- Python Client v3
- JS/TS Client v3
- JS/TS Client v2
- 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 jeopardy = client.collections.get('JeopardyQuestion')
const response = await jeopardy.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 Client v4
- Python Client v3
- JS/TS Client v3
- JS/TS Client v2
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.
const reviews = client.collections.get('WineReviewNV') // Collection with named vectors
const objectUuid = '' // Object UUID
const response = await reviews.query.fetchObjectById(objectUuid,{
includeVector: ['title', 'review_body']
})
console.log(response?.vectors.title) // print the title vector
console.log(response?.vectors.review_body) // print the review_body vector
// 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 Client v4
- Python Client v3
- JS/TS Client v3
- JS/TS Client v2
- Go
- Java
# generate uuid based on the key properties used during data insert
object_uuid = generate_uuid5({"name": "Author to fetch"})
authors = client.collections.get("Author")
author_exists = authors.data.exists(object_uuid)
print("Author exist: ", author_exists)
exists = client.data_object.exists(
"36ddd591-2dee-4e7e-a3cc-eb86d30a4303",
class_name="Author",
)
print(exists)
import { generateUuid5 } from 'weaviate-client';
// generate uuid based on the key properties used during data insert
const object_uuid = generateUuid5(
JSON.stringify({ name: "Author to fetch"})
)
const authors = await client.collections.get('Author')
const authorExists = await authors.data.exists(object_uuid)
console.log('Author exists: ' + authorExists)
const response = await client.data
.checker()
.withClassName('Author')
.withId('df48b9f6-ba48-470c-bf6a-57657cb07390')
.do();
console.log(response);
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 the user forum.