Skip to content

Components

This page describes each component of the Code Search system in detail.

The API server is the central hub for all client interactions.

  • Serve REST API endpoints
  • Handle search requests
  • Manage connections and repositories
  • Queue indexing jobs
  • Serve the web UI

All API endpoints are prefixed with /api/v1/:

EndpointDescription
/api/v1/searchCode search
/api/v1/reposRepository management
/api/v1/connectionsConnection management
/api/v1/jobsJob monitoring
server:
addr: ":8080"
read_timeout: 15s
write_timeout: 60s
GET /health

Returns 200 OK when healthy.

The indexer runs as a separate process and handles all repository operations.

  • Discover repositories from connections
  • Clone and update repositories
  • Build search indexes
  • Process job queue

Manages periodic tasks:

  • Connection syncing (repository discovery)
  • Repository updates (git fetch)
  • Index rebuilding

Concurrent job processing:

  • Configurable worker count
  • Job prioritization
  • Failure handling

Git operations:

  • Initial clone
  • Incremental fetch
  • Branch management

Search index creation:

  • File analysis
  • Language detection
  • Zoekt index building
indexer:
concurrency: 2
index_path: "./data/index"
repos_path: "./data/repos"
reindex_interval: 1h
zoekt_bin: "zoekt-git-index"

Zoekt is the search engine powering Code Search.

Zoekt is a fast text search engine intended for source code, originally developed by Google. It uses trigram indexing for fast substring matches.

  1. Indexing: Files are split into trigrams (3-character sequences)
  2. Index: Trigrams are stored in an inverted index
  3. Search: Query trigrams are matched against the index
  4. Ranking: Results are ranked by match quality
./data/
├── repos/
│ ├── github.com/
│ │ └── myorg/
│ │ └── api/
│ │ └── .git/
│ └── gitlab.com/
│ └── ...
└── index/
└── myorg_api_v1.zoekt
zoekt:
url: "http://localhost:6070"
index_path: "./data/index"
shards: 0

Code Search supports both PostgreSQL and MySQL for storing metadata. The database driver is auto-detected from the connection URL.

Connections Table

CREATE TABLE connections (
id SERIAL PRIMARY KEY,
name VARCHAR(255) UNIQUE NOT NULL,
type VARCHAR(50) NOT NULL,
url TEXT,
token_encrypted BYTEA,
config JSONB,
status VARCHAR(50),
last_synced_at TIMESTAMP,
created_at TIMESTAMP DEFAULT NOW()
);

Repositories Table

CREATE TABLE repositories (
id SERIAL PRIMARY KEY,
connection_id INTEGER REFERENCES connections(id),
name VARCHAR(255) NOT NULL,
url TEXT NOT NULL,
default_branch VARCHAR(255),
status VARCHAR(50),
last_indexed_at TIMESTAMP,
created_at TIMESTAMP DEFAULT NOW()
);

Jobs Table

CREATE TABLE jobs (
id SERIAL PRIMARY KEY,
type VARCHAR(50) NOT NULL,
status VARCHAR(50) NOT NULL,
repository_id INTEGER REFERENCES repositories(id),
payload JSONB,
error TEXT,
created_at TIMESTAMP DEFAULT NOW(),
started_at TIMESTAMP,
finished_at TIMESTAMP
);
database:
url: "postgres://codesearch:codesearch@localhost:5432/codesearch?sslmode=disable"
max_open_conns: 25
max_idle_conns: 5
conn_max_lifetime: 5m

Redis provides job queuing and distributed locking.

Jobs are queued in Redis lists:

code-search:queue:pending # Pending jobs
code-search:queue:running # Running jobs

Prevent concurrent operations on the same repository:

code-search:lock:repo:123 # Lock for repo ID 123
redis:
addr: "localhost:6379"
password: ""
db: 0

The web UI is a Next.js application.

PageDescription
/Search interface
/reposRepository list
/connectionsConnection management
/jobsJob monitoring
  • Framework: Next.js 14
  • Styling: TailwindCSS
  • Components: shadcn/ui
  • State: React Query
Terminal window
cd web
pnpm install
pnpm build

The CLI provides command-line access to all features.

cmd/cli/
├── main.go
├── cmd/
│ ├── root.go
│ ├── search.go
│ ├── replace.go
│ ├── repo.go
│ ├── find.go
│ └── config.go
└── client/
└── client.go

The CLI communicates with the API server via HTTP REST.

// Example API call
func (c *Client) Search(ctx context.Context, query string) (*SearchResponse, error) {
resp, err := c.http.Get(c.baseURL + "/api/v1/search?q=" + query)
// ...
}