···
1
1
-
# Query syntax
1
1
+
# Zoekt Query Language Guide
2
2
+
3
3
+
This guide explains the Zoekt query language, used for searching text within Git repositories. Zoekt queries allow combining multiple filters and expressions using logical operators, negations, and grouping. Here's how to craft queries effectively.
4
4
+
5
5
+
---
6
6
+
7
7
+
## Syntax Overview
8
8
+
9
9
+
A query is made up of expressions. An **expression** can be:
10
10
+
- A negation (e.g., `-`),
11
11
+
- A field (e.g., `repo:`).
12
12
+
- A grouping (e.g., parentheses `()`),
13
13
+
14
14
+
Logical `OR` operations combine multiple expressions. The **`AND` operator is implicit**, meaning multiple expressions written together will be automatically treated as `AND`.
15
15
+
16
16
+
---
17
17
+
18
18
+
## Query Components
19
19
+
20
20
+
### 1. **Fields**
21
21
+
22
22
+
Fields restrict your query to specific criteria. Here's a list of fields and their usage:
23
23
+
24
24
+
| Field | Aliases | Values | Description | Examples |
25
25
+
|--------------|---------|------------------------|------------------------------------------------------------|----------------------------------------|
26
26
+
| `archived:` | `a:` | `yes` or `no` | Filters archived repositories. | `archived:yes` |
27
27
+
| `case:` | `c:` | `yes`, `no`, or `auto` | Matches case-sensitive or insensitive text. | `case:yes content:"Foo"` |
28
28
+
| `content:` | `c:` | Text (string or regex) | Searches content of files. | `content:"search term"` |
29
29
+
| `file:` | `f:` | Text (string or regex) | Searches file names. | `file:"main.go"` |
30
30
+
| `fork:` | `f:` | `yes` or `no` | Filters forked repositories. | `fork:no` |
31
31
+
| `lang:` | `l:` | Text | Filters by programming language. | `lang:python` |
32
32
+
| `public:` | | `yes` or `no` | Filters public repositories. | `public:yes` |
33
33
+
| `regex:` | | Regex pattern | Matches content using a regular expression. | `regex:/foo.*bar/` |
34
34
+
| `repo:` | `r:` | Text (string or regex) | Filters repositories by name. | `repo:"github.com/user/project"` |
35
35
+
| `sym:` | | Text | Searches for symbol names. | `sym:"MyFunction"` |
36
36
+
| `branch:` | `b:` | Text | Searches within a specific branch. | `branch:main` |
37
37
+
| `type:` | `t:` | `filematch`, `filename`, `file`, or `repo` | Limits result types. | `type:filematch` |
38
38
+
39
39
+
---
40
40
+
41
41
+
### 2. **Negation**
42
42
+
43
43
+
Negate an expression using the `-` symbol.
44
44
+
45
45
+
#### Examples:
46
46
+
- Exclude a repository:
47
47
+
```plaintext
48
48
+
-repo:"github.com/example/repo"
49
49
+
```
50
50
+
- Exclude a language:
51
51
+
```plaintext
52
52
+
-lang:javascript
53
53
+
```
54
54
+
55
55
+
---
56
56
+
57
57
+
### 3. **Grouping**
58
58
+
59
59
+
Group queries using parentheses `()` to create complex logic.
60
60
+
61
61
+
#### Examples:
62
62
+
- Match either of two repositories:
63
63
+
```plaintext
64
64
+
(repo:repo1 or repo:repo2)
65
65
+
```
66
66
+
- Find test in either python or javascript files:
67
67
+
```plaintext
68
68
+
content:test (lang:python or lang:javascript)
69
69
+
```
70
70
+
71
71
+
---
72
72
+
73
73
+
### 4. **Logical Operators**
74
74
+
75
75
+
Use `or` to combine multiple expressions.
2
76
3
3
-
Query syntax examples can be found on the index page of a running Zoekt webserver.
4
4
-
These
5
5
-
[example queries can also be viewed in the corresponding HTML template](../web/templates.go#L158).
77
77
+
#### Examples:
78
78
+
- Match files in either of two languages:
79
79
+
```plaintext
80
80
+
lang:go or lang:java
81
81
+
```
82
82
+
83
83
+
`and` boolean operator is applied automatically when expressions are separated by a space.
84
84
+
85
85
+
---
86
86
+
87
87
+
## Special Query Values
88
88
+
89
89
+
- **Boolean Values**:
90
90
+
Use `yes` or `no` for fields like `archived:` or `fork:`.
91
91
+
92
92
+
- **Text Fields**:
93
93
+
Text fields (`content:`, `repo:`, etc.) accept:
94
94
+
- Strings: `"my text"`
95
95
+
- Regular expressions: `/my.*regex/`
96
96
+
97
97
+
- **Escape Characters**:
98
98
+
To include special characters, use backslashes (`\`).
99
99
+
100
100
+
#### Examples:
101
101
+
- Match the string `foo"bar`:
102
102
+
```plaintext
103
103
+
content:"foo\"bar"
104
104
+
```
105
105
+
- Match the regex `foo.*bar`:
106
106
+
```plaintext
107
107
+
content:/foo.*bar/
108
108
+
```
109
109
+
110
110
+
---
111
111
+
112
112
+
## Advanced Examples
113
113
+
114
114
+
1. **Search for content in Python files in public repositories**:
115
115
+
```plaintext
116
116
+
lang:python public:yes content:"my_function"
117
117
+
```
118
118
+
119
119
+
2. **Exclude archived repositories and match a regex**:
120
120
+
```plaintext
121
121
+
archived:no regex:/error.*handler/
122
122
+
```
123
123
+
124
124
+
3. **Find files named `README.md` in forks**:
125
125
+
```plaintext
126
126
+
file:"README.md" fork:yes
127
127
+
```
128
128
+
129
129
+
4. **Search for a specific branch**:
130
130
+
```plaintext
131
131
+
branch:main content:"TODO"
132
132
+
```
133
133
+
134
134
+
5. **Combine multiple fields**:
135
135
+
```plaintext
136
136
+
(repo:"github.com/example" or repo:"github.com/test") and lang:go
137
137
+
```
138
138
+
139
139
+
---
140
140
+
141
141
+
## Tips
142
142
+
143
143
+
1. **Combine Filters**: You can combine as many fields as needed. For instance:
144
144
+
```plaintext
145
145
+
repo:"github.com/example" lang:go content:"init"
146
146
+
```
147
147
+
148
148
+
2. **Use Regular Expressions**: Make complex content searches more powerful:
149
149
+
```plaintext
150
150
+
content:/func\s+\w+\s*\(/
151
151
+
```
152
152
+
153
153
+
3. **Case Sensitivity**: Use `case:yes` for exact matches:
154
154
+
```plaintext
155
155
+
case:yes content:"ExactMatch"
156
156
+
```
157
157
+
158
158
+
4. **Match Specific File Types**:
159
159
+
```plaintext
160
160
+
file:".*\.go" content:"package main"
161
161
+
```
162
162
+
163
163
+
### EBNF Summary
164
164
+
165
165
+
```ebnf
166
166
+
query = expression , { "or" , expression } ;
167
167
+
168
168
+
expression = negation
169
169
+
| grouping
170
170
+
| field ;
171
171
+
172
172
+
negation = "-" , expression ;
173
173
+
174
174
+
grouping = "(" , query , ")" ;
175
175
+
176
176
+
field = ( ( "archived:" | "a:" ) , boolean )
177
177
+
| ( ( "case:" | "c:" ) , ("yes" | "no" | "auto") )
178
178
+
| ( ( "content:" | "c:" ) , text )
179
179
+
| ( ( "file:" | "f:" ) , text )
180
180
+
| ( ( "fork:" | "f:" ) , boolean )
181
181
+
| ( ( "lang:" | "l:" ) , text )
182
182
+
| ( ( "public:" ) , boolean )
183
183
+
| ( ( "regex:" ) , text )
184
184
+
| ( ( "repo:" | "r:" ) , text )
185
185
+
| ( ( "sym:" ) , text )
186
186
+
| ( ( "branch:" | "b:" ) , text )
187
187
+
| ( ( "type:" | "t:" ) , type );
188
188
+
189
189
+
boolean = "yes" | "no" ;
190
190
+
text = string | regex ;
191
191
+
string = '"' , { character | escape } , '"' ;
192
192
+
regex = '/' , { character | escape } , '/' ;
193
193
+
194
194
+
type = "filematch" | "filename" | "file" | "repo" ;
195
195
+
```