Conduct a Vector Query
This topic describes how to conduct a vector query.
Unlike a vector similarity search, a vector query retrieves vectors via scalar filtering based on boolean expression. Milvus supports many data types in the scalar fields and a variety of boolean expressions. The boolean expression filters on scalar fields or the primary key field, and it retrieves all results that match the filters.
The following example shows how to perform a vector query on a 2000-row dataset of book ID (primary key), word count (scalar field), and book introduction (vector field), simulating the situation where you query for certain books based on their IDs.
Load collection
All search and query operations within Milvus are executed in memory. Load the collection to memory before conducting a vector query.
from pymilvus import Collection
collection = Collection("book") # Get an existing collection.
collection.load()
await milvusClient.loadCollection({
collection_name: "book",
});
err := milvusClient.LoadCollection(
context.Background(), // ctx
"book", // CollectionName
false // async
)
if err != nil {
log.Fatal("failed to load collection:", err.Error())
}
milvusClient.loadCollection(
LoadCollectionParam.newBuilder()
.withCollectionName("book")
.build()
);
var collection = milvusClient.GetCollection("book").LoadAsync();
Conduct a vector query
The following example filters the vectors with certain book_id
values, and returns the book_id
field and book_intro
of the results.
Milvus supports setting consistency level specifically for a query. The example in this topic sets the consistency level as Strong
. You can also set the consistency level as Bounded
, Session
or Eventually
. See Consistency for more information about the four consistency levels in Milvus.
You can also use dynamic fields in the filter expression and output fields in the query requests. For example, refer to Dynamic Schema.
res = collection.query(
expr = "book_id in [2,4,6,8]",
offset = 0,
limit = 10,
output_fields = ["book_id", "book_intro"],
)
const results = await milvusClient.query({
collection_name: "book",
expr: "book_id in [2,4,6,8]",
output_fields: ["book_id", "book_intro"],
limit: 10,
offset: 0,
});
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("fail to query collection:", err.Error())
}
List<String> query_output_fields = Arrays.asList("book_id", "word_count");
QueryParam queryParam = QueryParam.newBuilder()
.withCollectionName("book")
.withConsistencyLevel(ConsistencyLevelEnum.STRONG)
.withExpr("book_id in [2,4,6,8]")
.withOutFields(query_output_fields)
.withOffset(0L)
.withLimit(10L)
.build();
R<QueryResults> respQuery = milvusClient.query(queryParam);
var results = await Client.GetCollection("book").QueryAsync(
expression: "book_id in [2,4,6,8]",
new QueryParameters
{
Offset = 0,
Limit = 10,
OutputFields = { "book_id", "book_intro" }
});
curl --request POST \
--url '${MILVUS_HOST}:${MILVUS_PORT}/v1/vector/query' \
--header 'Authorization: Bearer <TOKEN>' \
--header 'accept: application/json' \
--header 'content-type: application/json'
-d '{
"collectionName": "collection1",
"outputFields": ["id", "name", "feature", "distance"],
"filter": "id in (1, 2, 3)",
"limit": 100,
"offset": 0
}'
{
"code": 200,
"data": {}
}
Parameter | Description |
---|---|
expr |
Boolean expression used to filter attribute. Find more expression details in Boolean Expression Rules. |
limit |
Number of the most similar results to return. The sum of this value and offset should be less than 16384. |
offset |
Number of results to skip in the returned set. This parameter is available only when limit is specified, and the sum of this value and limit should be less than 16384. |
output_fields (optional) |
List of names of the field to return. |
partition_names (optional) |
List of names of the partitions to query on. |
consistency_level (optional) |
Consistency level of the query. |
Parameter | Description |
---|---|
collection_name |
Name of the collection to query. |
expr |
Boolean expression used to filter attribute. Find more expression details in Boolean Expression Rules. |
output_fields (optional) |
List of names of the field to return. |
limit (optional) |
Number of the most similar results to return. The sum of this value and offset should be less than 16384. |
offset (optional) |
Number of results to skip in the returned set. This parameter is available only when limit is specified, and the sum of this value and limit should be less than 16384. |
Parameter | Description | Options |
---|---|---|
ctx |
Context to control API invocation process. | N/A |
CollectionName |
Name of the collection to query. | N/A |
partitionName |
List of names of the partitions to load. All partitions will be queried if it is left empty. | N/A |
expr |
Boolean expression used to filter attribute. | See Boolean Expression Rules for more information. |
OutputFields |
Name of the field to return. | Vector field is not supported in current release. |
opts |
Query options in the form of entity.SearchQueryOptionFunc . |
|
Parameter | Description | Options |
---|---|---|
CollectionName |
Name of the collection to load. | N/A |
OutFields |
Name of the field to return. | Vector field is not supported in current release. |
Expr |
Boolean expression used to filter attribute. | See Boolean Expression Rules for more information. |
Limit (optional) |
Number of the most similar results to return. The sum of this value and offset in WithOffset() should be less than 16384. |
|
Offset (optional) |
Number of results to skip in the returned set. This parameter is available only when limit is specified, and the sum of this value and limit in WithLimit() should be less than 16384. |
|
ConsistencyLevel |
The consistency level used in the query. | STRONG , BOUNDED , andEVENTUALLY . |
Parameter | Description |
---|---|
expression | Boolean expression used to filter attribute. See Boolean Expression Rules for more information. |
parameters | Query parameters. Possible options are:
|
Parameter | Description |
---|---|
collectionName |
(Required) The name of the collection to which this operation applies. |
filter |
The filter used to find matches for the search |
limit |
The maximum number of entities to return. The sum of this value of that of `offset` should be less than **1024**. The value defaults to 100 .The value ranges from 1 to 100 |
offset |
The number of entities to skip in the search results. The sum of this value and that of `limit` should not be greater than 1024 .The maximum value is 1024 . |
outputFields |
An array of fields to return along with the search results. |
Check the returned results.
sorted_res = sorted(res, key=lambda k: k['book_id'])
sorted_res
console.log(results.data)
fmt.Printf("%#v\n", queryResult)
for _, qr := range queryResult {
fmt.Println(qr.IDs)
}
QueryResultsWrapper wrapperQuery = new QueryResultsWrapper(respQuery.getData());
System.out.println(wrapperQuery.getFieldWrapper("book_id").getFieldData());
System.out.println(wrapperQuery.getFieldWrapper("word_count").getFieldData());
What’s next
Learn more basic operations of Milvus:
Explore API references for Milvus SDKs: