Skip to main content

Documentation Index

Fetch the complete documentation index at: https://upstash.com/docs/llms.txt

Use this file to discover all available pages before exploring further.

1. Create Index

import { Redis, s } from "@upstash/redis";

const redis = Redis.fromEnv();

const index = await redis.search.createIndex({
  name: "products",
  dataType: "json",
  prefix: "product:",
  schema: s.object({
    name: s.string(),
    description: s.string(),
    category: s.string().noTokenize(),
    price: s.number(),
    inStock: s.boolean(),
  }),
});
Create an index once, not on every request. createIndex throws if an index with the same name already exists. To make setup safely re-runnable, pass exists_ok=True in the Python SDK, or wrap the call in a try/catch in TypeScript.

2. Add Data

Add data using standard Redis JSON commands. Any key matching the index prefix will be automatically indexed.
Writes are indexed asynchronously: a JSON.SET returns before the document is searchable. For demos and tests, call waitIndexing() / wait_indexing() to block until pending updates are applied. In production, queries running on a later request will normally hit an up-to-date index without waiting.
await redis.json.set("product:1", "$", {
  name: "Wireless Headphones",
  description:
    "Premium noise-cancelling wireless headphones with 30-hour battery life",
  category: "electronics",
  price: 199.99,
  inStock: true,
});

await redis.json.set("product:2", "$", {
  name: "Running Shoes",
  description: "Lightweight running shoes with advanced cushioning technology",
  category: "sports",
  price: 129.99,
  inStock: true,
});

await index.waitIndexing();

3. Search Data

const results = await index.query({
  filter: { description: "wireless" },
});

const count = await index.count({
  filter: { price: { $lt: 150 } },
});

Next Steps

Schema Definition

Define the fields you want to index and how they are matched

Querying

Learn the JSON-based query language with filters and operators

Aggregations

Group and summarize your indexed data with aggregation pipelines

Recipes

Complete, real-world examples you can adapt to your own use cases