Skip to content
Apixo
Blog
news· 3 min read· via Towards AI

Benchmarking Legal Semantic Search in Go: Keyword Tuning Beats Expectations

A detailed evaluation of Ukrainian legal search reveals that keyword tuning boosts recall@10 from 0.16 to 0.37 for $0, while embedding models add up to 20 additional points.

Benchmarking Legal Semantic Search in Go: Keyword Tuning Beats Expectations

Evaluating semantic search against real-world data requires establishing rigorous baselines before writing embedding code. A comprehensive technical measurement conducted on a debug corpus of 810 Ukrainian court decisions—comprising 30.86 million characters and 24,716 text chunks—highlights the exact performance and financial tradeoffs between keyword tuning and vector models.

Measuring Keyword Baselines vs. Vector Embeddings

Before deploying vector search, establishing a clear non-model baseline is essential. In the test run, simply dropping the strict "all words" requirement from standard BM25 keyword search dramatically improved recall@10 from 0.16 to 0.37 without using an AI model or spending any money.

Adding vector search on top of the relaxed keyword baseline delivered further gains. Embedding models added an extra 14 to 20 percentage points to recall@10. However, strict pre-established decision rules prevented data misinterpretation: while the highest observed recall@10 reached 0.57, analysis showed this specific peak stemmed from rules invented directly on the test set of 23 queries from a single judge, exposing an instance of data over-fitting.

Architecture, Cyrillic Traps, and Cost Controls

The implementation was built using Go 1.26 using strictly the standard library (net/http and encoding/json), avoiding third-party SDKs. Because vector providers expose standard OpenAI-compatible /embeddings HTTP endpoints, a single client served both local Ollama instances and remote OpenRouter APIs.

Text processing revealed specific character encoding traps in Go when handling Ukrainian texts. Standard byte-length functions (len(s)) undercount Cyrillic strings, which consume two bytes per character in UTF-8. Using utf8.RuneCountInString ensured accurate passage chunking. The system targeted 1,500-character passages with a 2,500-character hard limit, breaking text at sentence boundaries when paragraphs exceeded limits. This produced 24,716 chunks with a median length of 1,312 characters.

Token density varied substantially across model tokenizers:

  • BAAI bge-m3: Averaged 3.92 to 3.95 characters per token. Embedding the full corpus cost $0.079 at $0.010 per 1M tokens.
  • OpenAI text-embedding-3-large: Averaged 1.71 characters per token. Embedding the corpus cost $2.344 at $0.130 per 1M tokens.

By running a 1% sample of the corpus through a local Ollama instance running bge-m3, the local token count accurately predicted the cloud provider's final bill within a single cent ($0.078 projected vs. $0.079 billed). Context window analysis on the same sample showed that only 0.4% of passages (1 out of 248) exceeded 512 tokens.

Vectors were stored in OpenSearch 2.19.7 using the k-NN plugin with a FAISS-backed HNSW index configured for inner product space (m=16, ef_construction=128). Checks confirmed bge-m3 vectors maintained exact unit length of 1.0, while OpenAI vectors shortened to 1,024 dimensions ranged from 0.99932 to 1.00075, resulting in virtually identical top-ten rankings between inner product and cosine calculations on 21 of 23 queries.

To manage paid API runs, an in-code Meter evaluated token-to-character ratios before sending each 64-chunk batch, automatically stopping execution if projected spend exceeded predefined budget caps ($3.00 for OpenAI, $0.50 for BAAI). Resumable state management wrote little-endian float32 binary vectors to disk after logging spend, ensuring crashes never caused under-counted billing logs.

What it means for developers

These measurements offer clear engineering directives for teams implementing search systems:

  1. Fix Keyword Search First: Optimizing query rules and tokenizers in standard engines like OpenSearch can yield massive recall improvements for $0 before introducing expensive vector pipelines.
  2. Protocol Standardization Simplifies Code: Using the standard OpenAI-compatible /embeddings interface allows developers to swap between local execution (via Ollama) and cloud providers seamlessly using native HTTP clients.
  3. Token Density Impacts Costs: Model selection dictates real-world API pricing due to character-per-token variances. Models yielding nearly 4 characters per token cost significantly less to run over large datasets than those yielding under 2 characters per token.
  4. Local Benchmarking Predicts Cloud Bills: Running a small 1% sample locally reveals exact token consumption and context limit boundaries before initiating paid cloud jobs.

For teams evaluating different model architectures, developers can try top AI models cheaply through one API at https://apixoai.online to benchmark vector quality, token density, and retrieval accuracy on their own custom datasets.


Source: Semantic Search Over Court Decisions Without an LLM. Part 2: How I Measured It, in Go — Towards AI. Written by the Apixo team from that report.

#ai-news#golang#opensearch#embeddings#semantic-search#ai
Try it with your own tools

One key for Claude, GPT, GLM, DeepSeek and more. Pay per token with crypto.

Get your API key

Keep reading