How Neurolawyer searches millions of legal documents with YDB vector search

Alexander Zevaykin11 Mar 2026

Problem

Yandex's legal assistant Neurolawyer answers questions with RAG: it finds documents by meaning, adds them to the prompt, and a language model writes the answer. Every query needs the 50 most relevant documents. Users also pick an area of law and a document category, and the document counts behind these filters differ by orders of magnitude. One set of vector index parameters cannot suit them all: small subsets return fewer than 50 vectors, large ones are scanned slowly.

Example from the article: case law on consumer rights protection holds 2.5 million texts, while corporate law legislation holds only 6 thousand.

Solution in brief

Build one vector index tree per filter combination, and create several indexes with different parameters. At query time, pick the index by the size of the filtered subset. Each index is tuned so its last cluster holds roughly 50 to 150 vectors.

Questions this case answers
  • How to retrieve a fixed number of nearest neighbors under metadata filters when the filtered subsets differ in size by orders of magnitude and one set of approximate-index parameters fits none of them
  • How to run filtered vector search for RAG so that every filter combination still returns enough documents
  • How to choose vector index parameters (levels, clusters) when subsets differ in size by orders of magnitude
  • How to keep top-50 vector search fast when the same index settings would be too coarse for small subsets and too fine for large ones

Tools and methods

  • K-means tree vector index — approximate nearest-neighbor search over clustered embeddings
  • Filtering vector index — one tree per unique filter value
  • Family of indexes — one index per subset-size range, each with its own levels and clusters
  • KMeansTreeSearchTopSize — per-query number of nearest clusters visited on each level
  • YDB — the database holding documents and vector indexes
Continue reading — it's free

Google or a one-time email code. No password.