Skip to main content

Session: Querying: How to stream query results?

Query results can be streamed using Stream method from Advanced session operations.

Syntax

// Define a query on a collection
IRavenQueryable<Employee> query = session
.Query<Employee>()
.Where(x => x.FirstName == "Robert");

// Call 'Stream' to execute the query
// Optionally, pass an 'out param' for getting the query stats
IEnumerator<StreamResult<Employee>> streamResults =
session.Advanced.Stream(query, out StreamQueryStatistics streamQueryStats);

// Read from the stream
while (streamResults.MoveNext())
{
// Process the received result
StreamResult<Employee> currentResult = streamResults.Current;

// Get the document from the result
// This entity will Not be tracked by the session
Employee employee = currentResult.Document;

// The currentResult item also provides the following:
var employeeId = currentResult.Id;
var documentMetadata = currentResult.Metadata;
var documentChangeVector = currentResult.ChangeVector;

// Can get info from the stats, i.e. get number of total results
int totalResults = streamQueryStats.TotalResults;
// Get the Auto-Index that was used/created with this dynamic query
string indexUsed = streamQueryStats.IndexName;
}
Parameters
queryIQueryable or IDocumentQueryQuery to stream results for.
queryHeaderInformationQueryHeaderInformationInformation about performed query.
Return Value
IEnumerator<StreamResult>Enumerator with entities.
QueryHeaderInformationInformation about performed query.

Example I

// Define a document query on a collection
IDocumentQuery<Employee> query = session
.Advanced
.DocumentQuery<Employee>()
.WhereEquals(x => x.FirstName, "Robert");

// Call 'Stream' to execute the query
// Optionally, add an out param for getting the query stats
IEnumerator<StreamResult<Employee>> streamResults =
session.Advanced.Stream(query, out StreamQueryStatistics streamQueryStats);

// Read from the stream
while (streamResults.MoveNext())
{
// Process the received result
StreamResult<Employee> currentResult = streamResults.Current;

// Get the document from the result
// This entity will Not be tracked by the session
Employee employee = currentResult.Document;

// The currentResult item also provides the following:
var employeeId = currentResult.Id;
var documentMetadata = currentResult.Metadata;
var documentChangeVector = currentResult.ChangeVector;

// Can get info from the stats, i.e. get number of total results
int totalResults = streamQueryStats.TotalResults;
// Get the Auto-Index that was used/created with this dynamic query
string indexUsed = streamQueryStats.IndexName;
}

Example II

// Define a raw query using RQL
IRawDocumentQuery<Employee> query = session
.Advanced
.RawQuery<Employee>("from Employees where FirstName = 'Robert'");

// Call 'Stream' to execute the query
IEnumerator<StreamResult<Employee>> streamResults = session.Advanced.Stream(query);

while (streamResults.MoveNext())
{
StreamResult<Employee> currentResult = streamResults.Current;
Employee employee = streamResults.Current.Document;
}

Remarks

Streaming results only works for queries that were made against a (predefined) static index.