milvus-logo
Star
0
Forks
0
快速开始

Conduct a Vector Query

Milvus Docs 需要你的帮助

本文档暂时没有中文版本,欢迎你成为社区贡献者,协助中文技术文档的翻译。
你可以通过页面右边的 编辑 按钮直接贡献你的翻译。更多详情,参考 贡献指南。如需帮助,你可以 提交 GitHub Issue

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.collectionManager.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());
load -c book
In current release, volume of the data to load must be under 70% of the total memory resources of all query nodes to reserve memory resources for execution engine.

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.

res = collection.query(expr = "book_id in [2,4,6,8]", output_fields = ["book_id", "book_intro"])
const results = await milvusClient.dataManager.query({
  collection_name: "book",
  expr: "book_id in [2,4,6,8]",
  output_fields: ["book_id", "book_intro"],
});
// This function is under active development on the GO client.
List<String> query_output_fields = Arrays.asList("book_id", "word_count");
QueryParam queryParam = QueryParam.newBuilder()
        .withCollectionName("book")
        .withExpr("book_id in [2,4,6,8]")
        .withOutFields(query_output_fields)
        .build();
R<QueryResults> respQuery = milvusClient.query(queryParam);
query

collection_name: book

The query expression: book_id in [2,4,6,8]

Name of partitions that contain entities(split by "," if multiple) []:

A list of fields to return(split by "," if multiple) []: book_id, book_intro

timeout []:
Parameter Description
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.
partition_names (optional) List of names of the partitions to query on.
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.
partition_names (optional) List of names of the partitions to query on.
Parameter Description Options
ctx Context to control API invocation process. N/A
CollectionName Name of the collection to query. N/A
partitionNames 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.
output_fields Name of the field to return. Vector field is not supported in current release.
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.
Option Full name Description
--help n/a Displays help for using the command.

Check the returned results.

sorted_res = sorted(res, key=lambda k: k['book_id'])
sorted_res
console.log(results.data)
// This function is under active development on the GO client.
QueryResultsWrapper wrapperQuery = new QueryResultsWrapper(respQuery.getData());
System.out.println(wrapperQuery.getFieldWrapper("book_id").getFieldData());
System.out.println(wrapperQuery.getFieldWrapper("word_count").getFieldData());
# Milvus CLI automatically returns the entities with the pre-defined output fields.

What's next

    该页面是否对你有帮助?
    本页目录