Skip to main content
`Ctrl`+`K`

# SearxNG Search

Utility for using SearxNG meta search API.

SearxNG is a privacy-friendly free metasearch engine that aggregates results from multiple search engines and databases and supports the OpenSearch specification.

More detailes on the installtion instructions here.

For the search API refer to https://docs.searxng.org/dev/search_api.html

## Quick Start

In order to use this utility you need to provide the searx host. This can be done by passing the named parameter `searx_host` or exporting the environment variable SEARX_HOST. Note: this is the only required parameter.

Then create a searx search instance like this:

>     from langchain.utilities import SearxSearchWrapper

>     # when the host starts with `http` SSL is disabled and the connection
>     # is assumed to be on a private network
>     searx_host='http://self.hosted'

>     search = SearxSearchWrapper(searx_host=searx_host)

You can now use the `search` instance to query the searx API.

## Searching

Use the `run()` and `results()` methods to query the searx API. Other methods are are available for convenience.

`SearxResults` is a convenience wrapper around the raw json result.

Example usage of the `run` method to make a search:

>     s.run(query="what is the best search engine?")

## Engine Parameters

You can pass any accepted searx search API parameters to the `SearxSearchWrapper` instance.

In the following example we are using the `engines` and the `language` parameters:

>     # assuming the searx host is set as above or exported as an env variable
>     s = SearxSearchWrapper(engines=['google', 'bing'],
>                         language='es')

## Search Tips

Searx offers a special search syntax that can also be used instead of passing engine parameters.

For example the following query:

>     s = SearxSearchWrapper("langchain library", engines=['github'])

>     # can also be written as:
>     s = SearxSearchWrapper("langchain library !github")
>     # or even:
>     s = SearxSearchWrapper("langchain library !gh")

In some situations you might want to pass an extra string to the search query. For example when the run() method is called by an agent. The search suffix can also be used as a way to pass extra parameters to searx or the underlying search engines.

>     # select the github engine and pass the search suffix
>     s = SearchWrapper("langchain library", query_suffix="!gh")

>     s = SearchWrapper("langchain library")
>     # select github the conventional google search syntax
>     s.run("large language models", query_suffix="site:github.com")

NOTE: A search suffix can be defined on both the instance and the method level. The resulting query will be the concatenation of the two with the former taking precedence.

See SearxNG Configured Engines and SearxNG Search Syntax for more details.

Notes

This wrapper is based on the SearxNG fork searxng/searxng which is better maintained than the original Searx project and offers more features.

Public searxNG instances often use a rate limiter for API usage, so you might want to use a self hosted instance and disable the rate limiter.

If you are self-hosting an instance you can customize the rate limiter for your own network as described here.

For a list of public SearxNG instances see https://searx.space/

class langchain.utilities.searx_search.SearxResults(data: str)

  Dict like wrapper around search api results.

  property answers: Any

    Helper accessor on the json result.

pydantic model langchain.utilities.searx_search.SearxSearchWrapper

  Wrapper for Searx API.

  To use you need to provide the searx host by passing the named parameter `searx_host` or exporting the environment variable `SEARX_HOST`.

  In some situations you might want to disable SSL verification, for example if you are running searx locally. You can do this by passing the named parameter `unsecure`. You can also pass the host url scheme as `http` to disable SSL.

  Example

      from langchain.utilities import SearxSearchWrapper
      searx = SearxSearchWrapper(searx_host="http://localhost:8888")

  Example with SSL disabled:

        from langchain.utilities import SearxSearchWrapper
        # note the unsecure parameter is not needed if you pass the url scheme as
        # http
        searx = SearxSearchWrapper(searx_host="http://localhost:8888",
                                                unsecure=True)

  Validators

    - `disable_ssl_warnings` » `unsecure`

    - `validate_params` » `all fields`

  field aiosession: Optional[Any] = None
  field categories: Optional[List[str]] = []
  field engines: Optional[List[str]] = []
  field headers: Optional[dict] = None
  field k: int = 10
  field params: dict [Optional]
  field query_suffix: Optional[str] = ''
  field searx_host: str = ''
  field unsecure: bool = False
  async aresults(query: str, num_results: int, engines: Optional[List[str]] = None, query_suffix: Optional[str] = '', **kwargs: Any) → List[Dict]

    Asynchronously query with json results.

    Uses aiohttp. See results for more info.

  async arun(query: str, engines: Optional[List[str]] = None, query_suffix: Optional[str] = '', **kwargs: Any) → str

    Asynchronously version of run.

  results(query: str, num_results: int, engines: Optional[List[str]] = None, categories: Optional[List[str]] = None, query_suffix: Optional[str] = '', **kwargs: Any) → List[Dict]

    Run query through Searx API and returns the results with metadata.

    Parameters

      - query – The query to search for.

      - query_suffix – Extra suffix appended to the query.

      - num_results – Limit the number of results to return.

      - engines – List of engines to use for the query.

      - categories – List of categories to use for the query.

      - **kwargs – extra parameters to pass to the searx API.

    Returns

      {

        snippet: The description of the result.

        title: The title of the result.

        link: The link to the result.

        engines: The engines used for the result.

        category: Searx category of the result.

      }

    Return type

      Dict with the following keys

  run(query: str, engines: Optional[List[str]] = None, categories: Optional[List[str]] = None, query_suffix: Optional[str] = '', **kwargs: Any) → str

    Run query through Searx API and parse results.

    You can pass any other params to the searx query API.

    Parameters

      - query – The query to search for.

      - query_suffix – Extra suffix appended to the query.

      - engines – List of engines to use for the query.

      - categories – List of categories to use for the query.

      - **kwargs – extra parameters to pass to the searx API.

    Returns

      The result of the query.

    Return type

      str

    Raises

      ValueError – If an error occured with the query.

    Example

    This will make a query to the qwant engine:

        from langchain.utilities import SearxSearchWrapper
        searx = SearxSearchWrapper(searx_host="http://my.searx.host")
        searx.run("what is the weather in France ?", engine="qwant")

        # the same result can be achieved using the `!` syntax of searx
        # to select the engine using `query_suffix`
        searx.run("what is the weather in France ?", query_suffix="!qwant")