ahvn.klengine.facet_engine module

class ahvn.klengine.facet_engine.FacetKLEngine(storage, inplace=True, include=None, exclude=None, facets=None, name=None, condition=None, *args, **kwargs)[source]

Bases: BaseKLEngine

A faceted search KLEngine implementation that provides multiple search interfaces.

This class extends BaseKLEngine with three specialized search methods: - ORM-like filter-based search through the default _search method - Raw SQL query execution through _search_sql method

The engine is designed to work with structured data that can be filtered using various facets (categorical attributes) and supports both programmatic and natural language querying interfaces.

Search Methods:

_search_facet(topk, offset, include, **kwargs): Perform faceted search using ORM-like filters. _search = _search_facet: Alias for _search_facet for default search behavior.

Abstract Methods (inherited from BaseKLEngine):

_upsert(kl): Insert or update a KL in the engine. _remove(key): Remove a KL from the engine by its key (id). _clear(): Clear all KLs from the engine.

Parameters:
inplace: bool = True
__init__(storage, inplace=True, include=None, exclude=None, facets=None, name=None, condition=None, *args, **kwargs)[source]

Initialize the FacetKLEngine.

Parameters:
  • storage (DatabaseKLStore) – attach FacetKLEngine to a DatabaseKLStore (required).

  • inplace (bool) – If True, search directly on storage database; if False, create a copied table with included f

  • include (if inplace=False) – List of BaseUKF field names to include. If None, includes all fields. Default is None.

  • exclude (if inplace=False) – List of BaseUKF field names to exclude. If None, excludes no fields. Default is None. Notice that exclude is applied after include, so if a field is in both include and exclude, it will be excluded. It is recommended to use only one of include or exclude.

  • facets (Dict[str, Any]) – global facets that will be applied to all searches.

  • name (Optional[str]) – Name of the KLEngine instance. If None, defaults to “{storage.name}_facet_idx”.

  • condition (Optional[Callable]) – Optional upsert/insert condition to apply to the KLEngine. KLs that do not satisfy the condition will be ignored. If None, all KLs are accepted.

  • *args – Additional positional arguments passed to FacetKLEngine.

  • **kwargs – Additional keyword arguments passed to FacetKLEngine.

recoverable: bool = True
__len__()[source]

Return the number of KLs in the engine.

Returns:

The number of KLs in the engine.

Return type:

int

close()[source]

Closes the engine.