CodeMatcher: Searching Code Based on Sequential Semantics of Important Query Words

Chao Liu, Xin Xia, David Lo, Zhiwei Liu, Ahmed E. Hassan, Shanping Li · DOI · OpenReview

Abstract

To accelerate software development, developers frequently search and reuse existing code snippets from large-scale codebases like GitHub. While traditional Information Retrieval (IR) models often fail to bridge the semantic gap between queries and code, and Deep Learning (DL) models are computationally expensive, we propose CodeMatcher. This IR-based model inherits the advantages of DL—specifically the ability to understand sequential semantics and filter noisy keywords—while leveraging indexing to achieve search speeds over 1,200 times faster than state-of-the-art DL models. Evaluated on a codebase of 17 million methods, CodeMatcher achieved a Mean Reciprocal Rank (MRR) of 0.60, significantly outperforming DeepCS, CodeHow, and commercial search engines like GitHub and Google.

Developers face two primary hurdles when searching for code: the semantic gap and the representation gap. Traditional IR tools often treat code as plain text, ignoring the critical importance of word order (e.g., "convert string to int" vs. "convert int to string"). Conversely, DL models like DeepCS attempt to bridge this gap via shared vector spaces but suffer from high computational costs, the need for massive training data, and "out-of-vocabulary" issues.

CodeMatcher was designed to provide the "intelligence" of a deep learning model—understanding sequence and word importance—with the speed and flexibility of a traditional search engine.

The CodeMatcher Architecture

CodeMatcher operates in two main phases: Indexing and Searching. The system uses JAnalyzer to parse Java source code into Abstract Syntax Trees (ASTs), extracting method names and bodies for indexing via Elasticsearch.

The CodeMatcher workflow, illustrating the pipeline from query understanding and codebase preprocessing to searching and reranking.

Query Understanding and Word Importance

Instead of treating every word in a user's query equally, CodeMatcher identifies "important" words using Part-of-Speech (POS) tagging and a hierarchical classification system. It prioritizes JDK-specific nouns and verbs over generic adjectives or adverbs to filter out noise that typically confuses search engines.

The five-level hierarchy used by CodeMatcher to determine the importance of query words based on their linguistic and technical roles.

Sequential Semantics and Reranking

To capture sequential semantics without a neural network, CodeMatcher performs an iterative "fuzzy" search using regular expressions (e.g., .*word1.*word2.*). This ensures that returned code snippets contain keywords in the same relative order as the query. Finally, candidates are reranked based on two metrics:

  • $S_{name}$: Matching quality against the method name.
  • $S_{body}$: Matching quality against the sequence of APIs used in the method body, with extra weight given to standard JDK APIs.

Experimental Results

The authors evaluated CodeMatcher against a dataset of ~41,000 Java repositories containing approximately 17 million methods.

  • Accuracy: CodeMatcher achieved an MRR of 0.60, outperforming the DL-based DeepCS by 82% and the IR-based CodeHow by 62%.
  • Efficiency: Search response time was approximately 0.3 seconds per query, representing a 1,200x speedup over DeepCS (which averaged 376 seconds per query).
  • Real-world Benchmarks: CodeMatcher outperformed native GitHub Search and Google Search (restricted to GitHub) by 46% and 33%, respectively.

Key Insights and Future Work

CodeMatcher demonstrates that method names serve as the strongest bridge between natural language and code; well-named methods (e.g., readTextLineByLine) often mirror natural language queries perfectly. While the current implementation is Java-specific and relies on external NLP parsers, the results suggest that the future of code search lies in fusing the transparency and speed of IR with the semantic depth of DL. Future work will focus on expanding language support and refining POS tagging for technical terminology.

Summary updated

BibTeX
@article{liu2021codematcher,
  author = {Chao Liu and Xin Xia and David Lo and Zhiwei Liu and Ahmed E. Hassan and Shanping Li},
  title = {{CodeMatcher: Searching Code Based on Sequential Semantics of Important Query Words}},
  journal = {ACM Transactions on Software Engineering and Methodology},
  year = {2021},
  doi = {10.1145/3465403},
}
Figure