Performing Conditional Queries
This topic introduces how to perform conditional queries, similar to SQL WHERE conditions.
Unlike vector similarity search, queries retrieve vectors based on scalar filtering using boolean expressions. Milvus supports various data types in scalar fields and a variety of boolean expressions. Boolean expressions can filter scalar fields or primary key fields and retrieve all results matching the filter conditions.
The following example demonstrates how to execute a query on a dataset containing 2000 rows, including book ID (primary key), word count (scalar field), and book introduction (vector field). It simulates querying for specific books based on their IDs.
Loading the Collection
In Milvus, all search and query operations are performed in memory. Before performing a query, the collection needs to be loaded into memory.
err := milvusClient.LoadCollection(
context.Background(), // ctx
"book", // CollectionName
false // async
)
if err != nil {
log.Fatal("Failed to load collection:", err.Error())
}
Executing the Query
The following example filters vectors based on specific book_id
values and returns the book_id
and book_intro
fields of the results.
Milvus supports setting consistency levels for queries. This example sets the consistency level to Strong
. You can also set the consistency level to Bounded
, Session
, or Eventually
. For more information on the four consistency levels in Milvus, refer to Consistency.
You can also use dynamic fields in the filter expressions and output fields in the query request. For example, refer to the dynamic pattern.
opt := client.SearchQueryOptionFunc(func(option *client.SearchQueryOption) {
option.Limit = 3
option.Offset = 0
option.ConsistencyLevel = entity.ClStrong
option.IgnoreGrowing = false
})
queryResult, err := milvusClient.Query(
context.Background(), // ctx
"book", // CollectionName
"", // PartitionName
entity.NewColumnInt64("book_id", []int64{2,4,6,8}), // expr
[]string{"book_id", "book_intro"}, // OutputFields
opt, // queryOptions
)
if err != nil {
log.Fatal("Failed to query the collection:", err.Error())
}
Parameter | Description | Options |
---|---|---|
ctx |
Context used to control the API call process. | N/A |
CollectionName |
Name of the collection to be queried. | N/A |
partitionName |
Names of the partitions to be loaded. If empty, query all partitions. | N/A |
expr |
Boolean expression used to filter attributes. | For more information, see Boolean Expression Rules. |
OutputFields |
Names of fields to be returned. | Vector fields are not supported in the current version. |
opts |
Query options represented in the form of entity.SearchQueryOptionFunc . |
- Limit indicates the number of entities to return. - Offset indicates the number of entities to skip during the search process. The sum of this parameter and Limit should be less than 16384 . - ConsistencyLevel indicates the consistency level to be applied during the search process. - Ignore Growing indicates whether to ignore the growing segment during the similarity search process. The default value is False , indicating that the search involves the growing segment. |
Check the returned result.
fmt.Printf("%#v\n", queryResult)
for _, qr := range queryResult {
fmt.Println(qr.IDs)
}
Calculating Entity Quantity
When executing a query, you can append count(*)
to output_fields
, so that Milvus can return the quantity of entities in the collection. If you want to count the quantity of entities that meet specific conditions, use expr
to define a boolean expression.
Restrictions
When using count(*)
in output_fields
, the use of the limit
parameter is not allowed.