Skip to main content

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).
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<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).


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)

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.
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.
Note: this method works for FloatVector/BinaryVector/SparseFloatVector, but it doesn't work for Float16Vector/BFloat16Vector.
It is recommended to use withFloatVectors/withBinaryVectors/withFloat16Vectors/withBFloat16Vectors/withSparseFloatVectors to input vectors expilicitly.

vectors:
- If target field type is FloatVector, List< List<Float>gt; is required.
- If target field type is BinaryVector, List<ByteBuffer> is required.
- If target field type is SparseFloatVector, List<SortedMap[Long, Float]> is required.

withFloatVectors(List<List<Float>gt; vectors)

Set the target vectors to search FloatVector field. Up to 16384 vectors allowed.
Note: this method will reset the target vectors of SearchParam. To input vectors, call it only once.

vectors: The target vectors

withBinaryVectors(List<ByteBuffer> vectors)

Set the target vectors to search BinaryVector field. Up to 16384 vectors allowed.
Note: this method will reset the target vectors of SearchParam. To input vectors, call it only once.

vectors: The target vectors

withFloat16Vectors(List<ByteBuffer> vectors)

Set the target vectors to search Float16Vector field. Up to 16384 vectors allowed.
Note: this method will reset the target vectors of SearchParam. To input vectors, call it only once.

vectors: The target vectors

withBFloat16Vectors(List<List<Float>gt; vectors)

Set the target vectors to search BFloat16Vector field. Up to 16384 vectors allowed.
Note: this method will reset the target vectors of SearchParam. To input vectors, call it only once.

vectors: The target vectors

withSparseFloatVectors(List<SortedMap<Long, Float>gt; vectors)

Set the target vectors to search SparseFloatVector field. Up to 16384 vectors allowed.
Note: this method will reset the target vectors of SearchParam. To input vectors, call it only once.

vectors: The target vectors

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 are 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", "search_k"
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.

withGroupByFieldName(String groupByFieldName)

Sets field name to do grouping.

groupByFieldName: The name of a field to do grouping.

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)
.withFloatVectors(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);
}
}