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().
RETURN TYPE:
CompletableFuture
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- >
The search results, one list per query vector. Each SearchResult has the following getters:
getEntity()(Map) - 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.
getHighlightResults()(Map) - Highlighting results keyed by field name.
getHighlightResult(String fieldName)(HighlightResult) -The highlighting result for one requested field, or
nullwhen no result exists.getElementOffset()(Long) -The element offset for array-of-struct subfield search.
nullfor ordinary searches.
getSessionTs()(long) -The session timestamp used for the search.
Each
HighlightResultexposes the following getters:getFieldName()(String) -The field name used for highlighting.
getFragments()(List) - The highlighted text fragments.
getScores()(List) - The relevance scores for the fragments.
getRecalls()(List) - 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:
-
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.common.DataType;
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.collection.request.AddFieldReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.request.LoadCollectionReq;
import io.milvus.v2.service.vector.request.InsertReq;
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 com.google.gson.Gson;
import com.google.gson.JsonObject;
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);
// Create a collection with an "id" field and a "vector" field with dimension 3
CreateCollectionReq.CollectionSchema collectionSchema = MilvusClientV2.CreateSchema();
collectionSchema.addField(AddFieldReq.builder().fieldName("id").dataType(DataType.Int64).isPrimaryKey(Boolean.TRUE).autoID(Boolean.FALSE).build());
collectionSchema.addField(AddFieldReq.builder().fieldName("vector").dataType(DataType.FloatVector).dimension(3).build());
IndexParam indexParam = IndexParam.builder()
.fieldName("vector")
.metricType(IndexParam.MetricType.COSINE)
.build();
client.createCollection(CreateCollectionReq.builder()
.collectionName("my_collection")
.collectionSchema(collectionSchema)
.indexParams(Collections.singletonList(indexParam))
.build());
// Insert a row
JsonObject row = new JsonObject();
row.addProperty("id", 0L);
row.add("vector", new Gson().toJsonTree(new float[]{0.1f, 0.2f, 0.3f}));
client.insert(InsertReq.builder()
.collectionName("my_collection")
.data(Collections.singletonList(row))
.build());
// Load the collection
client.loadCollection(LoadCollectionReq.builder().collectionName("my_collection").build());
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();