|
| 1 | +--- |
| 2 | +title: "SearchApi" |
| 3 | +id: integrations-searchapi |
| 4 | +description: "SearchApi integration for Haystack" |
| 5 | +slug: "/integrations-searchapi" |
| 6 | +--- |
| 7 | + |
| 8 | + |
| 9 | +## haystack_integrations.components.websearch.searchapi.websearch |
| 10 | + |
| 11 | +### SearchApiWebSearch |
| 12 | + |
| 13 | +Uses [SearchApi](https://www.searchapi.io/) to search the web for relevant documents. |
| 14 | + |
| 15 | +Usage example: |
| 16 | + |
| 17 | +```python |
| 18 | +from haystack.utils import Secret |
| 19 | + |
| 20 | +from haystack_integrations.components.websearch.searchapi import SearchApiWebSearch |
| 21 | + |
| 22 | +websearch = SearchApiWebSearch(top_k=10, api_key=Secret.from_env_var("SEARCHAPI_API_KEY")) |
| 23 | +results = websearch.run(query="Who is the boyfriend of Olivia Wilde?") |
| 24 | + |
| 25 | +assert results["documents"] |
| 26 | +assert results["links"] |
| 27 | +``` |
| 28 | + |
| 29 | +#### __init__ |
| 30 | + |
| 31 | +```python |
| 32 | +__init__( |
| 33 | + api_key: Secret = Secret.from_env_var("SEARCHAPI_API_KEY"), |
| 34 | + top_k: int | None = 10, |
| 35 | + allowed_domains: list[str] | None = None, |
| 36 | + search_params: dict[str, Any] | None = None, |
| 37 | +) -> None |
| 38 | +``` |
| 39 | + |
| 40 | +Initialize the SearchApiWebSearch component. |
| 41 | + |
| 42 | +**Parameters:** |
| 43 | + |
| 44 | +- **api_key** (<code>Secret</code>) – API key for the SearchApi API |
| 45 | +- **top_k** (<code>int | None</code>) – Number of documents to return. |
| 46 | +- **allowed_domains** (<code>list\[str\] | None</code>) – List of domains to limit the search to. |
| 47 | +- **search_params** (<code>dict\[str, Any\] | None</code>) – Additional parameters passed to the SearchApi API. |
| 48 | + For example, you can set 'num' to 100 to increase the number of search results. |
| 49 | + See the [SearchApi website](https://www.searchapi.io/) for more details. |
| 50 | + |
| 51 | +The default search engine is Google, however, users can change it by setting the `engine` |
| 52 | +parameter in the `search_params`. |
| 53 | + |
| 54 | +#### to_dict |
| 55 | + |
| 56 | +```python |
| 57 | +to_dict() -> dict[str, Any] |
| 58 | +``` |
| 59 | + |
| 60 | +Serializes the component to a dictionary. |
| 61 | + |
| 62 | +**Returns:** |
| 63 | + |
| 64 | +- <code>dict\[str, Any\]</code> – Dictionary with serialized data. |
| 65 | + |
| 66 | +#### from_dict |
| 67 | + |
| 68 | +```python |
| 69 | +from_dict(data: dict[str, Any]) -> SearchApiWebSearch |
| 70 | +``` |
| 71 | + |
| 72 | +Deserializes the component from a dictionary. |
| 73 | + |
| 74 | +**Parameters:** |
| 75 | + |
| 76 | +- **data** (<code>dict\[str, Any\]</code>) – The dictionary to deserialize from. |
| 77 | + |
| 78 | +**Returns:** |
| 79 | + |
| 80 | +- <code>SearchApiWebSearch</code> – The deserialized component. |
| 81 | + |
| 82 | +#### run |
| 83 | + |
| 84 | +```python |
| 85 | +run(query: str) -> dict[str, list[Document] | list[str]] |
| 86 | +``` |
| 87 | + |
| 88 | +Uses [SearchApi](https://www.searchapi.io/) to search the web. |
| 89 | + |
| 90 | +**Parameters:** |
| 91 | + |
| 92 | +- **query** (<code>str</code>) – Search query. |
| 93 | + |
| 94 | +**Returns:** |
| 95 | + |
| 96 | +- <code>dict\[str, list\[Document\] | list\[str\]\]</code> – A dictionary with the following keys: |
| 97 | +- "documents": List of documents returned by the search engine. |
| 98 | +- "links": List of links returned by the search engine. |
| 99 | + |
| 100 | +**Raises:** |
| 101 | + |
| 102 | +- <code>TimeoutError</code> – If the request to the SearchApi API times out. |
| 103 | +- <code>SearchApiError</code> – If an error occurs while querying the SearchApi API. |
| 104 | + |
| 105 | +#### run_async |
| 106 | + |
| 107 | +```python |
| 108 | +run_async(query: str) -> dict[str, list[Document] | list[str]] |
| 109 | +``` |
| 110 | + |
| 111 | +Asynchronously uses [SearchApi](https://www.searchapi.io/) to search the web. |
| 112 | + |
| 113 | +This is the asynchronous version of the `run` method with the same parameters and return values. |
| 114 | + |
| 115 | +**Parameters:** |
| 116 | + |
| 117 | +- **query** (<code>str</code>) – Search query. |
| 118 | + |
| 119 | +**Returns:** |
| 120 | + |
| 121 | +- <code>dict\[str, list\[Document\] | list\[str\]\]</code> – A dictionary with the following keys: |
| 122 | +- "documents": List of documents returned by the search engine. |
| 123 | +- "links": List of links returned by the search engine. |
| 124 | + |
| 125 | +**Raises:** |
| 126 | + |
| 127 | +- <code>TimeoutError</code> – If the request to the SearchApi API times out. |
| 128 | +- <code>SearchApiError</code> – If an error occurs while querying the SearchApi API. |
0 commit comments