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

searchAsync()

Performs a vector search asynchronously and returns a future.

Java
public CompletableFuture<SearchResp> searchAsync(SearchReq request)

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

Request Syntax​

Java
CompletableFuture<SearchResp> future = client.searchAsync(SearchReq.builder()
.collectionName(String collectionName)
.data(List<BaseVector> data)
.annsField(String annsField)
.limit(long limit)
.build());

For the full list of SearchReq builder methods, refer to search().

RETURN TYPE:

CompletableFuture<SearchResp>

RETURNS:

A future completed with a SearchResp object that contains search results, recalls, cost, scanned byte counts, and cache hit ratio, or completed exceptionally when the operation fails. The SearchResp object exposes the following getters:

  • getSearchResults() (List<List<SearchResult>>) -

    The search results, one list per query vector. Each SearchResult has the following getters:

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

      The retrieved entity data.

    • getScore() (Float) -

      The similarity score of the result.

    • getId() (Object) -

      The primary key value of the result.

    • getPrimaryKey() (String) -

      The primary key value rendered as a string.

  • getSessionTs() (long) -

    The session timestamp used for the search.

  • getRecalls() (List<Float>) -

    The recall values of the search, one per query vector.

  • getCost() (Long) -

    The time cost of the search operation, in milliseconds.

  • getScannedRemoteBytes() (Long) -

    The number of bytes scanned from remote storage.

  • getScannedTotalBytes() (Long) -

    The total number of bytes scanned during the search.

  • getCacheHitRatio() (Float) -

    The cache hit ratio of the search.

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.SearchReq;
import io.milvus.v2.service.vector.request.data.FloatVec;
import io.milvus.v2.service.vector.response.SearchResp;

import java.util.Collections;
import java.util.concurrent.CompletableFuture;

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

MilvusClientV2 client = new MilvusClientV2(connectConfig);

CompletableFuture<SearchResp> future = client.searchAsync(SearchReq.builder()
.collectionName("my_collection")
.data(Collections.singletonList(new FloatVec(new float[]{0.1f, 0.2f, 0.3f})))
.annsField("vector")
.limit(10)
.build());
SearchResp response = future.get();