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โ
- Python
- JavaScript/TypeScript
- GraphQL
response = (
client.query
.get("JeopardyQuestion", ["question"])
.do()
)
print(response)
result = await client
.graphql
.get()
.withClassName('JeopardyQuestion')
.withFields('question')
.do();
console.log(JSON.stringify(result, null, 2));
{
Get {
JeopardyQuestion {
question
}
}
}
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 != searchThe 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.
- Python
- JavaScript/TypeScript
- GraphQL
response = (
client.query
.get("JeopardyQuestion", ["question"])
.with_limit(1)
.do()
)
print(response)
result = await client
.graphql
.get()
.withClassName('JeopardyQuestion')
.withFields('question')
.withLimit(1)
.do();
console.log(JSON.stringify(result, null, 2));
{
Get {
JeopardyQuestion (
limit: 1
) {
question
}
}
}
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.
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.
- Python
- JavaScript/TypeScript
- GraphQL
response = (
client.query
.get("JeopardyQuestion", ["question"])
.with_limit(1)
.with_offset(1)
.do()
)
print(response)
result = await client
.graphql
.get()
.withClassName('JeopardyQuestion')
.withFields('question')
.withLimit(1)
.withOffset(1)
.do();
console.log(JSON.stringify(result, null, 2));
{
Get {
JeopardyQuestion (
limit: 1
offset: 1
) {
question
}
}
}
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.
- Python
- JavaScript/TypeScript
- GraphQL
response = (
client.query
.get("JeopardyQuestion", ["question", "answer", "points"])
.with_limit(1)
.do()
)
print(response)
result = await client
.graphql
.get()
.withClassName('JeopardyQuestion')
.withFields('question answer points')
.withLimit(1)
.do();
console.log(JSON.stringify(result, null, 2));
{
Get {
JeopardyQuestion (limit: 1) {
question
answer
points
}
}
}
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.
- Python
- JavaScript/TypeScript
- GraphQL
response = (
client.query
.get("JeopardyQuestion")
.with_additional("vector")
.with_limit(1)
.do()
)
print(response)
result = await client
.graphql
.get()
.withClassName('JeopardyQuestion')
.withFields('_additional {vector}')
.withLimit(1)
.do();
console.log(JSON.stringify(result, null, 2));
{
Get {
JeopardyQuestion (limit: 1) {
_additional {
vector
}
}
}
}
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.
- Python
- JavaScript/TypeScript
- GraphQL
response = (
client.query
.get("JeopardyQuestion")
.with_additional("id")
.with_limit(1)
.do()
)
print(response)
result = await client
.graphql
.get()
.withClassName('JeopardyQuestion')
.withFields('_additional { id }')
.withLimit(1)
.do();
console.log(JSON.stringify(result, null, 2));
{
Get {
JeopardyQuestion (limit: 1) {
_additional {
id
}
}
}
}
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.
- Python
- TypeScript
- GraphQL
response = (
client.query
.get("JeopardyQuestion", [
"question",
"hasCategory { ... on JeopardyCategory { title } }"
])
.with_limit(2)
.do()
)
print(json.dumps(response, indent=2))
result = await client.graphql
.get()
.withClassName('JeopardyQuestion')
.withFields(`
question
hasCategory {
... on JeopardyCategory {
title
}
}`)
.withLimit(2)
.do();
console.log(JSON.stringify(result, null, 2));
{
Get {
JeopardyQuestion (
limit: 2
)
{
question
hasCategory {
... on JeopardyCategory {
title
}
}
}
}
}
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:
- Frequently Asked Questions. Or,
- Knowledge base of old issues. Or,
- For questions: Stackoverflow. Or,
- For more involved discussion: Weaviate Community Forum. Or,
- We also have a Slack channel.