Basic search examples
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
- For tutorials, see Queries.
- For search using the GraphQL API, see GraphQL API.
Basic search
Use the Get
function to search.
- Python (v4)
- Python (v3)
- JavaScript/TypeScript
- GraphQL
jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects()
# for o in response.objects:
# print(json.dumps(o.properties, indent=2))
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
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
.
- Python (v4)
- Python (v3)
- JavaScript/TypeScript
- GraphQL
jeopardy = client.collections.get("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
limit=1
)
# for o in response.objects:
# print(json.dumps(o.properties, indent=2))
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
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.
- Python (v4)
- Python (v3)
- JavaScript/TypeScript
- GraphQL
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))
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
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.
- Python (v4)
- Python (v3)
- JavaScript/TypeScript
- GraphQL
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))
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
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.
- Python (v4)
- Python (v3)
- JavaScript/TypeScript
- GraphQL
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)
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
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.
- Python (v4)
- Python (v3)
- JavaScript/TypeScript
- GraphQL
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)
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
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
- Python (v4)
- Python (v3)
- JavaScript/TypeScript
- GraphQL
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)
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
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.
- Python (v4)
- Python (v3)
- JavaScript/TypeScript
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)
results = (
client.query.get("MultiTenancyClass", ["property1", "property2"])
.with_limit(1)
.with_tenant("tenantA")
.do()
result = await client
.graphql
.get()
.withClassName('MultiTenancyClass')
.withFields(['property1', 'property2'])
.withTenant('TenantA')
.do();
// console.log(JSON.stringify(result, null, 2));