upstash-search is a Python SDK for Upstash AI Search, enabling easier operations on Search Databases.

Using upstash-search you can:

  • Perform AI-powered search queries with or without reranking.
  • Upsert documents with metadata to an index.
  • Fetch documents by their IDs.
  • Delete documents from a database.
  • Access database stats.
  • Reset everything related to a database.

You can find the GitHub Repository here.

Install

pip install upstash-search

Usage

Initializing the client

There are two pieces of configuration required to use the Upstash search client: a REST token and REST URL. These values can be passed using environment variables or in code through a configuration object. Find your configuration values in the console dashboard.

Using environment variables

The environment variables used to configure the client are the following. You can follow this guide to retrieve credentials.

UPSTASH_SEARCH_REST_URL="your_rest_url"
UPSTASH_SEARCH_REST_TOKEN="your_rest_token"

When these environment variables are set, the client constructor does not require any additional arguments.

from upstash_search import Search

client = Search.from_env()

Using a configuration object

If you prefer to pass configuration in code, the constructor accepts a config object containing the url and token values. This could be useful if your application needs to interact with multiple databases, each with a different configuration.

from upstash_search import Search

client = Search(
    url="<SEARCH_INDEX_REST_URL>",
    token="<SEARCH_INDEX_REST_TOKEN>",
)

Using an Index

The Search client is for operations that are about manipulating the Search database. With it, you can create indexes which is where you can upsert and search documents.

index = client.index("films")

index.upsert(
    documents=[
        {
            "id": "movie-0",
            "content": {
                "title": "Star Wars",
                "overview": "Sci-fi space opera",
                "genre": "sci-fi",
                "category": "classic",
            },
            "metadata": {
                "poster": "https://poster.link/starwars.jpg",
            },
        },
    ],
)