lenskit.data.RecQuery#
- class lenskit.data.RecQuery(user_id=None, history_items=None, session_items=None, context_items=None, query_source=None, user_items=None, query_id=None)#
Bases:
objectRepresentation of a the data available for a recommendation query. This is generally the available inputs for a recommendation request except the set of candidate items.
Todo
Document and test methods for extending the recommendation query with arbitrary data to be used by client-provided pipeline components.
Todo
When LensKit supports context-aware recommendation, this should be extended to include context cues.
- Stability:
- Caller (see Stability Levels).
- Parameters:
user_id (int | str | bytes | integer[Any] | str_ | bytes_ | object_ | None)
history_items (ItemList | None)
session_items (ItemList | None)
context_items (ItemList | None)
query_source (Literal['history', 'session', 'context'] | set[~typing.Literal['history', 'session', 'context']] | None)
user_items (ItemList | None)
query_id (int | str | bytes | integer[Any] | str_ | bytes_ | object_ | None)
- __init__(user_id=None, history_items=None, session_items=None, context_items=None, query_source=None, user_items=None, query_id=None)#
- Parameters:
user_id (int | str | bytes | integer[Any] | str_ | bytes_ | object_ | None)
history_items (ItemList | None)
session_items (ItemList | None)
context_items (ItemList | None)
query_source (Literal['history', 'session', 'context'] | set[~typing.Literal['history', 'session', 'context']] | None)
user_items (ItemList | None)
query_id (int | str | bytes | integer[Any] | str_ | bytes_ | object_ | None)
- Return type:
None
Methods
__init__([user_id, history_items, ...])combined_items(*sources)Obtain a combined list of items from one or more sources.
create(data)Create a recommendation query from an input, filling in available components based on the data type.
Attributes
Get a single list of all items mentioned in the query.
Items that form a context for the requested recommendations.
The items from the user's interaction history, with ratings if available.
An identifier for this query.
Return the items the recommender should treat as a “query”.
The list of items to return from
query_items().Items the user has interacted with in the current session.
The user's identifier, if known.
Deprecated alias for
history_items.- user_id: int | str | bytes | integer[Any] | str_ | bytes_ | object_ | None = None#
The user’s identifier, if known.
- history_items: ItemList | None = None#
The items from the user’s interaction history, with ratings if available. This list is deduplicated, like
interaction_matrix(), rather than a full interaction log. As withuser_row(), the rating is expected to be on aratingfield, notscore.
- context_items: ItemList | None = None#
Items that form a context for the requested recommendations. For example:
To recommend products related to a product the user is viewing, the viewed product is a context item.
To recommend products to add to a shopping cart, the current cart contents are the context items.
- query_source: Literal['history', 'session', 'context'] | set[Literal['history', 'session', 'context']] | None = None#
The list of items to return from
query_items().
- user_items: ItemList | None = None#
Deprecated alias for
history_items.Deprecated since version 2025.6: Use
history_itemsinstead. This property will be removed in LensKit 2026.1.
- query_id: int | str | bytes | integer[Any] | str_ | bytes_ | object_ | None = None#
An identifier for this query.
Query identifiers are used for things like mapping batch recommendation outputs to their inputs.
- classmethod create(data)#
Create a recommendation query from an input, filling in available components based on the data type.
- property query_items: ItemList | None#
Return the items the recommender should treat as a “query”.
This property exists so that components can obtain a list of items to use for generating recommendations, regardless of the precise shape of recommendation problem being solved (e.g., user-personalized recommendation or session-based recommendation). This allows general models such as
ItemKNNScorerto operate across recommendation tasks.The
query_sourceattribute determines which list(s) of items in this query are considered the “query items”. If unset, the first of the following that is available are used as the query items: