queryIterator()
This operation returns a query iterator to iterate data.
public QueryIterator queryIterator(QueryIteratorReq request)
Request Syntax
queryIterator(QueryIteratorReq.builder()
.databaseName(String databaseName)
.collectionName(String collectionName)
.partitionNames(List<String> partitionNames)
.outputFields(List<String> outputFields)
.expr(String expr)
.consistencyLevel(ConsistencyLevel consistencyLevel)
.offset(long offset)
.limit(long limit)
.ignoreGrowing(boolean ignoreGrowing)
.timezone(String timezone)
.batchSize(long batchSize)
.reduceStopForBest(boolean reduceStopForBest)
.cursor(QueryIteratorCursor cursor)
.filterTemplateValues(Map<String, Object> filterTemplateValues)
.build()
);
BUILDER METHODS:
-
databaseName(String databaseName)The name of the database. Defaults to the current database if not specified.
-
collectionName(String collectionName)The name of the target collection.
-
partitionNames(List<String> partitionNames)A list of partition names to target.
-
outputFields(List<String> outputFields)A list of field names to include in the output.
-
expr(String expr)A boolean expression to filter results.
-
consistencyLevel(ConsistencyLevel consistencyLevel)The consistency level for the operation.
-
offset(long offset)The number of results to skip before returning.
-
limit(long limit)The maximum number of results to return.
-
ignoreGrowing(boolean ignoreGrowing)Whether to ignore growing segments during the operation.
-
timezone(String timezone)The timezone string for time-related filters.
-
batchSize(long batchSize)The batch size for iterator operations.
-
reduceStopForBest(boolean reduceStopForBest)Whether to stop iteration when the best result is found.
-
cursor(QueryIteratorCursor cursor)A previously captured cursor to resume pagination from. When set, the iterator continues from the cursor's session timestamp and position instead of starting over;
offset()is ignored in that case. -
filterTemplateValues(Map<String, Object> filterTemplateValues)A map of template variable values for parameterized filters.
RETURN TYPE:
QueryIterator
RETURNS:
A QueryIterator object to iterate query results page by page, which offers the following methods:
PARAMETERS:
-
next() (List<QueryResultsWrapper.RowRecord>) -
Returns the next batch of rows matching the query expression.
-
close() -
Releases the iterator and the resources held by its cursor.
-
getCursor() (QueryIteratorCursor) -
Returns the cursor that holds the iteration progress.
EXCEPTIONS:
-
MilvusClientException
This exception will be raised when any error occurs during this operation.
Example
import io.milvus.orm.iterator.QueryIterator;
import io.milvus.response.QueryResultsWrapper;
import io.milvus.v2.service.vector.request.QueryIteratorReq;
import io.milvus.v2.common.ConsistencyLevel;
import java.util.Arrays;
import java.util.List;
// Create a query iterator to retrieve results in batches
QueryIterator queryIterator = client.queryIterator(QueryIteratorReq.builder()
.collectionName("my_collection")
.expr("userID < 3000")
.outputFields(Arrays.asList("userID", "userAge"))
.batchSize(100)
.offset(0)
.limit(10000)
.consistencyLevel(ConsistencyLevel.BOUNDED)
.build());
// Iterate through all results
int counter = 0;
while (true) {
List<QueryResultsWrapper.RowRecord> res = queryIterator.next();
if (res.isEmpty()) {
queryIterator.close();
break;
}
for (QueryResultsWrapper.RowRecord record : res) {
System.out.println(record);
counter++;
}
}
System.out.printf("%d query results returned%n", counter);