Skip to main content

Basic search examples

With Weaviate you can query your data using vector similarity search, keyword search, or a mix of both with hybrid search. You can control what object properties and metadata to return.

This page provides fundamental search syntax to get you started.

You can get objects without specifying any parameters. This returns objects ordered by their UUID.

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects()

for o in response.objects:
print(o.properties)
Example response

The output is like this:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"question": "This prophet passed the time he spent inside a fish offering up prayers"
},
// shortened for brevity
]
}
}
}
Additional information

Specify the information that you want your query to return. You can return object properties, object IDs, and object metadata.

limit returned objects

Use limit to set a fixed maximum number of objects to return.

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
limit=1
)

for o in response.objects:
print(o.properties)
Example response

The output is like this:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"question": "This prophet passed the time he spent inside a fish offering up prayers"
},
// Note this will only have one result as we limited it to 1
]
}
}
}

Paginate with limit and offset

To start in the middle of your result set, define an offset. Set a limit to return objects starting at the offset.

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
limit=1,
offset=1
)

for o in response.objects:
print(o.properties)
Example response

The output is like this:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"question": "Pythons are oviparous, meaning they do this"
}
]
}
}
}

To paginate through the entire database, use a cursor instead of offset and limit.

Specify object properties

You can specify which object properties to return.

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
limit=1,
return_properties=["question", "answer", "points"]
)

for o in response.objects:
print(o.properties)
Example response

The output is like this:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"answer": "Jonah",
"points": 100,
"question": "This prophet passed the time he spent inside a fish offering up prayers"
},
]
}
}
}

Retrieve the object vector

You can retrieve the object vector. (Also applicable where named vectors are used.)

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
include_vector=True,
limit=1
)

print(response.objects[0].vector["default"])
Example response

The output is like this:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"vector": [
0.0065065133,
-0.017786196,
0.005879146,
0.006707012,
... // shortened for brevity
]
}
},
]
}
}
}

Retrieve the object id

You can retrieve the object id (uuid).

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
# Object IDs are included by default with the `v4` client! :)
limit=1
)

for o in response.objects:
print(o.uuid)
Example response

The output is like this:

{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"id": "0002bf92-80c8-5d94-af34-0d6c5fea1aaf"
}
},
// shortened for brevity
]
}
}
}

Retrieve cross-referenced properties

To retrieve properties from cross-referenced objects, specify:

  • The cross-reference property
  • The target cross-referenced collection
  • The properties to retrieve
from weaviate.classes.query import QueryReference

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
return_references=[
QueryReference(
link_on="hasCategory",
return_properties=["title"]
),
],
limit=2
)

for o in response.objects:
print(o.properties["question"])
# print referenced objects
for ref_obj in o.references["hasCategory"].objects:
print(ref_obj.properties)
Example response

The output is like this:

    {
"data": {
"Get": {
"JeopardyQuestion": [
{
"hasCategory": [{"title": "THE BIBLE"}],
"question": "This prophet passed the time he spent inside a fish offering up prayers",
},
{
"hasCategory": [{"title": "ANIMALS"}],
"question": "Pythons are oviparous, meaning they do this",
},
]
}
}
}

Retrieve metadata values

You can specify metadata fields to be returned.

from weaviate.classes.query import MetadataQuery

jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
limit=1,
return_metadata=MetadataQuery(creation_time=True)
)

for o in response.objects:
print(o.properties) # View the returned properties
print(o.metadata.creation_time) # View the returned creation time

For a comprehensive list of metadata fields, see GraphQL: Additional properties.

Multi-tenancy

If multi-tenancy is enabled, specify the tenant parameter in each query.

# Connect to the collection
mt_collection = client.collections.get("WineReviewMT")

# Get the specific tenant's version of the collection
collection_tenant_a = mt_collection.with_tenant("tenantA")

# Query tenantA's version
response = collection_tenant_a.query.fetch_objects(
return_properties=["review_body", "title"],
limit=1,
)

print(response.objects[0].properties)

Questions and feedback

If you have any questions or feedback, please let us know on our forum. For example, you can: