Skip to main content

Basic search examples

note

Weaviate client APIs are transitioning from the "class" model to a "collection" model. Expect to see differences in terminology, descriptions, and output formats during the transition period.

This page has basic search examples.

Additional information

Use the Get function to search.

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

# for o in response.objects:
# print(json.dumps(o.properties, indent=2))
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. Weaviate search is based on the GraphQL API.

limit returned objects

To return a limited number of objects, set a limit.

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

# for o in response.objects:
# print(json.dumps(o.properties, indent=2))
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(json.dumps(o.properties, indent=2))
Example response

The output is like this:

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

Specify object properties

You can specify object properties as below.

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

# for o in response.objects:
# print(json.dumps(o.properties, indent=2))
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

To retrieve the object vector, specify the vector in your query.

import weaviate.classes as wvc

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

# print(response.objects[0].vector)
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

To retrieve the object ID, request the _additional property and id sub-property.

The new Python client always returns the object ID.

import weaviate.classes as wvc

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 following items:

  • The cross-reference property
  • The target cross-referenced collection
  • The properties to retrieve
import weaviate.classes as wvc

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

# for o in response.objects:
# print(o.properties["question"])
# # print referenced objects
# for ref in o.properties["hasCategory"].objects:
# print(ref.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 other metadata values

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

Group results

To group results, see the groupBy GraphQL operator.

Multi-tenancy

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

import weaviate.classes as wvc

# Connect to the collection
collectionConnection = client.collections.get("AMultiTenancyCollection")

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

# Query tenantA's version
result = connectionForTenantA.query.fetch_objects(
return_properties=["property1", "property2"],
limit=1,
)

# print (result.objects[0].properties)