|
| 1 | +# SPDX-FileCopyrightText: 2023-present deepset GmbH <info@deepset.ai> |
| 2 | +# |
| 3 | +# SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +from typing import Any |
| 6 | + |
| 7 | +from haystack import component, default_from_dict, default_to_dict |
| 8 | +from haystack.dataclasses import Document |
| 9 | +from haystack.document_stores.types import FilterPolicy |
| 10 | +from haystack.document_stores.types.filter_policy import apply_filter_policy |
| 11 | + |
| 12 | +from haystack_integrations.document_stores.oracle import OracleDocumentStore |
| 13 | + |
| 14 | + |
| 15 | +@component |
| 16 | +class OracleKeywordRetriever: |
| 17 | + """ |
| 18 | + Retrieves documents from an OracleDocumentStore using keyword-based (BM25) similarity. |
| 19 | +
|
| 20 | + Requires Oracle Database 23ai and an automatically created DBMS_SEARCH index. |
| 21 | +
|
| 22 | + Use inside a Haystack pipeline:: |
| 23 | +
|
| 24 | + pipeline.add_component("retriever", OracleKeywordRetriever(document_store=store, top_k=5)) |
| 25 | + """ |
| 26 | + |
| 27 | + def __init__( |
| 28 | + self, |
| 29 | + *, |
| 30 | + document_store: OracleDocumentStore, |
| 31 | + filters: dict[str, Any] | None = None, |
| 32 | + top_k: int = 10, |
| 33 | + filter_policy: FilterPolicy = FilterPolicy.REPLACE, |
| 34 | + ) -> None: |
| 35 | + if not isinstance(document_store, OracleDocumentStore): |
| 36 | + msg = "document_store must be an instance of OracleDocumentStore" |
| 37 | + raise TypeError(msg) |
| 38 | + self.document_store = document_store |
| 39 | + self.filters = filters or {} |
| 40 | + self.top_k = top_k |
| 41 | + self.filter_policy = FilterPolicy.from_str(filter_policy) if isinstance(filter_policy, str) else filter_policy |
| 42 | + |
| 43 | + @component.output_types(documents=list[Document]) |
| 44 | + def run( |
| 45 | + self, |
| 46 | + query: str, |
| 47 | + filters: dict[str, Any] | None = None, |
| 48 | + top_k: int | None = None, |
| 49 | + ) -> dict[str, list[Document]]: |
| 50 | + """ |
| 51 | + Retrieve documents by keyword search. |
| 52 | +
|
| 53 | + Args: |
| 54 | + query: The keyword query string. |
| 55 | + filters: Runtime filters, merged with constructor filters according to filter_policy. |
| 56 | + top_k: Override the constructor top_k for this call. |
| 57 | +
|
| 58 | + Returns: |
| 59 | + ``{"documents": [Document, ...]}`` |
| 60 | + """ |
| 61 | + filters = apply_filter_policy(self.filter_policy, self.filters, filters) |
| 62 | + docs = self.document_store._keyword_retrieval( |
| 63 | + query, |
| 64 | + filters=filters, |
| 65 | + top_k=top_k if top_k is not None else self.top_k, |
| 66 | + ) |
| 67 | + return {"documents": docs} |
| 68 | + |
| 69 | + @component.output_types(documents=list[Document]) |
| 70 | + async def run_async( |
| 71 | + self, |
| 72 | + query: str, |
| 73 | + filters: dict[str, Any] | None = None, |
| 74 | + top_k: int | None = None, |
| 75 | + ) -> dict[str, list[Document]]: |
| 76 | + """Async variant of :meth:`run`.""" |
| 77 | + filters = apply_filter_policy(self.filter_policy, self.filters, filters) |
| 78 | + docs = await self.document_store._keyword_retrieval_async( |
| 79 | + query, |
| 80 | + filters=filters, |
| 81 | + top_k=top_k if top_k is not None else self.top_k, |
| 82 | + ) |
| 83 | + return {"documents": docs} |
| 84 | + |
| 85 | + def to_dict(self) -> dict[str, Any]: |
| 86 | + """ |
| 87 | + Serializes the component to a dictionary. |
| 88 | +
|
| 89 | + :returns: |
| 90 | + Dictionary with serialized data. |
| 91 | + """ |
| 92 | + return default_to_dict( |
| 93 | + self, |
| 94 | + document_store=self.document_store.to_dict(), |
| 95 | + filters=self.filters, |
| 96 | + top_k=self.top_k, |
| 97 | + filter_policy=self.filter_policy.value, |
| 98 | + ) |
| 99 | + |
| 100 | + @classmethod |
| 101 | + def from_dict(cls, data: dict[str, Any]) -> "OracleKeywordRetriever": |
| 102 | + """ |
| 103 | + Deserializes the component from a dictionary. |
| 104 | +
|
| 105 | + :param data: |
| 106 | + Dictionary to deserialize from. |
| 107 | + :returns: |
| 108 | + Deserialized component. |
| 109 | + """ |
| 110 | + params = data.get("init_parameters", {}) |
| 111 | + if "document_store" in params: |
| 112 | + params["document_store"] = OracleDocumentStore.from_dict(params["document_store"]) |
| 113 | + if filter_policy := params.get("filter_policy"): |
| 114 | + params["filter_policy"] = FilterPolicy.from_str(filter_policy) |
| 115 | + return default_from_dict(cls, data) |
0 commit comments