Perform a Lazy Query



Lazy query

To run a regular query (not a suggestions query or a faceted search) lazily, use .lazily() as follows.

# Define a lazy query
lazy_employees = (
    session.query(object_type=Employee).where_equals("first_name", "Robert")
    # Add a call to 'Lazily'
    .lazily()
)

employees = lazy_employees.value  # Query is executed here

Lazy count query

To count query results lazily, use count_lazily() as follows.

lazy_count = session.query(object_type=Employee).where_equals("first_name", "Robert").count_lazily()

count = lazy_count.value  # Query is executed here

Lazy suggestions query

To run a suggestions search lazily, use suggest_using along with .execute_lazy() as shown below.

lazy_suggestions = (
    session.query(object_type=Product).suggest_using(lambda builder: builder.by_field("name", "chaig"))
    # Add a call to 'execute_lazy'
    .execute_lazy()
)

suggest = lazy_suggestions.value  # Query is executed here
suggestions = suggest["name"].suggestions

Lazy facets query

To run a faceted query lazily:

  • Search a predefined static index (see Index_definition below)
  • Aggregate the results using a facets definition (see Facets_definition below)
  • Apply .execute_lazy() (see Query below)

# Define a lazy facets query
lazy_facets = (
    session.query_index_type(
        Products_ByCategoryAndPrice, Products_ByCategoryAndPrice.IndexEntry
    ).aggregate_by_facets(facets_definition)
    # Add a call to 'execute_lazy'
    .execute_lazy()
)

facets = lazy_facets.value  # Query is executed here

category_results = facets["Product Category"]
price_results = facets["Price per Unit"]
# The facets definition used in the facets query
facet = Facet(field_name="CategoryName")
facet.display_field_name = "Product Category"

range_facet = RangeFacet()
range_facet.ranges = [
    "price_per_unit < 200",
    "price_per_unit between 200 and 400",
    "price_per_unit between 400 and 600",
    "price_per_unit between 600 and 800",
    "price_per_unit >= 800",
]
range_facet.display_field_name = "Price per Unit"

facets_definition = [facet, range_facet]

with store.open_session() as session:
    # region lazy_10
    # Define a lazy facets query
    lazy_facets = (
        session.query_index_type(
            Products_ByCategoryAndPrice, Products_ByCategoryAndPrice.IndexEntry
        ).aggregate_by_facets(facets_definition)
        # Add a call to 'execute_lazy'
        .execute_lazy()
    )

    facets = lazy_facets.value  # Query is executed here

    category_results = facets["Product Category"]
    price_results = facets["Price per Unit"]
# The index definition used in the facets query
class Products_ByCategoryAndPrice(AbstractIndexCreationTask):
    def __init__(self):
        super().__init__()
        self.map = (
            "from p in docs.Products select new {"
            'CategoryName = LoadDocument(p.Category, "Categories").Name, PricePerUnit = p.PricePerUnit'
            "}"
        )

    class IndexEntry:
        def __init__(self, category_name: str, price_per_unit: int):
            self.category_name = category_name
            self.price_per_unit = price_per_unit

Learn more about facets in perform faceted search.

Syntax

# Lazy query
def lazily(self, on_eval: Callable[[List[_T]], None] = None) -> Lazy[List[_T]]: ...
# Lazy count query
def count_lazily(self) -> Lazy[int]: ...
# Lazy suggestions query
def execute_lazy(
    self, on_eval: Optional[Callable[[Dict[str, SuggestionResult]], None]] = None
) -> "Lazy[Dict[str, SuggestionResult]]": ...
# Lazy facet query
def execute_lazy(
    self, on_eval: Optional[Callable[[Dict[str, FacetResult]], None]] = None
) -> Lazy[Dict[str, FacetResult]]: ...
Parameters Type Description
on_eval Callable[[List[_T]], None]
Callable[[Dict[str, SuggestionResult]], None] (optional)
Callable[[Dict[str, FacetResult]], None] (optional)
An action that will be performed on the query results when the query is executed.
Return Value
Lazy[List[_T]]
Lazy[Dict[str, SuggestionResult]]
Lazy[Dict[str, FacetResult]]
A lazy instance that will evaluate the query only when needed.
Lazy[int] The results of a lazy count query