milvus-logo
LFAI
< Docs
  • Java
    • v1

queryIterator()

MilvusClient interface. This method returns an iterator for you to iterate over the query results. It is useful especially when the query result contains a large volume of data.

R<QueryIterator> queryIterator(QueryIteratorParam requestParam);

QueryIteratorParam

Use the QueryIteratorParam.Builder to construct a QueryIteratorParam object.

import io.milvus.param.dml.QueryIteratorParam;
QueryIteratorParam.Builder builder = QueryIteratorParam.newBuilder();

Methods of QueryIteratorParam.Builder:

Method 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 query.
withPartitionNames(List\ partitionNames) Sets partition names list to specify query scope (Optional). partitionNames: The name list of partitions to be queried.
addPartitionName(String partitionName) Adds a partition to specify query scope (Optional). partitionName: A partition name to be queried.
withOutFields(List\ outFields) Specifies output scalar fields (Optional).
If output fields are specified, the QueryResults returned by query() will contains the values of these fields.

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 query entities. For more information please refer to this doc. expr: The expression to query
withOffset(Long offset) Specify a position, the returned entities before this position will be ignored. Only take effect when the 'limit' value is specified. Default value is 0, start from begin. offset: A value to define the position.
withLimit(Long limit) Specify a value to control the returned number of entities. Must be a positive value. Default value is -1, will return without limit. limit: A value to define the limit of returned entities.
withIgnoreGrowing(Boolean ignoreGrowing) Ignore the growing segments to get best query performance. For the user case that don't require data visibility. Default value 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 QueryIteratorParam object. N/A

The QueryIteratorParam.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<QueryIterator> 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 QueryIterator held by the R template.

QueryIterator

Methods of QueryIterator:

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<QueryIterator> response = milvusClient.queryIterator(QueryIteratorParam.newBuilder()
        .withCollectionName(COLLECTION_NAME)
        .withExpr(expr)
        .withBatchSize(100L)
        .build());
if (response.getStatus() != R.Status.Success.getCode()) {
    System.out.println(response.getMessage());
}

QueryIterator queryIterator = response.getData();
while (true) {
    List<QueryResultsWrapper.RowRecord> batchResults = queryIterator.next();
    if (res.isEmpty()) {
        System.out.println("query iteration finished, close");
        queryIterator.close();
        break;
    }
    for (QueryResultsWrapper.RowRecord res : batchResults) {
        System.out.println(res);
    }
}