Experiments with Typesense for local semantic search over the PDFs
contained in the corpus/ directory. Everything runs locally: text extraction, chunking, and
embedding generation (done by Typesense's built-in model), with no external services.
Typesense runs locally in Docker:
docker run -d --name typesense -p 8108:8108 -v typesense-data:/data \
typesense/typesense:30.2 --data-dir /data --api-key=dev-secret-keyChange --api-key if you want a different key, and pass it to the indexer
(see Configuration).
Check that the server is up:
curl http://localhost:8108/health
# {"ok":true}Install the dependencies and index every PDF in corpus/:
pip install -r requirements.txt
python index_corpus.py --recreateExample output:
Creating collection 'pdf_chunks' (embedding model: ts/all-MiniLM-L12-v2)
gpus_for_ray_tracing_and_neural_networks.pdf: 4/4 chunks indexed
neural_networks.pdf: 4/4 chunks indexed
ray_tracing.pdf: 4/4 chunks indexed
three_layers_architecture.pdf: 5/5 chunks indexed
Done: 17/17 chunks from 4 PDFs in collection 'pdf_chunks'
index_corpus.py processes each PDF (searched recursively under corpus/) as follows:
- PDF → text: text is extracted page by page with
pypdf. Words hyphenated across line breaks are re-joined and whitespace is normalized. - Chunking: the text is split into word-based chunks of
--chunk-sizewords (default200). Consecutive chunks share--overlapwords (default50), so that content near a chunk boundary is never lost. Each chunk tracks the pages it spans. - JSON documents: each chunk becomes a JSON document with a reference to the original PDF (see the schema below).
- Indexing: documents are imported in batches (
upsert) into thepdf_chunkscollection. Typesense automatically computes an embedding for each chunk using its built-in local modelts/all-MiniLM-L12-v2. Failed documents are reported and make the script exit with code1.
| Field | Type | Description |
|---|---|---|
id |
string | <doc_id>-<chunk_index>, e.g. 3f2a9c0e1b7d4a6f-00002 |
doc_id |
string | Stable ID of the source PDF (hash of its path relative to corpus/) |
file_name |
string | PDF file name, e.g. ray_tracing.pdf |
file_path |
string | PDF path relative to corpus/ (stored, not indexed) |
title |
string | PDF title from metadata (optional) |
chunk_index |
int32 | Position of the chunk in the PDF (0-based, default sort field) |
page_start |
int32 | First page covered by the chunk (1-based) |
page_end |
int32 | Last page covered by the chunk (1-based) |
text |
string | Chunk text |
embedding |
float[] | Vector auto-generated by Typesense from text |
doc_id and file_name are facetable, so results can be filtered or grouped by source PDF
(e.g. filter_by: "file_name:=ray_tracing.pdf").
python index_corpus.py(without--recreate) keeps the collection and, for each PDF, deletes its previous chunks before importing the new ones: no duplicates and no stale chunks if a PDF shrinks or the chunking parameters change.python index_corpus.py --recreatedrops and recreates the whole collection. This is required when changing the schema or the embedding model (--model).
| Option | Environment variable | Default | Description |
|---|---|---|---|
--corpus |
./corpus |
Directory containing the PDFs | |
--collection |
pdf_chunks |
Typesense collection name | |
--model |
ts/all-MiniLM-L12-v2 |
Typesense built-in embedding model | |
--chunk-size |
200 |
Words per chunk | |
--overlap |
50 |
Words shared by consecutive chunks | |
--batch-size |
100 |
Documents per import request | |
--recreate |
off | Drop and recreate the collection first | |
--host |
TYPESENSE_HOST |
localhost |
Typesense host |
--port |
TYPESENSE_PORT |
8108 |
Typesense port |
--protocol |
TYPESENSE_PROTOCOL |
http |
Typesense protocol |
--api-key |
TYPESENSE_API_KEY |
dev-secret-key |
Typesense API key |
Run python index_corpus.py --help for the full list.
- Scanned (image-only) PDFs have no extractable text and are skipped; OCR is not performed.
- The first run may be slower because Typesense downloads the embedding model.
- Embedding generation runs on CPU inside Typesense, so large corpora may take a while to index.
Semantic search over the embedding field:
import typesense
client = typesense.Client({
"nodes": [{"host": "localhost", "port": 8108, "protocol": "http"}],
"api_key": "dev-secret-key",
})
results = client.collections["pdf_chunks"].documents.search({
"q": "how do graphics cards render light reflections",
"query_by": "embedding", # use "text,embedding" for hybrid keyword + semantic search
"exclude_fields": "embedding",
"per_page": 3,
})
for hit in results["hits"]:
doc = hit["document"]
print(hit["vector_distance"], doc["file_name"], doc["page_start"], doc["page_end"])A minimal web UI (app.py, Flask) to upload and index new PDFs and to search the corpus:
python app.py
# open http://localhost:5000-
Search: type a query and choose the mode:
Semantic: vector search onembedding(results farther than distance0.8are hidden);Hybrid: keyword + semantic (query_by: text,embedding);Keyword: classic full-text search ontext.
Each result shows the source PDF, the page range, the chunk number, the similarity (semantic and hybrid modes) and the chunk text, with keyword matches highlighted. Clicking the file name opens the PDF at the chunk's first page.
-
Upload & index: the uploaded PDF is saved to
corpus/and indexed with the same pipeline asindex_corpus.py. Uploading a file with an existing name replaces it and re-indexes it; if indexing fails, the previous file is restored. Only valid PDFs are accepted (max 50 MB by default). -
Indexed documents: the sidebar lists the indexed PDFs with their chunk counts.
The UI uses the same TYPESENSE_* environment variables as the indexer, plus:
| Environment variable | Default | Description |
|---|---|---|
TYPESENSE_COLLECTION |
pdf_chunks |
Collection to search/index |
TYPESENSE_MODEL |
ts/all-MiniLM-L12-v2 |
Embedding model, used only if the collection must be created |
CORPUS_DIR |
./corpus |
Where uploaded PDFs are saved |
CHUNK_SIZE |
200 |
Words per chunk for uploads |
CHUNK_OVERLAP |
50 |
Overlap in words for uploads |
MAX_UPLOAD_MB |
50 |
Maximum upload size |
PORT |
5000 |
HTTP port |
The server listens on 127.0.0.1 only and uses Flask's development server, which is fine for local
experiments but not for production.