Skip to main content

Search basics

Overviewโ€‹

This section includes the essentials of performing searches and retrieving objects with the Get function.

Get function requirementsโ€‹

To retrieve objects from Weaviate, you must use the Get function and specify at least:

  • The target class to search, and
  • One or more properties to retrieve.

Simple Get exampleโ€‹

response = (
client.query
.get("JeopardyQuestion", ["question"])
.do()
)

print(response)
Example response

It should produce a response like the one below:

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

The objects endpoint in Weaviate is designed for CRUD operations and not capable of performing searches.

limit returned objectsโ€‹

Often, you will only want the top n results from the query. This can be achieved by setting a limit as shown below.

response = (
client.query
.get("JeopardyQuestion", ["question"])
.with_limit(1)
.do()
)

print(response)
Example response

It should produce a response like the one below:

{
"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โ€‹

If you only want the n results after the first m results from the query, you can do this with limit and offset as shown below.

Be aware that although you will only see n results, this could become an expensive operation as m grows larger, as Weaviate must fetch n+m results.

For exhaustive retrieval, use after instead.

If you want to list and retrieve all objects from a class, use the cursor API instead with the after parameter. Read this guide for more information on how.

response = (
client.query
.get("JeopardyQuestion", ["question"])
.with_limit(1)
.with_offset(1)
.do()
)
print(response)
Example response

It should produce a response like the one below:

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

Specify the fetched propertiesโ€‹

You can specify the properties to be fetched, as long as one or more are specified.

Object propertiesโ€‹

You can specify object properties as below.

response = (
client.query
.get("JeopardyQuestion", ["question", "answer", "points"])
.with_limit(1)
.do()
)
print(response)
Example response

It should produce a response like the one below:

{
"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, request the _additional property and vector sub-property. You can do so as shown below.

response = (
client.query
.get("JeopardyQuestion")
.with_additional("vector")
.with_limit(1)
.do()
)
print(response)
Example response

It should produce a response like the one below:

{
"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. You can do so as shown below.

response = (
client.query
.get("JeopardyQuestion")
.with_additional("id")
.with_limit(1)
.do()
)
print(response)
Example response

It should produce a response like the one below:

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

Retrieve cross-referenced propertiesโ€‹

You can retrieve any properties of cross-referenced objects by specifying:

  • The cross-reference property,
  • The target cross-referenced object class, and
  • The desired properties to retrieve (of the cross-referenced objects).

The following example, retrieves for each JeopardyQuestion object the cross-referenced JeopardyCategory object, and the JeopardyCategory object's title property is returned. The property is accessed using the inline fragment GraphQL syntax.

response = (
client.query
.get("JeopardyQuestion", [
"question",
"hasCategory { ... on JeopardyCategory { title } }"
])
.with_limit(2)
.do()
)

print(json.dumps(response, indent=2))
Example response

It should produce a response like the one below:

{
"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"
}
]
}
}
}

More Resourcesโ€‹

If you can't find the answer to your question here, please look at the:

  1. Frequently Asked Questions. Or,
  2. Knowledge base of old issues. Or,
  3. For questions: Stackoverflow. Or,
  4. For more involved discussion: Weaviate Community Forum. Or,
  5. We also have a Slack channel.