Searching by Code: A New SearchBySnippet Dataset and SnippeR Retrieval Model for Searching by Code Snippets

Ivan Sedykh, Nikita Sorokin, Dmitry Abulkhanov, Sergey I. Nikolenko, Valentin Malykh

Abstract

Code search is an important and well-studied task, but it usually means searching for code by a text query. We argue that using a code snippet (and possibly an error traceback) as a query while looking for bugfixing instructions and code samples is a natural use case not covered by prior art. Moreover, existing datasets use code comments rather than full-text descriptions as text, making them unsuitable for this use case. We present a new SearchBySnippet dataset implementing the search-by-code use case based on StackOverflow data; we show that on SearchBySnippet, existing architectures fall short of a simple BM25 baseline even after fine-tuning. We present a new single encoder model SnippeR that outperforms several strong baselines on SearchBySnippet with a result of 0.451 Recall@10; we propose the SearchBySnippet dataset and SnippeR as a new important benchmark for code search evaluation.

Introduction

Traditional code search research focuses primarily on the text-to-code paradigm, where natural language queries are used to retrieve relevant code snippets. However, in real-world debugging, developers often start with a failing code snippet and an error traceback. The goal is to find forum posts or documentation that explain the failure and provide a fix. This "search-by-snippet" task is poorly served by existing datasets like CodeSearchNet, which pair clean function bodies with short docstrings rather than the messy, conversational text found in developer forums.

The search-by-code workflow using code snippets and error tracebacks as queries to retrieve relevant forum posts.

The SearchBySnippet Dataset

To address this gap, we introduce SearchBySnippet, a large-scale dataset derived from StackOverflow (2014–2021) focused on Python. The dataset contains nearly 1 million entries, extracting specific fields for code, error tracebacks, and accepted answers. Notably, the text fields in SearchBySnippet are 3x to 4x longer than those in CodeSearchNet, reflecting the complexity of real-world troubleshooting. For evaluation, we utilize 1,369 pairs of "duplicate" questions, tasking the model with using a snippet from one post to find the answer in its duplicate.

The SnippeR Retrieval Model

We propose SnippeR (Snippet Retrieval), a model based on the GraphCodeBERT (GCB) architecture designed specifically for this task. SnippeR employs a single encoder architecture, mapping code snippets, error messages, and natural language answers into a shared vector space.

To handle the significant length of tracebacks, we implement a "middle-out" truncation strategy, preserving the beginning and end of the error message where critical diagnostic information is typically located. Furthermore, SnippeR utilizes an iterative self-training loop for hard negative mining. An initial version of the model identifies "hard negatives"—documents that are superficially similar but incorrect—which are then used to retrain the model for better discriminative power.

The iterative training loop for SnippeR, featuring answer filtering and the generation of hard negatives to improve retrieval robustness.

Results and Insights

A key finding of our work is the "BM25 Wall." State-of-the-art models like CodeBERT and GraphCodeBERT initially performed poorly, failing to beat a simple keyword-based BM25 baseline. This suggests these models are over-fitted to clean documentation and struggle with the "noisy" text of forums.

SnippeR successfully overcomes this hurdle, outperforming all baselines:

Model Recall@10 Recall@50
GraphCodeBERT (Original) 0.001 0.009
BM25 (Classic Baseline) 0.406 0.562
SnippeR 0.451 0.657

The inclusion of tracebacks as primary query components, rather than noise, proved essential. By parsing error types into specific fields, the model learns to prioritize the root cause of crashes.

Conclusion

While SnippeR establishes a new state-of-the-art for search-by-snippet, the absolute recall numbers (e.g., Recall@5 of ~33%) indicate that this remains a challenging problem. Future work includes expanding the dataset beyond Python to other languages and improving snippet filtering to handle the inherent noise of StackOverflow data. SearchBySnippet and SnippeR serve as a robust benchmark for future developments in code-centric information retrieval.

Summary updated

BibTeX
@inproceedings{sedykh2024searching,
  author = {Ivan Sedykh and Nikita Sorokin and Dmitry Abulkhanov and Sergey I. Nikolenko and Valentin Malykh},
  title = {{Searching by Code: A New SearchBySnippet Dataset and SnippeR Retrieval Model for Searching by Code Snippets}},
  booktitle = {International Conference on Computational Linguistics},
  year = {2024},
  url = {https://openreview.net/forum?id=kqRs4t9c1Q},
}
Figure