Ibm Db
haystack_integrations.components.retrievers.ibm_db.embedding_retriever
IBMDb2EmbeddingRetriever
Retrieves documents from a IBMDb2DocumentStore using vector similarity.
Use inside a Haystack pipeline after a text embedder:
pipeline.add_component("embedder", SentenceTransformersTextEmbedder())
pipeline.add_component("retriever", IBMDb2EmbeddingRetriever(
document_store=store, top_k=5
))
pipeline.connect("embedder.embedding", "retriever.query_embedding")
init
__init__(
*,
document_store: IBMDb2DocumentStore,
filters: dict[str, Any] | None = None,
top_k: int = 10,
filter_policy: FilterPolicy = FilterPolicy.REPLACE
) -> None
Initialize the IBMDb2EmbeddingRetriever.
Parameters:
- document_store (
IBMDb2DocumentStore) – An instance ofIBMDb2DocumentStore. - filters (
dict[str, Any] | None) – Filters applied to the retrieved Documents. - top_k (
int) – Maximum number of Documents to return. - filter_policy (
FilterPolicy) – Policy to determine how filters are applied.
Raises:
TypeError– Ifdocument_storeis not an instance ofIBMDb2DocumentStore.
run
run(
query_embedding: list[float],
filters: dict[str, Any] | None = None,
top_k: int | None = None,
) -> dict[str, list[Document]]
Retrieve documents by vector similarity.
Parameters:
- query_embedding (
list[float]) – Dense float vector from an embedder component. - filters (
dict[str, Any] | None) – Runtime filters, merged with constructor filters according to filter_policy. - top_k (
int | None) – Override the constructor top_k for this call.
Returns:
dict[str, list[Document]]– A dictionary with keydocumentscontaining a list of matching :class:Documentobjects.
to_dict
Serializes the component to a dictionary.
Returns:
dict[str, Any]– Dictionary with serialized data.
from_dict
Deserializes the component from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary to deserialize from.
Returns:
IBMDb2EmbeddingRetriever– Deserialized component.
haystack_integrations.document_stores.ibm_db.document_store
IBM DB2 Document Store for Haystack.
IBMDb2DocumentStore
IBM DB2 Document Store for Haystack using vector search capabilities.
This document store uses IBM DB2's native vector search functionality to store and retrieve documents with embeddings.
init
__init__(
*,
database: str,
hostname: str,
username: Secret = Secret.from_env_var("DB2_USERNAME"),
password: Secret = Secret.from_env_var("DB2_PASSWORD"),
port: int = 50000,
protocol: str = "TCPIP",
schema: str | None = None,
use_ssl: bool = False,
ssl_certificate: str | None = None,
connection_options: dict[str, Any] | None = None,
table_name: str = "haystack_documents",
embedding_dim: int = 768,
distance_metric: Literal["EUCLIDEAN", "COSINE", "MANHATTAN"] = "COSINE",
recreate_table: bool = False
)
Initialize the IBM DB2 Document Store.
Parameters:
- database (
str) – Database name - hostname (
str) – Database server hostname - username (
Secret) – Database username as aSecret, e.g.Secret.from_env_var("DB2_USERNAME"). - password (
Secret) – Database password as aSecret, e.g.Secret.from_env_var("DB2_PASSWORD"). - port (
int) – Database server port (default: 50000) - protocol (
str) – Connection protocol (default: "TCPIP") - schema (
str | None) – Database schema (optional) - use_ssl (
bool) – Enable SSL/TLS connection (default: False) - ssl_certificate (
str | None) – Path to SSL certificate file (optional, required if use_ssl is True) - connection_options (
dict[str, Any] | None) – Additional connection options as dict (optional) - table_name (
str) – Name of the table to store documents (default: "haystack_documents") - embedding_dim (
int) – Dimension of embedding vectors (default: 768) - distance_metric (
Literal['EUCLIDEAN', 'COSINE', 'MANHATTAN']) – Distance metric for similarity search (default: "COSINE") - recreate_table (
bool) – If True, drop and recreate the table (default: False)
count_documents
Count all documents in the store.
Returns:
int– Number of documents
count_documents_by_filter
Count documents that match the provided filters.
Parameters:
- filters (
dict[str, Any] | None) – Filters to apply. See Haystack documentation for filter syntax.
Returns:
int– Number of documents matching the filters
write_documents
write_documents(
documents: list[Document], policy: DuplicatePolicy = DuplicatePolicy.NONE
) -> int
Write documents to the store.
Parameters:
- documents (
list[Document]) – List of documents to write - policy (
DuplicatePolicy) – Policy for handling duplicate documents
Returns:
int– Number of documents written
Raises:
ValueError– If documents is not a list of Document objects or has invalid embeddingsTypeError– If embeddings have invalid typesDuplicateDocumentError– If a document with the same id already exists and policy is FAIL or NONE
filter_documents
Filter documents using SQL-based metadata and field conditions.
Parameters:
- filters (
dict[str, Any] | None) – Optional filter dictionary to constrain the returned documents.
Returns:
list[Document]– List of matching documents.
delete_documents
Delete documents by their IDs.
Parameters:
- document_ids (
list[str]) – List of document IDs to delete
delete_by_filter
Delete documents that match the provided filters.
Parameters:
- filters (
dict[str, Any] | None) – Filters to apply. See Haystack documentation for filter syntax.
Returns:
int– Number of documents deleted
delete_all_documents
Delete all documents from the document store.
Parameters:
- recreate_index (
bool) – If True, recreate the table after deletion
Returns:
int– Number of documents deleted
update_by_filter
update_by_filter(
filters: dict[str, Any] | None = None, meta: dict[str, Any] | None = None
) -> int
Update documents that match the provided filters.
Parameters:
- filters (
dict[str, Any] | None) – Filters to apply. See Haystack documentation for filter syntax. - meta (
dict[str, Any] | None) – Dictionary of metadata fields to update
Returns:
int– Number of documents updated
get_metadata_field_unique_values
Get all unique values for a given metadata field.
Parameters:
- field (
str) – The metadata field name (can include 'meta.' prefix)
Returns:
list[Any]– List of unique values for the field
get_metadata_field_min_max
Get the minimum and maximum values for a numeric metadata field.
Parameters:
- field (
str) – The metadata field name (can include 'meta.' prefix)
Returns:
dict[str, Any]– Dictionary with 'min' and 'max' keys
get_metadata_fields_info
Get information about all metadata fields including their types.
Returns:
dict[str, dict[str, Any]]– Dictionary mapping field names to their type information
count_unique_metadata_by_filter
count_unique_metadata_by_filter(
filters: dict[str, Any] | None = None,
metadata_fields: list[str] | None = None,
) -> dict[str, int]
Count unique values for specified metadata fields, optionally filtered.
Parameters:
- filters (
dict[str, Any] | None) – Optional filters to apply before counting - metadata_fields (
list[str] | None) – List of metadata field names to count unique values for
Returns:
dict[str, int]– Dictionary mapping field names to their unique value counts
to_dict
Serialize the document store to a dictionary.
Returns:
dict[str, Any]– Dictionary representation
from_dict
Deserialize the document store from a dictionary.
Parameters:
- data (
dict[str, Any]) – Dictionary representation
Returns:
IBMDb2DocumentStore– IBMDb2DocumentStore instance