@upstash/search
TypeScript icon, indicating that this package has built-in type declarations

0.1.4 • Public • Published

Upstash AI Search npm (scoped) npm bundle size npm weekly download

[!NOTE]
This project is in GA Stage.

The Upstash Professional Support fully covers this project. It receives regular updates, and bug fixes. The Upstash team is committed to maintaining and improving its functionality.

It is a connectionless (HTTP based) AI Search client and designed for:

  • Serverless functions (AWS Lambda ...)
  • Cloudflare Workers
  • Next.js, Jamstack ...
  • Client side web/mobile applications
  • WebAssembly
  • and other environments where HTTP is preferred over TCP.

Quick Start

Install

Node.js

npm install @upstash/search

Create Database

Create a new database on Upstash

Basic Usage:

import { Search } from "@upstash/search";

type Content = {
  title: string;
  genre: "sci-fi" | "fantasy" | "horror" | "action";
  category: "classic" | "modern";
};

type Metadata = {
  director: string;
};

// Initialize Search client
const client = new Search({
  url: "<UPSTASH_SEARCH_REST_URL>",
  token: "<UPSTASH_SEARCH_REST_TOKEN>",
});

// Create or access a index
const index = client.index<Content, Metadata>("movies");

// Upsert data into the index
await index.upsert([
  {
    id: "star-wars",
    content: { title: "Star Wars", genre: "sci-fi", category: "classic" },
    metadata: { director: "George Lucas" },
  },
  {
    id: "inception",
    content: { title: "Inception", genre: "action", category: "modern" },
    metadata: { director: "Christopher Nolan" },
  },
]);

// Fetch documents by IDs
const documents = await index.fetch({
  ids: ["star-wars", "inception"],
});
console.log(documents);

// AI search with reranking:
const searchResults = await index.search({
  query: "space opera",
  limit: 2,
  reranking: true,
});
console.log(searchResults);

// AI search without reranking:
const searchResults = await index.search({
  query: "space opera",
  limit: 2,
});
console.log(searchResults);

// AI search with filter:
const searchResults = await index.search({
  query: "space",
  limit: 2,
  filter: "category = 'classic'",
});

// Delete a document by ID
await index.delete({
  ids: ["star-wars"],
});

// Search within a document range
const { nextCursor, documents: rangeDocuments } = await index.range({
  cursor: 0,
  limit: 1,
  prefix: "in",
});
console.log(rangeDocuments);

// Reset the index (delete all documents)
await index.reset();

// Get index and namespace info
const info = await search.info();
console.log(info);

Readme

Keywords

Package Sidebar

Install

npm i @upstash/search

Weekly Downloads

639

Version

0.1.4

License

MIT

Unpacked Size

69.2 kB

Total Files

14

Last publish

Collaborators

  • mdogan
  • buggyhunter
  • cahidarda
  • upstashnpm
  • joshtc