|
| 1 | +--- |
| 2 | +title: "DDGSWebSearch" |
| 3 | +id: ddgswebsearch |
| 4 | +slug: "/ddgswebsearch" |
| 5 | +description: "Multi-engine web search using ddgs (Dux Distributed Global Search), with no API key required." |
| 6 | +--- |
| 7 | + |
| 8 | +# DDGSWebSearch |
| 9 | + |
| 10 | +Search the web with ddgs (Dux Distributed Global Search), a metasearch library that aggregates results from multiple search engines without an API key. |
| 11 | + |
| 12 | +<div className="key-value-table"> |
| 13 | + |
| 14 | +| | | |
| 15 | +| --- | --- | |
| 16 | +| **Most common position in a pipeline** | Before a [`ChatPromptBuilder`](../builders/chatpromptbuilder.mdx) or right at the beginning of an indexing pipeline | |
| 17 | +| **Mandatory init variables** | None. `ddgs` requires no API key. | |
| 18 | +| **Mandatory run variables** | `query`: A string with your search query. | |
| 19 | +| **Output variables** | `documents`: A list of Haystack Documents containing search result snippets, with the result title and URL in the metadata. <br /> <br />`links`: A list of strings of resulting URLs. | |
| 20 | +| **API reference** | [ddgs API](/reference/integrations-ddgs) | |
| 21 | +| **GitHub link** | https://github.com/deepset-ai/haystack-core-integrations/blob/main/integrations/ddgs/src/haystack_integrations/components/websearch/ddgs/ddgs_websearch.py | |
| 22 | +| **Package name** | `ddgs-haystack` | |
| 23 | + |
| 24 | +</div> |
| 25 | + |
| 26 | +## Overview |
| 27 | + |
| 28 | +When you give `DDGSWebSearch` a query, it uses [ddgs](https://github.com/deedy5/ddgs) to search the web and return the result snippets as Haystack `Document` objects. It also returns a list of the source URLs. |
| 29 | + |
| 30 | +Unlike the other websearch components, `DDGSWebSearch` needs **no API key and no account**. `ddgs` is a free metasearch library that queries public search engines directly, aggregating results from backends such as DuckDuckGo, Google, Bing, Brave, Yahoo, Yandex, and Mullvad. |
| 31 | + |
| 32 | +You can configure the search with: |
| 33 | + |
| 34 | +- `backend`: A comma-separated list of ddgs backends to query, for example `"duckduckgo, google, brave"`, or `"auto"` to let ddgs choose. See the [ddgs documentation](https://github.com/deedy5/ddgs) for the full list of backends. |
| 35 | +- `region`: The region and locale of the search, for example `"us-en"`, `"de-de"`, or `"wt-wt"` for no region. |
| 36 | +- `safesearch`: The safe-search level, one of `"on"`, `"moderate"`, or `"off"`. |
| 37 | +- `top_k`: The maximum number of results to return. |
| 38 | +- `search_params`: Additional keyword arguments forwarded to the underlying `DDGS().text()` call, such as `page` or `timelimit`. Values you set here take precedence over `backend`, `region`, `safesearch`, and `top_k`. |
| 39 | + |
| 40 | +All of these can be overridden for a single search by passing them to `run()`. Note that a `search_params` dictionary passed to `run()` fully replaces the one set at initialization instead of being merged with it. |
| 41 | + |
| 42 | +`DDGSWebSearch` also supports asynchronous execution through `run_async()`. Because `ddgs` has no native async API, the blocking search runs in a worker thread. The underlying client is created lazily on the first search. To avoid the cold-start latency of the first call, you can call `warm_up()` explicitly. |
| 43 | + |
| 44 | +:::note[Best-effort results] |
| 45 | + |
| 46 | +`ddgs` queries public search engines without an API contract, so results are best-effort: they can differ between runs, and heavy use may be throttled or temporarily blocked. For production workloads that need predictable rate limits, consider a component backed by a commercial search API, such as [`TavilyWebSearch`](tavilywebsearch.mdx) or [`SerperDevWebSearch`](serperdevwebsearch.mdx). |
| 47 | +::: |
| 48 | + |
| 49 | +## Usage |
| 50 | + |
| 51 | +Install the `ddgs-haystack` package to use the `DDGSWebSearch` component: |
| 52 | + |
| 53 | +```shell |
| 54 | +pip install ddgs-haystack |
| 55 | +``` |
| 56 | + |
| 57 | +### On its own |
| 58 | + |
| 59 | +Here is a quick example of how `DDGSWebSearch` searches the web based on a query and returns a list of Documents. No API key is needed. |
| 60 | + |
| 61 | +```python |
| 62 | +from haystack_integrations.components.websearch.ddgs import DDGSWebSearch |
| 63 | + |
| 64 | +web_search = DDGSWebSearch(top_k=5) |
| 65 | +query = "What is Haystack by deepset?" |
| 66 | + |
| 67 | +response = web_search.run(query=query) |
| 68 | + |
| 69 | +for doc in response["documents"]: |
| 70 | + print(doc.meta["url"]) |
| 71 | + print(doc.content) |
| 72 | +``` |
| 73 | + |
| 74 | +To search with specific backends and in a specific region: |
| 75 | + |
| 76 | +```python |
| 77 | +web_search = DDGSWebSearch( |
| 78 | + top_k=5, |
| 79 | + backend="duckduckgo, brave", |
| 80 | + region="de-de", |
| 81 | + safesearch="off", |
| 82 | +) |
| 83 | +``` |
| 84 | + |
| 85 | +### In a pipeline |
| 86 | + |
| 87 | +Here is an example of a Retrieval-Augmented Generation (RAG) pipeline that uses `DDGSWebSearch` to look up an answer on the web. |
| 88 | + |
| 89 | +```python |
| 90 | +from haystack import Pipeline |
| 91 | +from haystack.utils import Secret |
| 92 | +from haystack.components.builders.chat_prompt_builder import ChatPromptBuilder |
| 93 | +from haystack.components.generators.chat import OpenAIChatGenerator |
| 94 | +from haystack_integrations.components.websearch.ddgs import DDGSWebSearch |
| 95 | +from haystack.dataclasses import ChatMessage |
| 96 | + |
| 97 | +web_search = DDGSWebSearch(top_k=3) |
| 98 | + |
| 99 | +prompt_template = [ |
| 100 | + ChatMessage.from_system("You are a helpful assistant."), |
| 101 | + ChatMessage.from_user( |
| 102 | + "Given the information below:\n" |
| 103 | + "{% for document in documents %}{{ document.content }}\n{% endfor %}\n" |
| 104 | + "Answer the following question: {{ query }}.\nAnswer:", |
| 105 | + ), |
| 106 | +] |
| 107 | + |
| 108 | +prompt_builder = ChatPromptBuilder( |
| 109 | + template=prompt_template, |
| 110 | + required_variables={"query", "documents"}, |
| 111 | +) |
| 112 | + |
| 113 | +llm = OpenAIChatGenerator( |
| 114 | + api_key=Secret.from_env_var("OPENAI_API_KEY"), |
| 115 | +) |
| 116 | + |
| 117 | +pipe = Pipeline() |
| 118 | +pipe.add_component("search", web_search) |
| 119 | +pipe.add_component("prompt_builder", prompt_builder) |
| 120 | +pipe.add_component("llm", llm) |
| 121 | + |
| 122 | +pipe.connect("search.documents", "prompt_builder.documents") |
| 123 | +pipe.connect("prompt_builder.prompt", "llm.messages") |
| 124 | + |
| 125 | +query = "What is Haystack by deepset?" |
| 126 | + |
| 127 | +result = pipe.run(data={"search": {"query": query}, "prompt_builder": {"query": query}}) |
| 128 | + |
| 129 | +print(result["llm"]["replies"][0].text) |
| 130 | +``` |
| 131 | + |
| 132 | +Because `ddgs` returns only short snippets rather than full page content, you can add a [`LinkContentFetcher`](../fetchers/linkcontentfetcher.mdx) and a converter after the search to fetch and read the actual web pages when you need more context. |
0 commit comments