searchIterator()
MilvusClient interface. This method returns a Python iterator for you to iterate over the search results. It is useful especially when the search result contains a large volume of data.
R<SearchIterator> searchIterator(SearchIteratorParam requestParam);
SearchIteratorParam
Use the SearchIteratorParam.Builder
to construct a SearchIteratorParam
object.
import io.milvus.param.dml.SearchIteratorParam;
SearchIteratorParam.Builder builder = SearchIteratorParam.newBuilder();
Methods of SearchIteratorParam.Builder
:
Methods |
Description |
Parameters |
---|---|---|
withCollectionName(collectionName) |
Set the collection name. Collection name cannot be empty or null. |
collectionName: The target collection name. |
withDatabaseName(String databaseName) |
Sets the database name. database name can be null for default database. |
databaseName: The database name. |
withConsistencyLevel(ConsistencyLevelEnum consistencyLevel) |
Sets the search consistency level(Optional). |
consistencyLevel: The consistency level used in the search. |
withPartitionNames(List<String> partitionNames) |
Sets partition names list to specify search scope (Optional). |
partitionNames: The name list of partitions to be searched. |
addPartitionName(String partitionName) |
Adds a partition to specify search scope (Optional). |
partitionName: A partition name to be searched. |
withOutFields(List<String> outFields) |
Specifies output scalar fields (Optional). |
|
addOutField(String fieldName) |
Specifies an output scalar field (Optional). |
fieldName: An output field name. |
withExpr(String expr) |
Set the expression to filter scalar fields before searching(Optional).For more information please refer to this doc. |
expr: The expression to filter scalar fields. |
withMetricType(MetricType metricType) |
Set metric type of ANN search. |
metricType: The metric type to search. |
withVectorFieldName(String vectorFieldName) |
Set target vector field by name. Field name cannot be empty or null. |
vectorFieldName: The target vector field name to do ANN search. |
withTopK(Integer topK) |
Set topK value of ANN search. |
topK: The topk value. |
withVectors(List<?> vectors) |
Set the target vectors. Up to 16384 vectors allowed. |
vectors: |
withRoundDecimal(Integer decimal) |
Specifies the decimal place for returned distance. |
decimal: How many digits are reserved after the decimal point. |
withParams(String params) |
Specifies the parameters of search in JSON format. The followings are valid keys of param: |
params: A JSON format string for extra parameters. |
withIgnoreGrowing(Boolean ignoreGrowing) |
Ignore the growing segments to get best search performance. For the user case that don't require data visibility. |
ignoreGrowing: Ignore growing segments or not. |
withBatchSize(Long batchSize) |
Specify a value to control the number of entities returned per batch. Must be a positive value. |
batchSize: A value to define the number of entities returned per batch |
build() |
Construct a SearchIteratorParam object. |
N/A |
The SearchIteratorParam.Builder.build()
can throw the following exceptions:
- ParamException: error if the parameter is invalid.
Returns
This method catches all the exceptions and returns an R<SearchIterator>
object.
If the API fails on the server side, it returns the error code and message from the server.
If the API fails by RPC exception, it returns
R.Status.Unknown
and error message of the exception.If the API succeeds, it returns a valid
SearchIterator
held by the R template.
SearchIterator
Methods of SearchIterator
:
Method |
Description |
Parameters |
Returns |
---|---|---|---|
next() |
Return a batch of results. |
N/A |
List<QueryResultsWrapper.RowRecord> |
close() |
Release the cache results. |
N/A |
N/A |
Example
import io.milvus.param.dml.*;
import io.milvus.orm.iterator.*;
import io.milvus.response.QueryResultsWrapper;
R<SearchIterator> response = milvusClient.searchIterator(SearchIteratorParam.newBuilder()
.withCollectionName(COLLECTION_NAME)
.withBatchSize(50L)
.withVectorFieldName(VECTOR_FIELD)
.withVectors(vectors)
.withParams(params)
.withMetricType(MetricType.L2)
.build());
if (response.getStatus() != R.Status.Success.getCode()) {
System.out.println(response.getMessage());
}
SearchIterator searchIterator = response.getData();
while (true) {
List<QueryResultsWrapper.RowRecord> batchResults = searchIterator.next();
if (res.isEmpty()) {
System.out.println("search iteration finished, close");
queryIterator.close();
break;
}
for (QueryResultsWrapper.RowRecord res : batchResults) {
System.out.println(res);
}
}