searchAsync()
Performs a vector search asynchronously and returns a future.
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
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().
RETURNS:
CompletableFuture
A future completed with a SearchResp, or completed exceptionally when the operation fails.
EXCEPTIONS:
MilvusClientException
This exception will be raised when request validation, transport, or server execution fails.
Example
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();