Hybrid capability
Protocol definition for hybrid search operations.
This module defines the HybridCapability protocol that all datastores supporting hybrid search operations must implement. It provides hybrid search combining multiple retrieval paradigms (fulltext, vector).
References
NONE
HybridCapability
Bases: Protocol
Protocol for hybrid search combining different retrieval paradigms.
This protocol defines the interface for datastores that support hybrid search operations combining multiple retrieval strategies (fulltext, vector).
clear(**kwargs)
async
Clear all records from the datastore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
**kwargs
|
Any
|
Datastore-specific parameters. |
{}
|
create(chunks, **kwargs)
async
Create chunks with automatic generation of all configured search fields.
This method automatically generates and indexes all fields required by the configured searches in with_hybrid(). For each chunk:
- FULLTEXT search: Indexes text content in the configured field name.
- VECTOR search: Generates dense embedding using the configured em_invoker and indexes it in the configured field name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunks
|
list[Chunk]
|
List of chunks to create and index. |
required |
**kwargs
|
Any
|
Datastore-specific parameters. |
{}
|
create_from_vectors(chunks, dense_vectors=None, **kwargs)
async
Create chunks with pre-computed vectors for multiple fields.
Allows indexing pre-computed vectors for multiple vector fields at once. Field names must match those configured in with_hybrid().
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunks
|
list[Chunk]
|
Chunks to index. |
required |
dense_vectors
|
dict[str, list[tuple[Chunk, Vector]]] | None
|
Dict mapping field names to lists of (chunk, vector) tuples. Defaults to None. |
None
|
**kwargs
|
Any
|
Datastore-specific parameters. |
{}
|
delete(filters=None, **kwargs)
async
Delete records from the datastore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filters
|
FilterClause | QueryFilter | None
|
Filters to select records to delete. FilterClause objects are automatically converted to QueryFilter internally. Defaults to None. |
None
|
**kwargs
|
Any
|
Datastore-specific parameters. |
{}
|
Note
If filters is None, no operation is performed (no-op).
retrieve(query, fusion_mode=None, filters=None, options=None, **kwargs)
async
Retrieve using hybrid search combining different retrieval paradigms.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Query text to search with. |
required |
fusion_mode
|
str | None
|
Fusion mode to use. Defaults to None, in which case the default fusion mode from with_hybrid() is used. |
None
|
filters
|
FilterClause | QueryFilter | None
|
Query filters to apply. FilterClause objects are automatically converted to QueryFilter internally. Defaults to None. |
None
|
options
|
QueryOptions | None
|
Query options like limit and sorting. Defaults to None. |
None
|
**kwargs
|
Any
|
Datastore-specific parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
list[Chunk]
|
list[Chunk]: Query results ordered by relevance. |
retrieve_by_vectors(query=None, dense_vector=None, fusion_mode=None, filters=None, options=None, **kwargs)
async
Hybrid search using pre-computed vectors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str | None
|
Optional query text (for fulltext search). Defaults to None. |
None
|
dense_vector
|
Vector | None
|
Pre-computed dense vector for VECTOR search. Defaults to None. |
None
|
fusion_mode
|
str | None
|
Fusion mode to use. Defaults to None, in which case the default fusion mode from with_hybrid() is used. |
None
|
filters
|
FilterClause | QueryFilter | None
|
Query filters to apply. FilterClause objects are automatically converted to QueryFilter internally. Defaults to None. |
None
|
options
|
QueryOptions | None
|
Query options like limit and sorting. Defaults to None. |
None
|
**kwargs
|
Any
|
Datastore-specific parameters. |
{}
|
Returns:
| Type | Description |
|---|---|
list[Chunk]
|
list[Chunk]: Query results ordered by relevance. |
update(update_values, filters=None, **kwargs)
async
Update existing records in the datastore.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
update_values
|
dict[str, Any]
|
Values to update. |
required |
filters
|
FilterClause | QueryFilter | None
|
Filters to select records to update. FilterClause objects are automatically converted to QueryFilter internally. Defaults to None. |
None
|
**kwargs
|
Any
|
Datastore-specific parameters. |
{}
|
HybridSearchType
Bases: StrEnum
Types of searches that can be combined in hybrid search.
SearchConfig
Bases: BaseModel
Configuration for a single search component in hybrid search.
Examples:
FULLTEXT search configuration:
python
config = SearchConfig(
search_type=HybridSearchType.FULLTEXT,
field="text",
weight=0.3
)
VECTOR search configuration:
python
config = SearchConfig(
search_type=HybridSearchType.VECTOR,
field="embedding",
em_invoker=em_invoker,
weight=0.5
)
Attributes:
| Name | Type | Description |
|---|---|---|
search_type |
HybridSearchType
|
Type of search (FULLTEXT or VECTOR). |
field |
str
|
Field name in the index (e.g., "text", "embedding"). |
weight |
float
|
Weight for this search in hybrid search. Defaults to 1.0. |
em_invoker |
BaseEMInvoker | None
|
Embedding model invoker required for VECTOR type. Defaults to None. |
top_k |
int | None
|
Per-search top_k limit (optional). Defaults to None. |
extra_kwargs |
dict[str, Any]
|
Additional search-specific parameters. Defaults to empty dict. |
validate_field_not_empty(v)
classmethod
Validate that field name is not empty.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
str
|
Field name value. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
Validated field name. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If field name is empty. |
validate_search_requirements()
Validate configuration based on search type.
Returns:
| Name | Type | Description |
|---|---|---|
SearchConfig |
'SearchConfig'
|
Validated configuration instance. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If required fields are missing for the search type. |
validate_top_k(v)
classmethod
Validate that top_k is positive if provided.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
v
|
int | None
|
top_k value. |
required |
Returns:
| Type | Description |
|---|---|
int | None
|
int | None: Validated top_k value. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If top_k is provided but not positive. |