46 - Search

Overview

OpenAlgo provides fast symbol search across equity, futures, and options instruments. The search system uses an in-memory cache with database fallback for optimal performance.

Architecture Diagram

┌──────────────────────────────────────────────────────────────────────────────┐
│                          Search Architecture                                  │
└──────────────────────────────────────────────────────────────────────────────┘

┌─────────────────────────────────────────────────────────────────────────────┐
│                           Search Request                                     │
│                                                                              │
│  ┌─────────────────┐  ┌─────────────────┐  ┌─────────────────┐             │
│  │  React UI       │  │   REST API      │  │   MCP Tools     │             │
│  │  /search        │  │   /api/search   │  │   search_inst   │             │
│  └────────┬────────┘  └────────┬────────┘  └────────┬────────┘             │
│           │                    │                    │                       │
│           └────────────────────┼────────────────────┘                       │
│                                │                                             │
│                        Search Service                                        │
│                                │                                             │
└────────────────────────────────┼────────────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────────────────────┐
│                    BrokerSymbolCache (Singleton)                             │
│                                                                              │
│  ┌─────────────────────────────────────────────────────────────────────┐   │
│  │                    In-Memory Data Structures                         │   │
│  │                                                                      │   │
│  │  symbols_list[]     - All symbols for iteration                     │   │
│  │  symbol_index{}     - symbol → data (O(1) lookup)                   │   │
│  │  exchange_index{}   - exchange → [symbols] (filtered search)        │   │
│  │  type_index{}       - instrument_type → [symbols]                   │   │
│  │  expiry_index{}     - underlying → [expiry_dates]                   │   │
│  └─────────────────────────────────────────────────────────────────────┘   │
│                                    │                                         │
│                    ┌───────────────┴───────────────┐                        │
│                    │                               │                         │
│               Cache Hit                       Cache Miss                     │
│                    │                               │                         │
│                    ▼                               ▼                         │
│           Return from memory              Query database                     │
│           (microseconds)                  (milliseconds)                     │
│                                                    │                         │
│                                                    ▼                         │
│                                           Update cache                       │
│                                                                              │
└─────────────────────────────────────────────────────────────────────────────┘

Cache Architecture

Singleton Pattern

Index Structures

Search Types

FNO Search with Filters

Exact Lookup (O(1))

Database Fallback

Cache Loading

Initial Load

Cache Refresh

API Endpoints

Search Symbols

Response:

Search F&O

Response:

Get Expiries

Response:

Frontend Integration

Search Component

F&O Filter Component

Performance Characteristics

Operation
Time Complexity
Notes

Exact lookup

O(1)

Hash map access

Prefix search

O(n)

Linear scan with filter

Exchange filter

O(k)

k = symbols in exchange

F&O filter

O(k × m)

k = candidates, m = filters

Cache load

O(n)

n = total symbols

Key Files Reference

File
Purpose

services/search_service.py

Search logic and cache

database/token_db.py

Symbol database queries

restx_api/search.py

Search API endpoints

frontend/src/components/SymbolSearch.tsx

Search UI component

frontend/src/pages/FnOChain.tsx

F&O search interface

Last updated