Perform a Lazy Query
-
Query execution can be deferred: the query can be defined as Lazy, and executed at a later time, when its results are actually needed.
-
This article contains examples for lazy queries. Prior to reading it, please refer to perform requests lazily for general knowledge about RavenDB's lazy behavior and other request types that can be executed lazily within a session.
-
In this page:
Lazy query
// Define a lazy query
const lazyEmployees = session
.query({ collection: "Employees" })
.whereEquals("FirstName", "Robert")
// Add a call to 'lazily'
.lazily();
const employees = await lazyEmployees.getValue(); // Query is executed here
- Learn more about queries in this query overview.
Lazy count query
// Define a lazy count query
const lazyCount = session
.query({ collection: "Employees" })
.whereEquals("FirstName", "Robert")
// Add a call to 'countLazily'
.countLazily();
const count = await lazyCount.getValue(); // Query is executed here
Lazy suggestions query
// Define a lazy suggestion query
const lazySuggestions = session
.query({ collection: "Products" })
.suggestUsing(builder => builder.byField("Name", "chaig"))
// Add a call to 'executeLazy'
.executeLazy();
const suggestResult = await lazySuggestions.getValue(); // Query is executed here
const suggestions = suggestResult["Name"].suggestions;
- Learn more about suggestions in query for suggestions.
Lazy facets query
// The facets definition used in the facets query:
// ===============================================
const categoryNameFacet = new Facet();
categoryNameFacet.fieldName = "categoryName";
categoryNameFacet.displayFieldName = "Product Category";
const rangeFacet = new RangeFacet();
rangeFacet.ranges = [
"pricePerUnit < " + 25,
"pricePerUnit >= " + 25 + " and pricePerUnit < " + 50,
"pricePerUnit >= " + 50 + " and pricePerUnit < " + 100,
"pricePerUnit >= " + 100
];
rangeFacet.displayFieldName = 'Price per Unit';
const facetsDefinition = [categoryNameFacet, rangeFacet];
// The lazy factes query:
// ======================
const lazyFacets = session
.query({ indexName: "Products/ByCategoryAndPrice" })
.aggregateBy(...facetsDefinition)
// Add a call to 'executeLazy'
.executeLazy();
const facets = await lazyFacets.getValue(); // Query is executed here
const categoryResults = facets["Product Category"];
const priceResults = facets["Price per Unit"];
// The index definition used in the facets query:
class Products_ByCategoryAndPrice extends AbstractJavaScriptIndexCreationTask {
constructor() {
super();
const { load } = this.mapUtils();
this.map("Products", product => {
return {
categoryName: load(product.Category, "Categories").Name,
pricePerUnit: product.PricePerUnit
}
});
}
}
- Learn more about facets in perform faceted search.
Syntax
lazily();
lazily(onEval);
countLazily();
executeLazy();
Parameters | Type | Description |
---|---|---|
onEval | (object[]) => void |
An action that will be performed on the query results when the query is executed. |
Return Value | |
---|---|
object | A Lazy instance that will evaluate the query only when needed. |