|
| 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 | +### Adressing structured data |
| 27 | + |
| 28 | +Data stored deep inside nested data structures like `object` or `json` fields can be addressed using dots as separators in the field name. |
| 29 | +For instance, the document `{"product": {"attributes": {color": "red"}}}` is matched by |
| 30 | +``` |
| 31 | +product.attributes.color:red |
| 32 | +``` |
| 33 | + |
| 34 | +If the keys of your object contain dots, the above syntax has some ambiguity : by default `{"k8s.component.name": "quickwit"}` will be matched by |
| 35 | +```k8s.component.name:quickwit``` |
| 36 | + |
| 37 | +It is possible to remove the ambiguity by setting expand_dots in the json field configuration. |
| 38 | +In that case, it will be necessary to escape the `.` in the query to match this document like this : |
| 39 | +``` |
| 40 | +k8s\.component\.name:quickwit |
| 41 | +``` |
| 42 | + |
| 43 | +### Clauses Cheat Sheet |
| 44 | + |
| 45 | +Quickwit support various types of clauses to express different kinds of conditions. Here's a quick overview of them: |
| 46 | + |
| 47 | +| type | syntax | examples | description| `default_search_field`| |
| 48 | +|-------------|--------|----------|------------|-----------------------| |
| 49 | +| 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 | |
| 50 | +| term prefix | `field: prefix*` | `app_name: tant*` <br/> `quick*` | A term clause tests the existence of a token starting with the provided value | yes | |
| 51 | +| 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 | |
| 52 | +| phrase | `field: "sequence of tokens"` | `full_name: "john doe"` | A phrase clause tests the existence of the provided sequence of tokens | yes | |
| 53 | +| 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 | |
| 54 | +| all | `*` | `*` | A match-all clause will match every document | no | |
| 55 | +| exist | `field: *` | `error: *` | An exist clause tests the existence of any value for the field, it will match only if the field exists | no | |
| 56 | +| range | `field: bounds` |`duration: [0 1000}` <br/> `last_name: [banner miller]` | A term clause tests the existence of a token between the provided bounds | no | |
| 57 | + |
| 58 | +## Queries |
| 59 | + |
| 60 | +Clauses can be combined using operators to form more complex queries. |
| 61 | + |
| 62 | +### Combining queries |
| 63 | + |
| 64 | +Clauses can be combined using boolean operators `AND` and `OR` to create search exp |
| 65 | +An `AND` query will match only if conditions on both sides of the operator are met |
| 66 | +``` |
| 67 | +type:rose AND color:red |
| 68 | +``` |
| 69 | + |
| 70 | +An `OR` query will match if either or both conditions on each side of the operator are met |
| 71 | +``` |
| 72 | +weekday:6 OR weekday:7 |
| 73 | +``` |
| 74 | + |
| 75 | +If no operator is provided, `AND` is implicitly assumed. |
| 76 | + |
| 77 | +``` |
| 78 | +type:violet color:blue |
| 79 | +``` |
| 80 | + |
| 81 | +### Grouping queries |
| 82 | +You can build complex expressions by grouping clauses using parentheses. |
| 83 | +``` |
| 84 | +(type:rose AND color:red) OR (type:violet AND color:blue) |
| 85 | +``` |
| 86 | + |
| 87 | +When no parentheses are used, `AND` takes precedence over `OR`, meaning that the following query is equivalent to the one above. |
| 88 | + |
| 89 | +``` |
| 90 | +type:rose AND color:red OR type:violet AND color:blue |
| 91 | +``` |
| 92 | + |
| 93 | +### Negating queries |
| 94 | + |
| 95 | +An expression can be negated either with the operator `NOT` or by prefixing the query with a dash `-`. |
| 96 | + |
| 97 | +`NOT` and `-` take precedence over everything, such that `-a AND b` means `(-a) AND b`, not `-(a AND B)`. |
| 98 | + |
| 99 | +``` |
| 100 | +NOT severity:debug |
| 101 | +``` |
| 102 | + |
| 103 | +or |
| 104 | + |
| 105 | +``` |
| 106 | +type:proposal -(status:rejected OR status:pending) |
| 107 | +``` |
| 108 | + |
| 109 | + |
| 110 | +## Dive deeper |
| 111 | + |
| 112 | +If you want to know more about the query language, head to the [Query Language Reference](/docs/reference/query-language-reference) |
0 commit comments