Read objects
Instead of querying your database, you can use an ID to retrieve individual objects.
Additional information
Get an object by id
Section titled “Get an object by id”Use an ID to retrieve an object. If the id doesn't exist, Weaviate returns a 404 error.
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)
}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));}Retrieve the object's vector
Section titled “Retrieve the object's vector”Object vectors can be retrieved by specifying its return.
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)}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
Section titled “Retrieve named vectors”Where named vectors are used, you can retrieve one or more of them by specifying their names.
reviews = client.collections.use("WineReviewNV") # Collection with named vectorsconst reviews = client.collections.use('WineReviewNV') // Collection with named vectorsconst objectUuid = '' // Object UUIDconst response = await reviews.query.fetchObjectById(objectUuid,{ includeVector: ['title', 'review_body']})console.log(response?.vectors.title) // print the title vectorconsole.log(response?.vectors.review_body) // print the review_body vectorCollectionHandle<Map<String, Object>> reviews = client.collections.use("WineReviewNV"); // Collection with namedvar reviews = client.Collections.Use("WineReviewNV"); // Collection with namedCheck object existence
Section titled “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:
# generate uuid based on the key properties used during data insert
object_uuid = generate_uuid5({"name": "Author to fetch"})import { generateUuid5 } from 'weaviate-client';// generate uuid based on the key properties used during data insertconst 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
Section titled “Related pages”Questions and feedback
Section titled “Questions and feedback”Have a question or feedback? Here's how to reach us.