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.
If a snippet doesn't work or you have feedback, please open a GitHub issue.
jeopardy = client.collections.use("JeopardyQuestion")
data_object = jeopardy.query.fetch_object_by_id("00ff6900-e64f-5d94-90db-c8cfa3fc851b")
print(data_object.properties)
const jeopardy = client.collections.use('JeopardyQuestion')
const response = await jeopardy.query.fetchObjectById('ed89d9e7-4c9d-4a6a-8d20-095cb0026f54')
console.log(response?.properties)
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)
}
questions := client.Collections.Use("JeopardyQuestion")
// There is no fetch-by-id call. Filter on the object id instead.
response, err := questions.Query.OverAll(ctx, query.OverAll{
Filter: &filter.Cond{
Target: filter.UUID,
Operator: filter.Equal,
Value: "a1b2c3d4-e5f6-4a5b-8c9d-1a2b3c4d5e6f",
},
})
if err != nil {
// handle error
panic(err)
}
for _, obj := range response.Objects {
fmt.Printf("%s: %v\n", obj.UUID, obj.Properties)
}
CollectionHandle<Map<String, Object>> jeopardy = client.collections.use("JeopardyQuestion");
var dataObjectOpt = jeopardy.query.fetchObjectById("00ff6900-e64f-5d94-90db-c8cfa3fc851b");
dataObjectOpt.ifPresent(dataObject -> System.out.println(dataObject.properties()));
var jeopardy = client.Collections.Use("JeopardyQuestion");
var dataObject = await jeopardy.Query.FetchObjectByID(
Guid.Parse("00ff6900-e64f-5d94-90db-c8cfa3fc851b")
);
if (dataObject != null)
{
Console.WriteLine(JsonSerializer.Serialize(dataObject.Properties));
}
By default, all properties and object UUIDs are returned. Blob and reference properties are excluded unless specified otherwise. This does not apply to the Go client library.
Retrieve the object's vector
Object vectors can be retrieved by specifying its return.
If a snippet doesn't work or you have feedback, please open a GitHub issue.
jeopardy = client.collections.use("JeopardyQuestion")
data_object = jeopardy.query.fetch_object_by_id(
"00ff6900-e64f-5d94-90db-c8cfa3fc851b",
include_vector=True
)
print(data_object.vector["default"])
const jeopardy = client.collections.use('JeopardyQuestion')
const response = await jeopardy.query.fetchObjectById('ed89d9e7-4c9d-4a6a-8d20-095cb0026f54',{
includeVector: true
})
console.log(response?.properties)
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)
}
questions := client.Collections.Use("JeopardyQuestion")
// There is no fetch-by-id call. Filter on the object id instead.
response, err := questions.Query.OverAll(ctx, query.OverAll{
Filter: &filter.Cond{
Target: filter.UUID,
Operator: filter.Equal,
Value: "a1b2c3d4-e5f6-4a5b-8c9d-1a2b3c4d5e6f",
},
ReturnVectors: []string{"default"},
})
if err != nil {
// handle error
panic(err)
}
for _, obj := range response.Objects {
fmt.Printf("%s: %v\n", obj.UUID, obj.Vectors["default"].Single)
}
CollectionHandle<Map<String, Object>> jeopardy = client.collections.use("JeopardyQuestion");
var dataObjectOpt = jeopardy.query.fetchObjectById("00ff6900-e64f-5d94-90db-c8cfa3fc851b",
q -> q.includeVector()
);
dataObjectOpt.ifPresent(dataObject -> System.out
.println(Arrays.toString(dataObject.vectors().getSingle("default"))));
var jeopardy = client.Collections.Use("JeopardyQuestion");
var dataObject = await jeopardy.Query.FetchObjectByID(
Guid.Parse("00ff6900-e64f-5d94-90db-c8cfa3fc851b"),
includeVectors: true
);
if (dataObject?.Vectors.ContainsKey("default") ?? false)
{
var vector = dataObject.Vectors["default"];
Console.WriteLine(vector);
}
Retrieve named vectors
Where named vectors are used, you can retrieve one or more of them by specifying their names.
If a snippet doesn't work or you have feedback, please open a GitHub issue.
reviews = client.collections.use("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]}...")
const reviews = client.collections.use('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
CollectionHandle<Map<String, Object>> reviews = client.collections.use("WineReviewNV"); // Collection with named
var dataObjectOpt = reviews.query.fetchObjectById(objUuid, // Object UUID
q -> q.includeVector(vectorNames) // Specify to include vectors
);
// The vectors are returned in the `vectors` property as a dictionary
dataObjectOpt.ifPresent(dataObject -> {
for (String n : vectorNames) {
float[] vector = dataObject.vectors().getSingle(n);
if (vector != null) {
System.out.printf("Vector '%s': %s...\n", n, Arrays.toString(Arrays.copyOf(vector, 5)));
}
}
});
var reviews = client.Collections.Use("WineReviewNV"); // Collection with named
var dataObject = await reviews.Query.FetchObjectByID(
objUuid.Value, // Object UUID
includeVectors: true // Specify to include vectors
);
// The vectors are returned in the `Vectors` property as a dictionary
if (dataObject != null)
{
foreach (var name in vectorNames)
{
if (dataObject.Vectors.TryGetValue(name, out var vector))
{
Console.WriteLine(vector);
}
}
}
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:
If a snippet doesn't work or you have feedback, please open a GitHub issue.
# generate uuid based on the key properties used during data insert
object_uuid = generate_uuid5({"name": "Author to fetch"})
authors = client.collections.use("Author")
author_exists = authors.data.exists(object_uuid)
print("Author exist: ", author_exists)
import { generateUuid5 } from '@weaviate/node';
// 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.use('Author')
const authorExists = await authors.data.exists(object_uuid)
console.log('Author exists: ' + authorExists)
package main
import (
"context"
"fmt"
"github.com/weaviate/weaviate-go-client/v5/weaviate"
"github.com/weaviate/weaviate-go-client/v5/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.
CollectionHandle<Map<String, Object>> jeopardy = client.collections.use("JeopardyQuestion");
boolean exists = jeopardy.data.exists("00ff6900-e64f-5d94-90db-c8cfa3fc851b");
System.out.println(exists);
var jeopardy = client.Collections.Use("JeopardyQuestion");
// The C# client checks for existence by attempting to fetch an object and checking for null.
var dataObject = await jeopardy.Query.FetchObjectByID(
Guid.Parse("00ff6900-e64f-5d94-90db-c8cfa3fc851b")
);
bool exists = dataObject != null;
Console.WriteLine(exists);
Related pages
Questions and feedback
Have a question or feedback? Here's how to reach us.
