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
- Boolean Operators
- Comparison Operators
- Grouping and Ordering
- Fields and Ranges
- File Types
- Fuzzy Search
- Limit and Offset
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:
- Find all Go files in the
src
directory containing the worderror
:
ext:go path:src/ error
- Find all files containing the word
error
orwarn
in thesrc
ortests
directories, excluding files containing the wordtest
:
(path:"src/*/*.go" OR path:"tests/*/*.go") AND (error OR warn) NOT test
- Find all files containing the word
error
orwarn
in thesrc
ortests
directories, excluding files containing the wordtest
, sorted by name:
(path:"src/*/*.go" OR path:"tests/*/*.go") AND (error OR warn) NOT test | sort(name)
References
- Full Grammar | Tremor
- Grafana datasource for SAP HANA® | Grafana Enterprise plugins documentation
- JSON model | Grafana Cloud documentation
- JSON model | Grafana documentation
- Evolution of Telemetry at Bloomberg | Grafana Labs
- Query Grammar | Tremor
- Install on Istio | Grafana Loki documentation
- werf kubectl get | Command line interface | Reference | werf
- Use Zoekt For code search | GitLab
- What is a database query? SQL and NoSQL queries explained | Educative.io
- How To SELECT Rows FROM Tables in SQL | DigitalOcean
- Modular Queries | Tremor
- Tremor-Query | Tremor