Session: Querying: How to perform queries lazily?

In some situations query execution must be delayed. To cover such scenario lazily and many others query extensions has been introduced.

Lazily

public Lazy<List<T>> lazily();

public Lazy<List<T>> lazily(Action1<List<T>> onEval);
Parameters
onEval Action1<List<T>> Action that will be performed on query results.
Return Value
Lazy<List<TResult>> Lazy query initializer returning query results.

Example

QEmployee e = QEmployee.employee;
Lazy<List<Employee>> employeesLazy = session
  .query(Employee.class)
  .where(e.firstName.eq("Robert"))
  .lazily();

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

CountLazily

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

Example

QEmployee e = QEmployee.employee;
Lazy<Integer> countLazy = session
  .query(Employee.class)
  .where(e.firstName.eq("Robert"))
  .countLazily();

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

SuggestLazy

public Lazy<SuggestionQueryResult> suggestLazy();

public Lazy<SuggestionQueryResult> suggestLazy(SuggestionQuery query);
Parameters
query SuggestionQuery A suggestion query definition containing all information required to query a specified index.
Return Value
Lazy<SuggestionQueryResult> Lazy query initializer containing array of all suggestions for executed query.

Example

Lazy<SuggestionQueryResult> suggestLazy = session
  .query(Employee.class)
  .suggestLazy();

SuggestionQueryResult suggest = suggestLazy.getValue(); // query will be executed here
String[] suggestions = suggest.getSuggestions();

ToFacetsLazy

public Lazy<FacetResults> toFacetsLazy(List<Facet> facets);

public Lazy<FacetResults> toFacetsLazy(List<Facet> facets, int start);

public Lazy<FacetResults> toFacetsLazy(List<Facet> facets, int start, Integer pageSize);

public Lazy<FacetResults> toFacetsLazy(String facetSetupDoc);

public Lazy<FacetResults> toFacetsLazy(String facetSetupDoc, int start);

public Lazy<FacetResults> toFacetsLazy(String facetSetupDoc, int start, Integer pageSize);
Parameters
facets List<Facet> List of facets required to perform a facet query (mutually exclusive with facetSetupDoc).
facetSetupDoc String Document key that contains predefined FacetSetup (mutually exclusive with facets).
start int number of results that should be skipped. Default: 0.
pageSize int maximum number of results that will be retrieved. Default: null.
Return Value
Lazy<FacetResults> Lazy query initializer containing Facet query results with query Duration and list of Results - one entry for each term/range as specified in [FacetSetup] document or passed in parameters.

Example

QEmployee e = QEmployee.employee;
Lazy<FacetResults> facetsLazy = session
  .query(Camera.class, "Camera/Costs")
  .toFacetsLazy("facets/CameraFacets");

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