Skip to content

Commit 685142b

Browse files
ddelemenytrinity-1686afmassot
authored
Rewrite Query Language docs (#4726)
* Add query language intro to the docs * Rewrite query language refdoc * Improve QL docs Co-Authored-By: trinity-1686a <[email protected]> * Add precision about sort order for range clauses * Trim unwanted space * Minor change to QL intro --------- Co-authored-by: trinity-1686a <[email protected]> Co-authored-by: François Massot <[email protected]>
1 parent 57eb4f3 commit 685142b

File tree

2 files changed

+275
-59
lines changed

2 files changed

+275
-59
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
---
2+
title: Introduction to Quickwit's query language
3+
sidebar_position: 3
4+
---
5+
6+
Quickwit allows you to search on your indexed documents using a simple query language. Here's a quick overview.
7+
8+
## Clauses
9+
10+
The main concept of this language is a clause, which represents a simple condition that can be tested against documents.
11+
12+
### Querying fields
13+
14+
A clause operates on fields of your document. It has the following syntax :
15+
```
16+
field:condition
17+
```
18+
19+
For example, when searching documents where the field `app_name` contains the token `tantivy`, you would write the following clause:
20+
```
21+
app_name:tantivy
22+
```
23+
24+
In many cases the field name can be omitted, quickwit will then use the `default_search_fields` configured for the index.
25+
26+
### Clauses Cheat Sheet
27+
28+
Quickwit support various types of clauses to express different kinds of conditions. Here's a quick overview of them:
29+
30+
| type | syntax | examples | description| `default_search_field`|
31+
|-------------|--------|----------|------------|-----------------------|
32+
| term | `field:token` | `app_name:tantivy` <br/> `process_id:1234` <br/> `word` | A term clause tests the existence of avalue in the field's tokens | yes |
33+
| term prefix | `field:prefix*` | `app_name:tant*` <br/> `quick*` | A term clause tests the existence of a token starting with the provided value | yes |
34+
| term set | `field:IN [token token ..]` |`severity:IN [error warn]` | A term set clause tests the existence of any of the provided value in the field's tokens| yes |
35+
| phrase | `field:"sequence of tokens"` | `full_name:"john doe"` | A phrase clause tests the existence of the provided sequence of tokens | yes |
36+
| phrase prefix | `field:"sequence of tokens"*` | `title:"how to m"*` | A phrase prefix clause tests the exsitence of a sequence of tokens, the last one used like in a prefix clause | yes |
37+
| all | `*` | `*` | A match-all clause will match every document | no |
38+
| exist | `field:*` | `error:*` | An exist clause tests the existence of any value for the field, it will match only if the field exists | no |
39+
| range | `field:bounds` |`duration:[0 TO 1000}` <br/> `last_name:[banner TO miller]` | A term clause tests the existence of a token between the provided bounds | no |
40+
41+
## Queries
42+
43+
### Combining queries
44+
45+
Clauses can be combined using boolean operators `AND` and `OR` to create more complex search expressions
46+
An `AND` query will match only if conditions on both sides of the operator are met
47+
```
48+
type:rose AND color:red
49+
```
50+
51+
An `OR` query will match if either or both conditions on each side of the operator are met
52+
```
53+
weekday:6 OR weekday:7
54+
```
55+
56+
If no operator is provided, `AND` is implicitly assumed.
57+
58+
```
59+
type:violet color:blue
60+
```
61+
62+
### Grouping queries
63+
You can build complex expressions by grouping clauses using parentheses.
64+
```
65+
(type:rose AND color:red) OR (type:violet AND color:blue)
66+
```
67+
68+
When no parentheses are used, `AND` takes precedence over `OR`, meaning that the following query is equivalent to the one above.
69+
70+
```
71+
type:rose AND color:red OR type:violet AND color:blue
72+
```
73+
74+
### Negating queries
75+
76+
An expression can be negated either with the operator `NOT` or by prefixing the query with a dash `-`.
77+
78+
`NOT` and `-` take precedence over everything, such that `-a AND b` means `(-a) AND b`, not `-(a AND B)`.
79+
80+
```
81+
NOT severity:debug
82+
```
83+
84+
or
85+
86+
```
87+
type:proposal -(status:rejected OR status:pending)
88+
```
89+
90+
91+
## Dive deeper
92+
93+
If you want to know more about the query language, head to the [Query Language Reference](/docs/reference/query-language-reference)

docs/reference/query-language.md

Lines changed: 182 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,231 @@
11
---
2-
title: Query language
2+
title: Query Language Reference
33
sidebar_position: 40
44
---
55

6-
Quickwit uses a query mini-language which is used by providing a `query` parameter to the search endpoints.
6+
## Pseudo-grammar
77

8-
### Terms
8+
```
9+
query = '(' query ')'
10+
| query operator query
11+
| unary_operator query
12+
| query query
13+
| clause
14+
15+
operator = 'AND' | 'OR'
16+
17+
unary_operator = 'NOT' | '-'
18+
19+
clause = field_name ':' field_clause
20+
| defaultable_clause
21+
| '*'
922
10-
The `query` is parsed into a series of terms and operators. There are two types of terms: single terms such as “tantivy” and phrases which is a group of words surrounded by double quotes such as “hello world”.
23+
field_clause = term | term_prefix | term_set | phrase | phrase_prefix | range | '*'
24+
defaultable_clause = term | term_prefix | term_set | phrase | phrase_prefix
25+
```
26+
---
27+
## Writing Queries
28+
### Escaping Special Characters
1129

12-
Multiple terms can be combined together with Boolean operators `AND, OR` to form a more complex query. By default, terms will be combined with the `AND` operator.
30+
Some characters need to be escaped in non quoted terms because they are syntactically significant otherwise: special reserved characters are: `+` , `^`, `` ` ``, `:`, `{`, `}`, `"`, `[`, `]`, `(`, `)`, `~`, `!`, `\\`, `*`, `SPACE`. If such such characters appear in query terms, they need to be escaped by prefixing them with an anti-slash `\`.
1331

14-
IP addresses can be provided as IpV4 or IpV6. It is recommended to use the same format as in the indexed documents.
32+
In quoted terms, the quote character in use `'` or `"` needs to be escaped.
1533

16-
### Fields
34+
###### Allowed characters in field names
1735

18-
You can specify fields to search in the query by following the syntax `field_name:term`.
36+
See the [Field name validation rules](https://quickwit.io/docs/configuration/index-config#field-name-validation-rules) in the index config documentation.
1937

20-
For example, let's assume an index that contains two fields, `title`, and `body` with `body` the default field. To search for the phrase “Barack Obama” in the title AND “president” in the body, you can enter:
38+
### Addressing nested structures
2139

40+
Data stored deep inside nested data structures like `object` or `json` fields can be addressed using dots as separators in the field name.
41+
For instance, the document `{"product": {"attributes": {color": "red"}}}` is matched by
2242
```
23-
title:"barack obama" AND president
43+
product.attributes.color:red
2444
```
2545

26-
Note that a query like `title:barack obama` will find only `barack` in the title and `obama` in the default fields. If no default field has been set on the index, this will result in an error.
46+
If the keys of your object contain dots, the above syntax has some ambiguity : by default `{"k8s.component.name": "quickwit"}` will be matched by
47+
```k8s.component.name:quickwit```
2748

28-
### Searching structures nested in documents.
49+
It is possible to remove the ambiguity by setting expand_dots in the json field configuration.
50+
In that case, it will be necessary to escape the `.` in the query to match this document like this :
51+
```
52+
k8s\.component\.name:quickwit
53+
```
2954

30-
Quickwit is designed to index structured data.
31-
If you search into some object nested into your document, whether it is an `object`, a `json` object, or whether it was caught through the `dynamic` mode, the query language is the same. You simply need to chain the different steps to reach your value from the root of the document.
55+
---
3256

33-
For instance, the document `{"product": {"attributes": {color": "red"}}}` is returned if you query `product.attributes.color:red`.
57+
## Structured data
58+
### Datetime
59+
Datetime values must be provided in rfc3339 format, such as `1970-01-01T00:00:00Z`
3460

35-
If a dot `.` exists in one of the key of your object, the above syntax has some ambiguity.
36-
For instance, by default, `{"k8s.component.name": "quickwit"}` will be matched by `k8s.component.name:quickwit`.
61+
### IP addresses
62+
IP addresses can be provided as IPv4 or IPv6. It is recommended to search with the format used when indexing documents.
63+
There is no support for searching for a range of IP using CIDR notation, but you can use normal range queries.
3764

38-
It is possible to remove the ambiguity by setting `expand_dots` in the json field configuration.
39-
In that case, it will be necessary to escape the `.` in the query to match this document.
65+
---
4066

41-
For instance, the above document will match the query `k8s\.component\.name:quickwit`.
67+
## Types of clauses
4268

43-
### Boolean Operators
69+
### Term `field:term`
70+
```
71+
term = term_char+
72+
```
4473

45-
Quickwit supports `AND`, `+`, `OR`, `NOT` and `-` as Boolean operators (case sensitive). By default, the `AND` is chosen, this means that if you omit it in a query like `title:"barack obama" president` Quickwit will interpret the query as `title:"barack obama" AND president`.
74+
Matches documents if the targeted field contains a token equal to the provided term.
4675

47-
### Grouping boolean operators
76+
`field:value` will match any document where the field 'field' has a token 'value'.
4877

49-
Quickwit supports parenthesis to group multiple clauses:
78+
### Term Prefix `field:prefix*`
79+
```
80+
term_prefix = term '*'
81+
```
5082

83+
Matches documents if the targeted field contains a token which starts with the provided value.
84+
85+
`field:quick*` will match any document where the field 'field' has a token like `quickwit` or `quickstart`, but not `qui` or `abcd`.
86+
87+
### Term set `field:IN [a b c]`
5188
```
52-
(color:red OR color:green) AND size:large
89+
term_set = 'IN' '[' term_list ']'
90+
term_list = term_list term
91+
| term
5392
```
93+
Matches if the document contains any of the tokens provided.
94+
95+
###### Examples
96+
`field:IN [ab cd]` will match 'ab' or 'cd', but nothing else.
97+
98+
###### Perfomance Note
99+
This is a lot like writing `field:ab OR field:cd`. When there are only a handful of terms to search for, using ORs is usually faster.
100+
When there are many values to match, a term set query can become more efficient.
54101

55-
### Slop Operator
102+
<!-- previously a field was required. It looks like it may no longer be the case -->
56103

57-
Quickwit also supports phrase queries with a slop parameter using the slop operator `~` followed by the value of the slop.
58-
The query will match phrases if its terms are separated by slop terms at most.
104+
### Phrase `field:"sequence of words"`
105+
```
106+
phrase = phrase_string
107+
| phrase_string slop
108+
phrase_string = '"' phrase_char '"'
109+
slop = '~' [01-9]+
110+
111+
```
59112

60-
The slop can be considered a budget between all terms. E.g. `"A B C"~1` matches `"A X B C"`, `"A B X C"`, but not `"A X B X C"`.
113+
Matches if the field contains the sequence of token provided. `field:"looks good to me"` will match any document containing that sequence of tokens.
114+
The field must have been configured with `record: position` when indexing.
61115

62-
Transposition costs 2, e.g. `"A B"~1` will not match `"B A"` but it would with `"A B"~2`.
116+
###### Slop operator
117+
Is is also possible to add a slop, which allow matching a sequence with some distance. For instance `"looks to me"~1` will match "looks good to me", but not "looks very good to me".
118+
Transposition costs 2, e.g. `"A B"~1` will not match `"B A"` but it would with `"A B"~2`.
63119
Transposition is not a special case, in the example above A is moved 1 position and B is moved 1 position, so the slop is 2.
64120

65-
:::caution
66-
Slop queries can only be used on field indexed with the [record option](./../configuration/index-config.md#text-type) set to `position` value.
67-
:::
121+
### Phrase Prefix `field:"finish this phr"*`
122+
```
123+
phrase_prefix = phrase '*'
124+
```
125+
126+
Matches if the field contains the sequence of token provided, where the last token in the query may be only a prefix of the token in the document.
127+
128+
The field must have been configured with `record: position` when indexing.
129+
130+
There is no slop for phrase prefix queries.
131+
132+
###### Examples
133+
`field:"thanks for your contrib"*` will match 'thanks for your contribution'.
134+
135+
###### Limitation
68136

69-
### Set Operator
137+
Quickwit may trim some results matched by this clause in some cases. If you search for `"thanks for your co"*`, it will enumerate the first 50 tokens which start with "co" (in their storage order), and search for any documents where "thanks for your" is followed by any of these tokens.
70138

71-
Quickwit supports `IN [value1 value2 ...]` as a set membership operator. This is more cpu efficient than the equivalent `OR`ing of many terms, but may download more of the split than `OR`ing, especially when only a few terms are searched. You must specify a field being searched for Set queries.
139+
If there are many tokens starting with "co", "contribution" might not be one of the 50 selected tokens, and the query won't match a document containing "thanks for your contribution". Normal prefix queries don't suffer from this issue.
72140

73-
### Range queries
141+
### Range `field:[low_bound TO high_bound}`
142+
```
143+
range = explicit_range | comparison_half_range
144+
145+
explicit_range = left_bound_char bounds right_bound_char
146+
left_bound_char = '[' | '{'
147+
right_bound_char = '}' | ']'
148+
bounds = term TO term
149+
| term TO '*'
150+
| '*' TO term
151+
152+
comparison_range = comparison_operator term
153+
comparision_operator = '<' | '>' | '<=' | '>='
154+
```
155+
156+
Matches if the document contains a token between the provided bounds for that field.
157+
For range queries, you must provide a field. Quickwit won't use `default_search_fields` automatically.
158+
159+
###### Order
160+
For text fields, the ranges are defined by lexicographic order on uft-8 encoded byte arrays. It means for a text field, 100 is between 1 and 2.
161+
<!-- TODO: Build a more comprehensive example set to showcase how wharacters are sorted -->
162+
163+
When using ranges on integers, it behaves naturally.
164+
165+
###### Inclusive and exclusive bounds
166+
Inclusive bounds are represented by square brackets `[]`. They will match tokens equal to the bound term.
167+
Exclusive bounds are represented by curly brackets `{}`. They will not match tokens equal to the bound term.
74168

75-
Range queries can only be executed on fields with a fast field. Currently only fields of type `ip` are supported.
169+
###### Half-Open bounds
170+
You can make an half open range by using `*` as one of the bounds. `field:[b TO *]` will match 'bb' and 'zz', but not 'ab'.
171+
You can also use a comparison based syntax:`field:<b`, `field:>b`, `field:<=b` or `field:>=b`.
76172

173+
<!-- NOTE : empty values likely not indexed -->
174+
175+
###### Examples
77176
- Inclusive Range: `ip:[127.0.0.1 TO 127.0.0.50]`
78177
- Exclusive Range: `ip:{127.0.0.1 TO 127.0.0.50}`
79178
- Unbounded Inclusive Range: `ip:[127.0.0.1 TO *] or ip:>=127.0.0.1`
80179
- Unbounded Exclusive Range: `ip:{127.0.0.1 TO *] or ip:>127.0.0.1`
81180

82181

83-
#### Examples:
182+
### Exists `field:*`
84183

85-
With the following corpus:
86-
```json
87-
[
88-
{"id": 1, "body": "a red bike"},
89-
{"id": 2, "body": "a small blue bike"},
90-
{"id": 3, "body": "a small, rusty, and yellow bike"},
91-
{"id": 4, "body": "fred's small bike"},
92-
{"id": 5, "body": "a tiny shelter"}
93-
]
94-
```
95-
The following queries will output:
184+
Matches documents where the field is set. You have to specify a field for this query, Quickwit won't use `default_search_fields` automatically.
96185

97-
- `body:"small bird"~2`: no match []
98-
- `body:"red bike"~2`: matches [1]
99-
- `body:"small blue bike"~3`: matches [2]
100-
- `body:"small bike"`: matches [4]
101-
- `body:"small bike"~1`: matches [2, 4]
102-
- `body:"small bike"~2`: matches [2, 4]
103-
- `body:"small bike"~3`: matches [2, 3, 4]
104-
- `body: IN [small tiny]`: matches [2, 3, 4, 5]
186+
### Match All `*`
105187

106-
### Escaping Special Characters
188+
Matches every document. You can't put a field in front. It is simply written as `*`.
189+
190+
---
191+
192+
## Building Queries
193+
Most queries are composed of more than one clause. When doing so, you may add operators between clauses.
194+
195+
Implicitly if no operator is provided, 'AND' is assumed.
196+
197+
### Conjunction `AND`
198+
An `AND` query will match only if both sides match.
199+
200+
<!-- TODO: Formal example ?*-->
201+
202+
### Disjunction `OR`
203+
An `OR` query will match if either (or both) sides match.
204+
205+
<!-- TODO: Formal example ?*-->
206+
207+
### Negation `NOT` or `-`
208+
A `NOT` query will match if the clause it is applied to does not match.
209+
The `-` prefix is equivalent to the `NOT` operator.
210+
211+
### Grouping `()`
212+
Parentheses are used to force the order of evaluation of operators.
213+
For instance, if a query should match if 'field1' is 'one' or 'two', and 'field2' is 'three', you can use `(field1:one OR field1:two) AND field2:three`.
214+
215+
### Operator Precedence
216+
Without parentheses, `AND` takes precedence over `OR`. That is, `a AND b OR c` is interpreted as `(a AND b) or c`.
217+
218+
`NOT` and `-` takes precedence over everything, such that `-a AND b` means `(-a) AND b`, not `-(a AND B)`.
219+
220+
221+
---
222+
223+
## Other considerations
224+
225+
### Default Search Fields
226+
In many case it is possible to omit the field you search if it was configured in the `default_search_fields` array of the index configuration. If more than one field is configured as default, the resulting implicit clauses are combined using a conjunction ('OR').
227+
228+
### Tokenization
229+
Note that the result of a query can depend on the tokenizer used for the field getting searched. Hence this document always speaks of tokens, which may be the exact value the document contain (in case of the raw tokenizer), or a subset of it (for instance any tokenizer cutting on spaces).
107230

108-
Special reserved characters are: `+` , `^`, `` ` ``, `:`, `{`, `}`, `"`, `[`, `]`, `(`, `)`, `~`, `!`, `\\`, `*`, `SPACE`. Such characters can still appear in query terms, but they need to be escaped by an antislash `\` .
231+
<!-- NOTE : should dig deeper ? -->

0 commit comments

Comments
 (0)