class_docstr: Search engine using SearchApi API. Given a query, it returns a list of URLs that are the most relevant.
  
  See the [SearchApi website](https://www.searchapi.io/) for more details.
docstr: Search the SearchApi API for the given query and return the results as a list of Documents and a list of links.
  
  :param query: Query string.
code: def run(self, query: str):
      """
      Search the SearchApi API for the given query and return the results as a list of Documents and a list of links.
  
      :param query: Query string.
      """
      query_prepend = (
          "OR ".join(
              f"site:{domain} " for domain in self.allowed_domains
          )
          if self.allowed_domains
          else ""
      )
  
      payload = json.dumps(
          {"q": query_prepend + " " + query, **self.search_params}
      )
      headers = {
          "Authorization": f"Bearer {self.api_key}",
          "X-SearchApi-Source": "Haystack",
      }
  
      try:
          response = requests.get(
              SEARCHAPI_BASE_URL,
              headers=headers,
              params=payload,
              timeout=90,
          )
          response.raise_for_status()  # Will raise an HTTPError for bad responses
      except requests.Timeout:
          raise TimeoutError(
              f"Request to {self.__class__.__name__} timed out."
          )
  
      except requests.RequestException as e:
          raise SearchApiError(
              f"An error occurred while querying {self.__class__.__name__}. Error: {e}"
          ) from e
  
      # Request succeeded
      json_result = response.json()
  
      # organic results are the main results from the search engine
      organic_results = []
      if "organic_results" in json_result:
          for result in json_result["organic_results"]:
              organic_results.append(
                  Document.from_dict(
                      {
                          "title": result["title"],
                          "content": result["snippet"],
                          "link": result["link"],
                      }
                  )
              )
  
      # answer box has a direct answer to the query
      answer_box = []
      if "answer_box" in json_result:
          answer_box = [
              Document.from_dict(
                  {
                      "title": json_result["answer_box"].get(
                          "title", ""
                      ),
                      "content": json_result["answer_box"].get(
                          "answer", ""
                      ),
                      "link": json_result["answer_box"].get("link", ""),
                  }
              )
          ]
  
      knowledge_graph = []
      if "knowledge_graph" in json_result:
          knowledge_graph = [
              Document.from_dict(
                  {
                      "title": json_result["knowledge_graph"].get(
                          "title", ""
                      ),
                      "content": json_result["knowledge_graph"].get(
                          "description", ""
                      ),
                  }
              )
          ]
  
      related_questions = []
      if "related_questions" in json_result:
          for result in json_result["related_questions"]:
              related_questions.append(
                  Document.from_dict(
                      {
                          "title": result["question"],
                          "content": result["answer"]
                          if result.get("answer")
                          else result.get("answer_highlight", ""),
                          "link": result.get("source", {}).get(
                              "link", ""
                          ),
                      }
                  )
              )
  
      documents = (
          answer_box
          + knowledge_graph
          + organic_results
          + related_questions
      )
  
      links = [
          result["link"] for result in json_result["organic_results"]
      ]
  
      logger.debug(
          "SearchApi returned %s documents for the query '%s'",
          len(documents),
          query,
      )
      return {
          "documents": documents[: self.top_k],
          "links": links[: self.top_k],
      }
  
file_path: haystack/haystack/components/websearch/searchapi.py
imports: import json
  import logging
  from typing import Dict, List, Optional, Any
  import os
  import requests
  from haystack import Document, component, default_to_dict, ComponentError
label_desc: Use image node_id 3 for calling the SearchApiWebSearch obj's underlying member method code with example usage: obj.run(query) and returns: dict