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.

Lazily

query.lazily();
Return Value
Lazy<object[]> Lazy query initializer returning query results.

Example

const employeesLazy = session
    .query({ collection: "Employees" })
    .whereEquals("FirstName", "Robert")
    .lazily();

const employees = await employeesLazy.getValue(); // query will be executed here

Counts

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

Example

const countLazy = session
    .query({ collection: "Employees" })
    .whereEquals("FirstName", "Robert")
    .countLazily();

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

Suggestions

query.executeLazy();
Return Value
Lazy<{ [key]: SuggestionResult }> Lazy query initializer containing a map with suggestions for matching executed query

Example

const suggestLazy = session
    .query({ indexName: "Employees_ByFullName" })
    .suggestUsing(builder => builder.byField("FullName", "Johne"))
    .executeLazy();

const suggestResult = await suggestLazy.getValue(); // query will be executed here
const suggestions = suggestResult["FullName"].suggestions;

Facets

query.executeLazy();
Return Value
Lazy<{ [key]: FacetResult }> Lazy query initializer containing a map with facet results matching executed query

Example

const facetsLazy = session
    .query({ indexName: "Camera/Costs" })
    .aggregateUsing("facets/CameraFacets")
    .executeLazy();

const facets = await facetsLazy.getValue(); // query will be executed here
const results = facets["manufacturer"];