milvus-logo
LFAI
< Docs
  • Java
    • v1

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.
withConsistencyLevel(ConsistencyLevelEnum consistencyLevel) Sets the search consistency level(Optional).
If the level is not set, will use the default consistency level of the collection.
consistencyLevel: The consistency level used in the search.
withPartitionNames(List\ 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\ outFields) Specifies output scalar fields (Optional).
outFields: The name list of fields to be outputed.
addOutField(String fieldName) Specifies an output scalar field (Optional). fieldName: An output field name.
withExpr(String expr) expr: The expression to filter scalar fields.
withMetricType(MetricType metricType) Set metric type of ANN search.
Default value is MetricType.None, which means let the server determine the defaul metric type. Please refer to MetricType in Misc.
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.
Avaiable range: [1, 16384]
topK: The topk value.
withVectors(List\ vectors) Set the target vectors. Up to 16384 vectors allowed. vectors:
- If target field type is FloatVector, List\< List\gt; is required.
- If target field type is BinaryVector vector, List\ is required.
withRoundDecimal(Integer decimal) Specifies the decimal place for returned distance.
Avaiable range: [-1, 6]
Default value is -1, return all digits.
decimal: How many digits reserved after the decimal point.
withParams(String params) Specifies the parameters of search in JSON format. The followings are valid keys of param:
1. special parameters for index, such as "nprobe", "ef", "searchk"
2. metric type with key "metric
type" and a string value such as "L2", "IP".
3. offset for pagination with key "offset" and an integer value
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.
Default is False.
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.
Default value is 1000, will return without batchSize.
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\
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);
    }
}

Feedback

Was this page helpful?