You are currently browsing legacy 4.0 version of documentation. Click here to switch to the newest 5.1 version.

We can help you with migration to the latest RavenDB

Contact Us Now
see on GitHub

Session: Querying: How to Perform Queries Lazily

In some situations, query execution must be delayed. To cover such a scenario, lazily and many other query extensions have been introduced.


Lazy<List<T>> lazily();

Lazy<List<T>> lazily(Consumer<List<T>> onEval);
onEval Consumer<List<TResult>> An action that will be performed on the query results.
Return Value
Lazy<List<TResult>> Lazy query initializer returning query results.


Lazy<List<Employee>> employeesLazy = session
    .whereEquals("FirstName", "Robert")

List<Employee> employees = employeesLazy.getValue(); // query will be executed here


Lazy<Integer> countLazily();
Return Value
Lazy Lazy query initializer returning a count of matched documents.


Lazy<Integer> countLazy = session
    .whereEquals("FirstName", "Robert")

Integer count = countLazy.getValue(); // query will be executed here


Lazy<Map<String, SuggestionResult>> executeLazy();

Lazy<Map<String, SuggestionResult>> executeLazy(Consumer<Map<String, SuggestionResult>> onEval);
Return Value
Lazy<Map<String, SuggestionResult>> Lazy query initializer containing a map with suggestions for matching executed query


Lazy<Map<String, SuggestionResult>> suggestLazy = session
    .query(Employee.class, Query.index("Employees_ByFullName"))
    .suggestUsing(builder -> builder.byField("FullName", "johne"))

Map<String, SuggestionResult> suggest = suggestLazy.getValue(); // query will be executed here
List<String> suggestions = suggest.get("FullName").getSuggestions();


Lazy<Map<String, FacetResult>> executeLazy();

Lazy<Map<String, FacetResult>> executeLazy(Consumer<Map<String, FacetResult>> onEval);
Return Value
Lazy<Map<String, FacetResult>> Lazy query initializer containing a map with facet results matching executed query


Lazy<Map<String, FacetResult>> facetsLazy = session
    .query(Camera.class, Query.index("Camera/Costs"))

Map<String, FacetResult> facets = facetsLazy.getValue(); // query will be executed here
FacetResult results = facets.get("manufacturer");