Integration: SearchApi
Uses SearchApi for web searches
Table of Contents
Overview
The SearchApiWebSearch component allows you to perform web searches using the
SearchApi service. It retrieves relevant snippets and URLs that can be used directly in your Haystack applications, such as Retrieval-Augmented Generation (RAG) pipelines or with Haystack Agents.
This component is part of the searchapi-haystack integration package, maintained in
haystack-core-integrations.
When you pass a query to SearchApiWebSearch, it returns a list of URLs and text snippets that are most relevant to your search.
Installation
Install the searchapi-haystack package:
pip install searchapi-haystack
You will also need to get an API key from SearchApi and set it as an environment variable:
export SEARCHAPI_API_KEY="your-api-key"
Usage
Components
This integration provides the following component:
-
SearchApiWebSearch: A component that queries the SearchApi service to find web pages relevant to a given query.
Standalone Usage
Here is how you can use SearchApiWebSearch directly:
from haystack_integrations.components.websearch.searchapi import SearchApiWebSearch
from haystack.utils import Secret
import os
# Ensure the environment variable is set
os.environ["SEARCHAPI_API_KEY"] = "your-api-key"
# Initialize the component (it automatically uses the SEARCHAPI_API_KEY environment variable)
web_search = SearchApiWebSearch(api_key=Secret.from_env_var("SEARCHAPI_API_KEY"))
# Run a search query
results = web_search.run(query="What is Haystack AI?")
# Access the search results
documents = results["documents"]
links = results["links"]
for doc in documents:
print(f"Title: {doc.meta.get('title')}")
print(f"Snippet: {doc.content}")
print(f"URL: {doc.meta.get('link')}")
print("-" * 50)
License
searchapi-haystack is distributed under the terms of the
Apache-2.0 license.
