跳到主要内容
版本:v2.6.x

queryAsync()

Queries entities in a collection asynchronously and returns a future.

Java
public CompletableFuture<QueryResp> queryAsync(QueryReq request)

This method uses the same request parameters as query() but returns a CompletableFuture<QueryResp> immediately. Use the returned future to consume the result or handle the exceptional completion when the operation fails.

Request Syntax​

Java
CompletableFuture<QueryResp> future = client.queryAsync(QueryReq.builder()
.collectionName(String collectionName)
.filter(String filter)
.outputFields(List<String> outputFields)
.build());

For the full list of QueryReq builder methods, refer to query().

RETURN TYPE:

CompletableFuture<QueryResp>

RETURNS:

A future completed with a QueryResp object that contains query rows with the specified output fields, or completed exceptionally when the operation fails. The QueryResp object exposes the following getters:

  • getQueryResults() (List<QueryResult>) -

    The query result rows. Each QueryResult has the following getters:

    • getEntity() (Map<String, Object>) -

      The field values of the result row.

  • getSessionTs() (long) -

    The session timestamp used for the query.

EXCEPTIONS:

  • MilvusClientException

    This exception will be raised when request validation, transport, or server execution fails.

Example​

Java
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.vector.request.QueryReq;
import io.milvus.v2.service.vector.response.QueryResp;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;

ConnectConfig connectConfig = ConnectConfig.builder()
.uri("http://localhost:19530")
.token("root:Milvus")
.build();

MilvusClientV2 client = new MilvusClientV2(connectConfig);

CompletableFuture<QueryResp> future = client.queryAsync(QueryReq.builder()
.collectionName("my_collection")
.filter("age > 20")
.outputFields(Arrays.asList("name", "age"))
.build());
QueryResp response = future.get();