# Source: https://upstash.com/docs/vector/features/algorithm.md

# Source: https://upstash.com/docs/search/features/algorithm.md

> ## 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.

# Algorithm

Our algorithm combines AI-powered query enhancement, hybrid search techniques, and intelligent reranking to understand user intent (also known as [search intent](https://backlinko.com/hub/seo/search-intent)) and return the most accurate results.

Upstash Search processes every query through three key stages:

1. **Input Enrichment**: Enhances the search query using AI to better understand user intent.
2. **Hybrid Vector Search**: Combines semantic search and full-text search to find relevant documents.
3. **Reranking**: Uses AI models to reorder results based on relevance.

### 1. Input Enrichment

The first stage enhances your search query using a Large Language Model (LLM). This process:

* Expands the original query with related terms and context
* Improves understanding of user intent
* Handles typos and alternative phrasings
* Adds semantic context that might be missing from the original query

While input enrichment introduces some latency, it significantly improves search quality.

Input enrichment is enabled by default. You can disable this feature if you want to preserve the user query for full text-search or if you want to reduce latency.

<CodeGroup>
  ```typescript TypeScript theme={"system"}
  const results = await index.search({
    query: "space opera",
    inputEnrichment: false // faster but less enhanced results
  });
  ```

  ```python Python theme={"system"}
  results = index.search(
      query="space opera",
      input_enrichment=False  # faster but less enhanced results
  )
  ```
</CodeGroup>

### 2. Hybrid Vector Search

The second stage performs hybrid search by combining semantic search and full-text search:

* **Semantic Search**: Uses vector embeddings to understand meaning and context
* **Full-Text Search**: Performs traditional keyword matching
* **Result Combination**: Merges results using configurable weights

By default, Upstash Search uses a 75% semantic weight and 25% full-text weight. You can adjust this balance based on your use case:

* Higher semantic weight: Better for conceptual searches and finding related content
* Lower semantic weight: Better for exact keyword matching and technical queries

<CodeGroup>
  ```typescript TypeScript theme={"system"}
  const results = await index.search({
    query: "artificial intelligence concepts",
    semanticWeight: 0.9 // 90% semantic, 10% full-text
  });
  ```

  ```python Python theme={"system"}
  results = index.search(
      query="artificial intelligence concepts",
      semantic_weight=0.9  # 90% semantic, 10% full-text
  )
  ```
</CodeGroup>

### 3. Reranking

The final stage reranks the hybrid search results using AI models. Upstash Search offers two reranking options:

**Advanced Reranking (`reranking: true`)**

* Uses a powerful, state-of-the-art reranking model
* Provides the highest quality results
* Costs \$1 per 1K reranking operations
* Recommended for applications where search quality is critical

**Standard Reranking (`reranking: false`, default)**

* Uses a simpler, faster reranking model
* Still provides significant improvements over raw hybrid results
* No additional cost

<CodeGroup>
  ```typescript TypeScript theme={"system"}
  const results = await index.search({
    query: "complex technical documentation",
    reranking: true // uses premium reranking model
  });
  ```

  ```python Python theme={"system"}
  results = index.search(
      query="complex technical documentation",
      reranking=True  # uses premium reranking model
  )
  ```
</CodeGroup>

## Conclusion

This three-stage approach ensures that Upstash Search:

* **Understands Intent**: Input enrichment helps the system understand what users are really looking for
* **Finds Relevant Content**: Hybrid search captures both semantic meaning and exact keyword matches
* **Prioritizes Quality**: Reranking ensures the most relevant results appear first
* **Stays Flexible**: Each stage can be configured based on your specific needs

The result is a search system that works well across all kinds of content and domains, handling everything from precise technical queries to broad conceptual searches.