Improving BM25 Code Retrieval Under Fixed Generic Tokenization: Adaptive q-Log Odds as a Drop-In BM25 Fix
This paper proposes a drop-in BM25 improvement called adaptive q-Log Odds, which replaces the standard logarithmic IDF with a q-logarithm to significantly enhance code retrieval performance under fixed generic tokenization by better separating identifier tails, while maintaining negligible impact on text retrieval and requiring no changes to query latency.
Original paper licensed under CC BY 4.0 (http://creativecommons.org/licenses/by/4.0/). This is an AI-generated explanation of the paper below. It is not written or endorsed by the authors. For technical accuracy, refer to the original paper. Read full disclaimer
The Problem: The "Lost in Translation" Search
Imagine you are a detective (a coding AI) trying to solve a crime. You have a massive library of 50,000 files, and you need to find the one specific file that contains the clue: a function named handleWebSocketUpgrade.
Your current tool is a standard library search engine (called BM25). This tool was originally designed to search for natural language, like news articles or books. It works well for words like "the," "run," or "happy." But code is different. Code is full of unique, specific names (identifiers) that act like secret codes.
The Issue:
The standard search engine treats a unique code name (like handleWebSocketUpgrade, which appears in only one file) almost the same as a slightly less common name (like logger, which appears in 50 files).
- Analogy: Imagine a library where the librarian gives a "relevance score" to books. If you are looking for a book with a very specific, one-of-a-kind title, the librarian should scream, "THIS IS THE ONE!" But the current librarian whispers, "This is a good book, but so is that other one."
- The Result: The AI gets distracted. It reads the wrong files, gets confused, and fails to fix the bug. The paper argues that the failure isn't the AI's fault; it's the search engine's fault for not valuing the unique "code names" enough.
The Cause: A "Frozen" Dictionary
The authors explain that in many companies, the search engine is built by an infrastructure team using a "frozen" dictionary (tokenizer). This dictionary breaks words down based on how humans speak, not how code is written.
- The Constraint: The people using the search engine (the AI developers) cannot change the dictionary. They are stuck with the "frozen" setup. They need a fix that works without rebuilding the whole library.
The Solution: The "Volume Knob" (q-Log)
The authors propose a clever, one-line math tweak to the search engine's scoring system. They call it Adaptive q-Log Odds.
The Analogy:
Think of the search engine's scoring system as a volume knob for different types of words.
- Common words (like "function" or "return") are turned down low because they appear everywhere.
- Rare words (the unique code names) need to be turned up high.
- The Problem: The standard volume knob (the logarithm) is broken. It turns up the volume on rare words, but not enough. It treats a word appearing once and a word appearing 50 times as almost the same volume.
The Fix:
The authors replace the standard volume knob with a new one called q-log.
- This new knob has a special setting (parameter q) that acts like a "super-amplifier" for the rarest words.
- If you set q = 1, it acts exactly like the old, broken knob (standard BM25).
- If you set q < 1 (like 0.05), it screams "THIS IS THE ONE!" for words that appear only once. It amplifies the difference between a unique identifier and a common one by thousands of times.
How It Works in Practice
The paper tested this on a massive collection of Go language code (182,000 files).
- Before: The search engine found the correct file only 25% of the time in the top 10 results.
- After: With the new "volume knob" turned to the right setting, it found the correct file 48% of the time.
- The Magic: This is an 89% improvement in accuracy. The AI can now find the right file almost twice as often, simply by turning up the volume on the unique code names.
The "Smart" Part: Auto-Tuning
You might ask, "How do we know what setting (q) to use?"
The authors created a simple formula that looks at the library itself to decide the setting automatically.
- The Rule: They count how many "one-of-a-kind" words (hapaxes) exist in the library.
- The Logic:
- If the library is full of unique code names (like Go), the formula sets the volume knob to "Super Amplify" (q = 0.05).
- If the library is mostly common words (like Python or regular text), the formula sets the knob back to "Normal" (q = 1).
- Why this matters: This means the fix works automatically. It doesn't break text searches (where unique words aren't as important) and it doesn't need human experts to tune it for every new project.
The Catch: Tokenizers
The paper also discovered a limit. If you can change the dictionary (tokenizer) to understand code better (splitting handleWebSocketUpgrade into handle, web, socket, upgrade), then the standard search engine works fine, and this special "volume knob" isn't needed.
- The Takeaway: This fix is specifically for situations where you cannot change the dictionary. It is the "best possible fix" for a locked-down system.
Summary
- The Problem: Standard search engines ignore unique code names, making AI coding agents fail.
- The Fix: A mathematical tweak that massively amplifies the importance of words that appear only once.
- The Result: A massive jump in finding the right code files (from ~25% to ~48% success rate in top results).
- The Benefit: It works automatically, requires no changes to the existing search infrastructure, and is free to compute.
In short, the paper teaches us how to turn up the volume on the "secret codes" in a library, ensuring the detective (the AI) hears them clearly and finds the right file.
Drowning in papers in your field?
Get daily digests of the most novel papers matching your research keywords — with technical summaries, in your language.