Object-level queries (Get)
This page covers object-level query functions. They are collectively referred to as Get queries within.
Parameters​
A Get query requires the target collection to be specified.
-
In GraphQL calls, the properties to be retrieved to be must be specified explicitly.
-
In gRPC calls, all properties are fetched by default.
-
Metadata retrieval is optional in both GraphQL and gRPC calls.
Available arguments​
Each Get query can include any of the following types of arguments:
| Argument | Description | Required |
|---|---|---|
| Collection | Also called "class". The object collection to be retrieved from. | Yes |
| Properties | Properties to be retrieved | Yes (GraphQL) (No if using gRPC API) |
| Cross-references | Cross-references to be retrieved | No |
| Metadata | Metadata (additional properties) to be retrieved | No |
| Conditional filters | Filter the objects to be retrieved | No |
| Search operators | Specify the search strategy (e.g. near text, hybrid, bm25) | No |
| Additional operators | Specify additional operators (e.g. limit, offset, sort) | No |
| Tenant name | Specify the tenant name | Yes, if multi-tenancy enabled. (Read more: what is multi-tenancy?) |
| Consistency level | Specify the consistency level | No |
Example usage​
If a snippet doesn't work or you have feedback, please open a GitHub issue.
import weaviate
import weaviate.classes as wvc
import os
client = weaviate.connect_to_local()
try:
collection = client.collections.use("JeopardyQuestion")
response = collection.query.fetch_objects()
for o in response.objects:
print(o.properties) # Inspect returned objects
finally:
client.close()
Example response
The above query will result in something like the following:
{'points': 400.0, 'answer': 'Refrigerator Car', 'air_date': '1997-02-14', 'hasCategory': 'TRANSPORTATION', 'question': 'In the 19th century Gustavus Swift developed this type of railway car to preserve his packed meat', 'round': 'Jeopardy!'}
{'points': 800.0, 'hasCategory': 'FICTIONAL CHARACTERS', 'answer': 'Forsyte', 'air_date': '1998-05-27', 'question': 'Last name of Soames & Irene, the 2 principal characters in John Galsworthy\'s 3 novel "saga"', 'round': 'Double Jeopardy!'}
{'points': 500.0, 'answer': 'Duluth', 'air_date': '1996-12-17', 'hasCategory': 'MUSEUMS', 'question': 'This eastern Minnesota city is home to the Lake Superior Museum of Transportation', 'round': 'Jeopardy!'}
{'points': 1000.0, 'answer': 'Ear', 'air_date': '1988-11-16', 'hasCategory': 'HISTORY', 'round': 'Double Jeopardy!', 'question': "An eighteenth-century war was named for this part of Robert Jenkins' body, reputedly cut off by Spaniards"}
{'points': 400.0, 'answer': 'Bonnie Blair', 'air_date': '1997-02-28', 'hasCategory': 'SPORTS', 'round': 'Jeopardy!', 'question': "At the 1994 Olympics, this U.S. woman speed skater surpassed Eric Heiden's medal total"}
{'points': 1600.0, 'answer': 'Turkish', 'air_date': '2008-03-24', 'hasCategory': 'LANGUAGES', 'question': 'In the 1920s this language of Anatolia switched from the Arabic to the Latin alphabet', 'round': 'Double Jeopardy!'}
{'points': 100.0, 'answer': 'Ireland', 'air_date': '1998-10-01', 'hasCategory': 'POTPOURRI', 'round': 'Jeopardy!', 'question': "Country in which you'd find the Book of Kells"}
{'points': 800.0, 'answer': 'Ichabod Crane', 'air_date': '2008-01-03', 'hasCategory': 'LITERATURE', 'round': 'Double Jeopardy!', 'question': 'Washington Irving based this character on his friend Jesse Merwin, a schoolteacher'}
{'points': 300.0, 'air_date': '1997-12-05', 'hasCategory': 'LITERATURE', 'answer': '"The Prince and the Pauper"', 'question': 'Tom Canty, born in a slum called Offal Court, & Edward Tudor are the title characters in this Twain novel', 'round': 'Jeopardy!'}
{'points': 500.0, 'answer': 'Seattle', 'air_date': '1999-05-10', 'hasCategory': 'U.S. CITIES', 'round': 'Jeopardy!', 'question': "The site of the World's Fair in 1962, it's flanked on the west by Puget Sound & on the east by Lake Washington"}
Order of retrieved objects
Without any arguments, the objects are retrieved according to their ID.
Accordingly, such a Get query is not suitable for a substantive object retrieval strategy. Consider the Cursor API for that purpose.
Get groupBy​
You can use retrieve groups of objects that match the query.
The groups are defined by a property, and the number of groups and objects per group can be limited.
groupBy limitationsgroupByonly works withnear<Media>operators.- The
groupBypathis limited to one property or cross-reference. Nested paths are not supported.
Syntax​
{
Get{
<Class>(
<vectorSearchOperator> # e.g. nearVector, nearObject, nearText
groupBy:{
path: [<propertyName>] # Property to group by (only one property or cross-reference)
groups: <number> # Max. number of groups
objectsPerGroup: <number> # Max. number of objects per group
}
) {
_additional {
group {
id # An identifier for the group in this search
groupedBy{ value path } # Value and path of the property grouped by
count # Count of objects in this group
maxDistance # Maximum distance from the group to the query vector
minDistance # Minimum distance from the group to the query vector
hits { # Where the actual properties for each grouped objects will be
<properties> # Properties of the individual object
_additional {
id # UUID of the individual object
vector # The vector of the individual object
distance # The distance from the individual object to the query vector
}
}
}
}
}
}
}
Example usage:​
If a snippet doesn't work or you have feedback, please open a GitHub issue.
questions = client.collections.use("JeopardyQuestion")
response = questions.query.near_text(
query="animals",
group_by=wvc.query.GroupBy(
prop="points",
number_of_groups=3,
objects_per_group=5
)
)
for k, v in response.groups.items(): # View by group
print(k, v)
for o in response.objects: # View by object
print(o)
Consistency levels​
v1.19Where replication is enabled, you can specify a consistency argument with a Get query. The available options are:
ONEQUORUM(Default)ALL
Read more about consistency levels here.
If a snippet doesn't work or you have feedback, please open a GitHub issue.
import weaviate
import weaviate.classes as wvc
import os
client = weaviate.connect_to_local()
try:
questions = client.collections.use("JeopardyQuestion").with_consistency_level(consistency_level=wvc.config.ConsistencyLevel.QUORUM)
response = collection.query.fetch_objects()
for o in response.objects:
print(o.properties) # Inspect returned objects
finally:
client.close()
Multi-tenancy​
v1.20In a multi-tenancy collection, each Get query must specify a tenant.
If a snippet doesn't work or you have feedback, please open a GitHub issue.
multi_collection = client.collections.use("MultiTenancyCollection")
# Get collection specific to the required tenant
multi_tenantA = multi_collection.with_tenant("tenantA")
# Query tenantA
result = multi_tenantA.query.fetch_objects(
limit=2,
)
print(result.objects[0].properties)
Cross-references​
Queries involving cross-references can be slower than queries that do not involve cross-references, especially at scale such as for multiple objects or complex queries.
At the first instance, we strongly encourage you to consider whether you can avoid using cross-references in your data schema. As a scalable AI database, Weaviate is well-placed to perform complex queries with vector, keyword and hybrid searches involving filters. You may benefit from rethinking your data schema to avoid cross-references where possible.
For example, instead of creating separate "Author" and "Book" collections with cross-references, consider embedding author information directly in Book objects and using searches and filters to find books by author characteristics.
Weaviate supports cross-references between objects. Each cross-reference behaves like a property.
You can retrieve cross-referenced properties with a Get query.
If a snippet doesn't work or you have feedback, please open a GitHub issue.
questions = client.collections.use("JeopardyQuestion")
response = questions.query.fetch_objects(
return_references=wvc.query.QueryReference(
link_on="hasCategory",
return_properties=["title"]
)
)
for o in response.objects:
print(f"References for {o.uuid}")
for ro in o.references["hasCategory"].objects: # Inspect returned references
print(ro.properties)
Expected response
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"answer": "Jonah",
"hasCategory": [
{
"title": "THE BIBLE"
}
],
"points": 100,
"question": "This prophet passed the time he spent inside a fish offering up prayers"
},
// shortened for brevity
]
}
}
}
Additional properties / metadata​
Various metadata properties may be retrieved with Get{} requests. They include:
| Property | Description |
|---|---|
id | Object id |
vector | Object vector |
generate | Generative module outputs |
rerank | Reranker module outputs |
creationTimeUnix | Object creation time |
lastUpdateTimeUnix | Object last updated time |
distance | Vector distance to query (vector search only) |
certainty | Vector distance to query, normalized to certainty (vector search only) |
score | Search score (BM25 and hybrid only) |
explainScore | Explanation of the score (BM25 and hybrid only) |
classification | Classification outputs |
featureProjection | Feature projection outputs |
They are returned through the _additional properties in the response.
For further information see:
Search operators​
The following search operators are available.
| Argument | Description | Required integration type | Learn more |
|---|---|---|---|
nearObject | Vector search using a Weaviate object | none | Learn more |
nearVector | Vector search using a raw vector | none | Learn more |
nearText | Vector search using a text query | Text embedding model | |
nearImage | Vector search using an image | Multi-modal embedding model | |
hybrid | Combine vector and BM25 search results | none | Learn more |
bm25 | Keyword search with BM25F ranking | none | Learn more |
For further information see:
Conditional filters​
Get{} queries can be combined with a conditional filter.
For further information see:
Additional operators​
Get{} queries can be combined with additional operators such as limit, offset, autocut, after or sort.
For further information see:
Related pages​
Questions and feedback​
If you have any questions or feedback, let us know in the user forum.
