OASIS: Order-Augmented Strategy for Improved Code Search

Zuchen Gao, Zizheng Zhan, Xianming Li, Erxin Yu, Haotian Zhang, Chenbin Chenbin, Yuqun Zhang, Jing Li

Abstract

Code embeddings capture the semantic representations of code and are crucial for various code-related large language model (LLM) applications, such as code search. Previous training primarily relies on optimizing the InfoNCE loss by comparing positive natural language (NL)-code pairs with in-batch negatives. However, due to the sparse nature of code contexts, training solely by comparing the major differences between positive and negative pairs may fail to capture deeper semantic nuances. To address this issue, we propose a novel order-augmented strategy for improved code search (OASIS). It leverages order-based similarity labels to train models to capture subtle differences in similarity among negative pairs. Extensive benchmark evaluations demonstrate that our OASIS model significantly outperforms previous state-of-the-art models focusing solely on major positive-negative differences. It underscores the value of exploiting subtle differences among negative pairs with order labels for effective code embedding training.

Introduction

In the realm of software engineering, code search—the ability to find relevant code snippets using natural language queries—is a fundamental task. Modern approaches rely on code embeddings, where both queries and code snippets are mapped into a shared vector space. Traditionally, these models are trained using contrastive learning, specifically the InfoNCE loss, which treats training as a binary classification problem: pulling the "correct" code snippet closer to the query while pushing all other "negative" snippets away.

However, this binary approach often fails to account for the "sparse" nature of code. Unlike natural language, where synonyms are common, code logic can be completely altered by a single character change (e.g., changing a > to a <). Standard models frequently fall into the trap of superficial similarity, ranking snippets highly based on shared keywords rather than logical relevance.

The Challenge of Sparse Code Context

An illustration of the code search challenge. While multiple functions (1 and 2) may share keywords with the query, only snippet 3 provides the precise logical implementation required.

As shown in Figure 1, a query like "Get a parameter from a layer in the network" might return several functions that share keywords like "parameter" or "layer." However, the actual logic required might be buried in a function with a less obvious name, such as find. Standard contrastive learning pushes all non-target snippets away with equal force, ignoring the fact that some "wrong" snippets are semantically much closer to the target than others. By failing to distinguish between a "hard negative" (a snippet that is logically related but incorrect) and a "random negative" (completely unrelated code), models miss critical semantic nuances.

The OASIS Framework

To address these limitations, we propose OASIS (Order-Augmented Strategy for Improved code Search). OASIS moves beyond binary labels to utilize "order-based" similarity rankings, teaching the model the relative hierarchy of code snippets.

The OASIS training pipeline, featuring NL generation, similarity refinement, and hybrid order-based training.

As detailed in Figure 2, the OASIS framework consists of a three-step pipeline:

  1. Similarity Annotation: We utilize LLMs and repo-level program analysis (examining function callers and callees) to generate high-fidelity docstrings. These are paired with functions from the same repository to create a pool of "hard negatives"—snippets that are contextually related but not the exact match.
  2. Similarity Refinement: To ensure the accuracy of similarity labels, we employ a multi-stage filtering process. We use a Gaussian Mixture Model (GMM) to identify "suspicious" pairs where a negative snippet has an unexpectedly high similarity score. We also calculate Abstract Syntax Tree (AST) edit distances to find structurally identical snippets. Finally, an LLM acts as a judge to decide if these suspicious pairs are "hidden positives" or merely highly similar negatives, adjusting the scores accordingly.
  3. Hybrid Training: The model is trained using a combination of two losses. The standard InfoNCE loss maintains absolute positive-negative separation, while the CoSENT (Cosine Sentence) loss focuses on relative ranking. CoSENT ensures that if Snippet A is labeled as more similar to the query than Snippet B, the model’s embeddings reflect that specific hierarchy.

Experimental Evaluation

We trained OASIS on a massive dataset of 53 million NL-Code pairs across 9 programming languages. Evaluation was conducted across several major benchmarks, including CoSQA, AdvTest, and CodeSearchNet (CSN).

OASIS, utilizing a 1.5B parameter backbone, achieved a new state-of-the-art (SOTA) performance:

  • Outperforming Closed-Source Models: OASIS surpassed OpenAI’s text-embedding-3-large by an average of ~2% on NL2Code tasks and a significant ~18% on Code2Code tasks.
  • Semantic Robustness: On the AdvTest dataset—where identifiers are renamed to prevent keyword matching—OASIS demonstrated superior semantic understanding compared to previous open-source models like CodeSage.
  • Hard Sample Handling: On a "Hard Subset" of Python queries, OASIS improved the Mean Reciprocal Rank (MRR) by over 5%, proving its ability to navigate complex and ambiguous search spaces.

Conclusion

OASIS demonstrates that the relationship between negative samples contains rich semantic information that has been largely ignored in code embedding training. By leveraging order-augmented labels and a refined similarity pipeline, OASIS captures the subtle logical nuances of code, significantly advancing the state-of-the-art in code search and retrieval. Future work will explore scaling this strategy to even larger model architectures and utilizing open-source LLMs for the refinement phase to increase pipeline accessibility.

Summary updated

BibTeX
@inproceedings{gao2025oasis,
  author = {Zuchen Gao and Zizheng Zhan and Xianming Li and Erxin Yu and Haotian Zhang and Chenbin Chenbin and Yuqun Zhang and Jing Li},
  title = {{OASIS: Order-Augmented Strategy for Improved Code Search}},
  booktitle = {Annual Meeting of the Association for Computational Linguistics},
  year = {2025},
  url = {https://openreview.net/forum?id=Hjg0dIuf5v},
}
Figure