Query Language - sourcegraph/zoekt

This document explains the query language used in the Zoekt project, which is a code search engine developed by Sourcegraph. The query language is used to efficiently retrieve relevant results based on keywords, file types, and other criteria. The following sections cover the possible options and provide examples for each option, with sources quoted from the provided documentation and code snippets.

Table of Contents

  1. Introduction to Query Language
  2. Syntax and Operators
  1. Examples
  2. References

Introduction to Query Language

A query language is a formal language for making queries to a database. It allows users to define and execute queries in a structured way, making it easier to retrieve and manipulate data. Zoekt uses a custom query language designed for efficient code search.

Syntax and Operators

Boolean Operators

Boolean operators allow combining multiple conditions using logical operators AND, OR, and NOT.

Example:

main AND (error OR warn) NOT test

Comparison Operators

Comparison operators allow comparing values using operators like =, !=, <, >, <=, and >=.

Example:

name > "z"

Grouping and Ordering

Grouping and ordering allow users to control the order of evaluation and the sorting of results.

Example:

(path:"src/*/*.go" OR path:"tests/*/*.go") AND name >= "test" | sort(name)

Fields and Ranges

Fields and ranges allow users to filter results based on specific fields and ranges of values.

Example:

age:[18 TO 30]

File Types

File types allow users to filter results based on the file type.

Example:

ext:go

Fuzzy Search

Fuzzy search allows users to find matches even if the query is not an exact match.

Example:

~func main

Limit and Offset

Limit and offset allow users to control the number of results and the starting point.

Example:

limit:10 offset:20

Examples

Here are some examples of Zoekt query language in action:

  1. Find all Go files in the src directory containing the word error:
ext:go path:src/ error
  1. Find all files containing the word error or warn in the src or tests directories, excluding files containing the word test:
(path:"src/*/*.go" OR path:"tests/*/*.go") AND (error OR warn) NOT test
  1. Find all files containing the word error or warn in the src or tests directories, excluding files containing the word test, sorted by name:
(path:"src/*/*.go" OR path:"tests/*/*.go") AND (error OR warn) NOT test | sort(name)

References