]> git.cworth.org Git - notmuch-wiki/blob - manpages/notmuch-sexp-queries-7.mdwn
e6968ada5ebd74cfcb454c0d432a219831505792
[notmuch-wiki] / manpages / notmuch-sexp-queries-7.mdwn
1 <h1>NOTMUCH-SEXP-QUERIES(7)</h1>
2
3 <h2>NAME</h2>
4 <pre>
5        notmuch-sexp-queries - s-expression syntax for notmuch queries
6 </pre>
7
8 <h2>SYNOPSIS</h2>
9 <pre>
10        <b>notmuch</b> <u>subcommand</u> <b>--query=sexp</b> [option ...]  <b>--</b> &apos;(and (to santa) (date
11        december))&apos;
12 </pre>
13
14 <h2>DESCRIPTION</h2>
15 <pre>
16        Notmuch supports an alternative query syntax based on  <u>S-expressions</u>  .
17        It  can  be selected with the command line <b>--query=sexp</b> or with the ap‐
18        propriate option to the library function <b>notmuch</b>_<b>query</b>_<b>create</b>_<b>with</b>_<b>syn-</b>
19        <b>tax()</b>.   Support for this syntax is currently optional, you can test if
20        your build of notmuch supports it with
21
22           $ notmuch config get built_with.sexp_queries
23 </pre>
24
25 <h3> &nbsp; S-EXPRESSIONS</h3>
26 <pre>
27        An <u>s-expression</u> is either an atom,  or  list  of  whitespace  delimited
28        s-expressions inside parentheses. Atoms are either
29
30        <u>basic</u> <u>value</u>
31           A basic value is an unquoted string containing no whitespace, double
32           quotes, or parentheses.
33
34        <u>quoted</u> <u>string</u>
35           Double quotes (&quot;) delimit strings possibly containing whitespace  or
36           parentheses.  These  can contain double quote characters by escaping
37           with backslash. E.g. <b>&quot;this</b> <b>is</b> <b>a</b> <b>quote</b> <b>\&quot;&quot;</b>.
38 </pre>
39
40 <h3> &nbsp; S-EXPRESSION QUERIES</h3>
41 <pre>
42        An s-expression query is either an atom, the empty list, or a  <u>compound</u>
43        <u>query</u>  consisting  of  a  prefix atom (first element) defining a <u>field</u>,
44        <u>logical</u> <u>operation</u>, or <u>modifier</u>, and 0 or more subqueries.
45
46        <b>*</b>
47           &quot;*&quot; matches any non-empty string in the current field.
48
49        <b>()</b>
50           The empty list matches all messages
51
52        <u>term</u>
53           Match all messages  containing  <u>term</u>,  possibly  after  stemming  or
54           phrase  splitting.  For  discussion  of stemming in notmuch see <a href='../notmuch-search-terms-7/'>not‐</a>
55           <a href='../notmuch-search-terms-7/'>much-search-terms</a>(7). Stemming only applies to unquoted terms (basic
56           values)  in  s-expression queries.  For information on phrase split‐
57           ting see <u>FIELDS</u>.
58
59        <b>(</b> <u>field</u> <u>q</u>1 <u>q</u>2 ... <u>q</u>n <b>)</b>
60           Restrict the queries <u>q</u>1 to <u>q</u>n to <u>field</u>, and combine  with  <u>and</u>  (for
61           most fields) or <u>or</u>. See <u>FIELDS</u> for more information.
62
63        <b>(</b> <u>operator</u> <u>q</u>1 <u>q</u>2 ... <u>q</u>n <b>)</b>
64           Combine queries <u>q</u>1 to <u>q</u>n. Currently supported operators are <b>and</b>, <b>or</b>,
65           and <b>not</b>. <b>(not</b> <u>q</u>1 ... <u>q</u>n <b>)</b> is equivalent to <b>(and</b> <b>(not</b> <u>q</u>1 <b>)</b>  <b>...</b>  <b>(not</b>
66           <u>q</u>n <b>))</b>.
67
68        <b>(</b> <u>modifier</u> <u>q</u>1 <u>q</u>2 ... <u>q</u>n <b>)</b>
69           Combine queries <u>q</u>1 to <u>q</u>n, and reinterpret the result (e.g. as a reg‐
70           ular expression).  See <u>MODIFIERS</u> for more information.
71
72        <b>(macro</b> <b>(</b> <u>p</u>1 ... <u>p</u>n <b>)</b> <b>body)</b>
73           Define saved query with parameter substitution. The syntax is recog‐
74           nized  only  in  saved  s-expression  queries  (see <b>squery.*</b> in <a href='../notmuch-config-1/'>not‐</a>
75           <a href='../notmuch-config-1/'>much-config</a>(1)). Parameter names in <b>body</b> must be prefixed with <b>,</b>  to
76           be expanded (see <u>MACRO</u> <u>EXAMPLES</u>).  Macros may refer to other macros,
77           but only to their own parameters [1].
78 </pre>
79
80 <h3> &nbsp; FIELDS</h3>
81 <pre>
82        <u>Fields</u> [2] correspond to attributes of mail messages. Some are inherent
83        (and  immutable)  like  <b>subject</b>, while others <b>tag</b> and <b>property</b> are set‐
84        table by the user.  Each concrete field in <u>the</u> <u>table</u> <u>below</u> is discussed
85        further  under  &quot;Search  prefixes&quot;  in <a href='../notmuch-search-terms-7/'>notmuch-search-terms</a>(7). The row
86        <u>user</u> refers to user defined fields, described in <a href='../notmuch-config-1/'>notmuch-config</a>(1).
87
88        Most fields are either <u>phrase</u> <u>fields</u>  [3]  (which  match  sequences  of
89        words),  or  <u>term</u> <u>fields</u> [4] (which match exact strings). <u>Phrase</u> <u>split‐</u>
90        <u>ting</u> breaks the term (basic value or quoted string) into words,  ignore
91        punctuation.  Phrase  splitting  is  applied to terms in phrase (proba‐
92        bilistic) fields. Both phrase splitting  and  stemming  apply  only  in
93        phrase fields.
94
95        Each  term or phrase field has an associated combining operator (<b>and</b> or
96        <b>or</b>) used to combine the queries from each element of the  tail  of  the
97        list.  This  is  generally  <b>or</b> for those fields where a message has one
98        such attribute, and <b>and</b> otherwise.
99
100        Term or phrase fields can contain arbitrarily complex queries  made  up
101        from terms, operators, and modifiers, but not other fields.
102 </pre>
103
104 <h3> &nbsp; Fields with supported modifiers</h3>
105 <pre>
106              ┌───────────┬─────────┬────────┬────────┬──────────┬───────┐
107              │field      │ combine │ type   │ expand │ wildcard │ regex │
108              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
109              │<u>none</u>       │ and     │        │ no     │ yes      │ no    │
110              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
111              │<u>user</u>       │ and     │ phrase │ no     │ yes      │ no    │
112              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
113              │attachment │ and     │ phrase │ yes    │ yes      │ no    │
114              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
115              │body       │ and     │ phrase │ no     │ no       │ no    │
116              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
117              │date       │         │ range  │ no     │ no       │ no    │
118              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
119              │folder     │ or      │ phrase │ yes    │ yes      │ yes   │
120              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
121              │from       │ and     │ phrase │ yes    │ yes      │ yes   │
122              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
123              │id         │ or      │ term   │ no     │ yes      │ yes   │
124              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
125              │is         │ and     │ term   │ yes    │ yes      │ yes   │
126              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
127              │lastmod    │         │ range  │ no     │ no       │ no    │
128              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
129              │mid        │ or      │ term   │ no     │ yes      │ yes   │
130              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
131              │mimetype   │ or      │ phrase │ yes    │ yes      │ no    │
132              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
133              │path       │ or      │ term   │ no     │ yes      │ yes   │
134              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
135              │property   │ and     │ term   │ yes    │ yes      │ yes   │
136              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
137              │subject    │ and     │ phrase │ yes    │ yes      │ yes   │
138              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
139              │tag        │ and     │ term   │ yes    │ yes      │ yes   │
140              ├───────────┼─────────┼────────┼────────┼──────────┼───────┤
141              │thread     │ or      │ term   │ yes    │ yes      │ yes   │
142              └───────────┴─────────┴────────┴────────┴──────────┴───────┘
143
144              │to         │ and     │ phrase │ yes    │ yes      │ no    │
145              └───────────┴─────────┴────────┴────────┴──────────┴───────┘
146 </pre>
147
148 <h3> &nbsp; MODIFIERS</h3>
149 <pre>
150        <u>Modifiers</u>  refer  to  any prefixes (first elements of compound queries)
151        that are neither operators nor fields.
152
153        <b>(infix</b> <u>atom</u> <b>)</b>
154           Interpret   <u>atom</u>   as   an   infix   notmuch   query    (see    <a href='../notmuch-search-terms-7/'>not‐</a>
155           <a href='../notmuch-search-terms-7/'>much-search-terms</a>(7)). Not supported inside fields.
156
157        <b>(matching</b> <u>q</u>1 <u>q</u>2 ... <u>q</u>n <b>)</b> <b>(of</b> <u>q</u>1 <u>q</u>2 ... <u>q</u>n  <b>)</b>
158           Match  all  messages  have  the  same values of the current field as
159           those matching all of <u>q</u>1 ... <u>q</u>n.  Supported  in  most  term  [7]  or
160           phrase fields. Most commonly used in the <b>thread</b> field.
161
162        <b>(query</b> <u>atom</u> <b>)</b>
163           Expand  to  the saved query named by <u>atom</u>. See <a href='../notmuch-config-1/'>notmuch-config</a>(1) for
164           more. Note that the saved  query  must  be  in  infix  syntax  (<a href='../notmuch-search-terms-7/'>not‐</a>
165           <a href='../notmuch-search-terms-7/'>much-search-terms</a>(7)). Not supported inside fields.
166
167        <b>(regex</b> <u>atom</u> <b>)</b> <b>(rx</b> <u>atom</u> <b>)</b>
168           Interpret  <u>atom</u> as a POSIX.2 regular expression (see <b>regex</b>(7)). This
169           applies in term fields and a subset [5] of phrase fields (see <u>Fields</u>
170           <u>with</u> <u>supported</u> <u>modifiers</u>).
171
172        <b>(starts-with</b> <u>subword</u> <b>)</b>
173           Matches  any  term  starting  with  <u>subword</u>.  This applies in either
174           phrase or term <u>fields</u>,  or  outside  of  fields  [6].  Note  that  a
175           <b>starts-with</b>  query  cannot be part of a phrase. The atom <b>*</b> is a syn‐
176           onym for <b>(starts-with</b> <b>&quot;&quot;)</b>.
177 </pre>
178
179 <h2>EXAMPLES</h2>
180 <pre>
181        <b>Wizard</b>
182           Match all messages containing the word &quot;wizard&quot;, ignoring case.
183
184        <b>added</b>
185           Match all messages containing &quot;added&quot;,  but  also  those  containing
186           &quot;add&quot;, &quot;additional&quot;, &quot;Additional&quot;, &quot;adds&quot;, etc... via stemming.
187
188        <b>(and</b> <b>Bob</b> <b>Marley)</b>
189           Match  messages  containing words &quot;Bob&quot; and &quot;Marley&quot;, or their stems
190           The words need not be adjacent.
191
192        <b>(not</b> <b>Bob</b> <b>Marley)</b>
193           Match messages containing neither  &quot;Bob&quot;  nor  &quot;Marley&quot;,  nor  their
194           stems,
195
196        <b>&quot;quick</b> <b>fox&quot;</b> <b>quick-fox</b> <b>quick@fox</b>
197           Match the <u>phrase</u> &quot;quick&quot; followed by &quot;fox&quot; in phrase fields (or out‐
198           side a field). Match the literal string in a term field.
199
200        <b>(folder</b> <b>(of</b> <b>(id</b> <b>1234@invalid)))</b>
201           Match any message in the same folder as the one with  Message-Id  &quot;‐
202           <u>1234@invalid</u>&quot;
203
204        <b>(id</b> <b>1234@invalid</b> <b>blah@test)</b>
205           Matches Message-Id &quot;<u>1234@invalid</u>&quot; <u>or</u> Message-Id &quot;<u>blah@test</u>&quot;
206
207        <b>(and</b> <b>(infix</b> <b>&quot;date:2009-11-18..2009-11-18&quot;)</b> <b>(tag</b> <b>unread))</b>
208           Match messages in the given date range with tag unread.
209
210        <b>(starts-with</b> <b>prelim)</b>
211           Match any words starting with &quot;prelim&quot;.
212
213        <b>(subject</b> <b>quick</b> <b>&quot;brown</b> <b>fox&quot;)</b>
214           Match  messages  whose  subject contains &quot;quick&quot; (anywhere, stemmed)
215           and the phrase &quot;brown fox&quot;.
216
217        <b>(subject</b> <b>(starts-with</b> <b>prelim))</b>
218           Matches any word starting with &quot;prelim&quot;, inside a message subject.
219
220        <b>(subject</b> <b>(starts-wih</b> <b>quick)</b> <b>&quot;brown</b> <b>fox&quot;)</b>
221           Match messages whose subject contains &quot;quick brown  fox&quot;,  but  also
222           &quot;brown fox quicksand&quot;.
223
224        <b>(thread</b> <b>(of</b> <b>(id</b> <b>1234@invalid)))</b>
225           Match  any  message in the same thread as the one with Message-Id &quot;‐
226           <u>1234@invalid</u>&quot;
227
228        <b>(thread</b> <b>(matching</b> <b>(from</b> <b>bob@example.com)</b> <b>(to</b> <b>bob@example.com)))</b>
229           Match any (messages in)  a  thread  containing  a  message  from  &quot;‐
230           <u>bob@example.com</u>&quot;  and a (possibly distinct) message to &quot;bob at exam‐
231           ple.com&quot;)
232
233        <b>(to</b> <b>(or</b> <b>bob@example.com</b> <b>mallory@example.org))</b> <b>(or</b> <b>(to</b>  <b>bob@example.com)</b>
234        <b>(to</b> <b>mallory@example.org))</b>
235           Match   in   the   &quot;To&quot;   or  &quot;Cc&quot;  headers,  &quot;<u>bob@example.com</u>&quot;,  &quot;‐
236           <u>mallory@example.org</u>&quot;, and also &quot;<u>bob@example.com.au</u>&quot;  since  it  con‐
237           tains the adjacent triple &quot;bob&quot;, &quot;example&quot;, &quot;com&quot;.
238
239        <b>(not</b> <b>(to</b> <b>*))</b>
240           Match messages with an empty or invalid &apos;To&apos; and &apos;Cc&apos; field.
241
242        <b>(List</b> <b>*)</b>
243           Match  messages with a non-empty List-Id header, assuming configura‐
244           tion <b>index.header.List=List-Id</b>
245 </pre>
246
247 <h3> &nbsp; MACRO EXAMPLES</h3>
248 <pre>
249        A macro that takes two parameters and applies different fields to them.
250
251           $ notmuch config set squery.TagSubject &apos;(macro (tagname subj) (and (tag ,tagname) (subject ,subj)))&apos;
252           $ notmuch search --query=sexp &apos;(TagSubject inbox maildir)&apos;
253
254        Nested macros are allowed.
255
256           $ notmuch config set squery.Inner &apos;(macro (x) (subject ,x))&apos;
257           $ notmuch config set squery.Outer  &apos;(macro (x y) (and (tag ,x) (Inner ,y)))&apos;
258           $ notmuch search --query=sexp &apos;(Outer inbox maildir)&apos;
259
260        Parameters can be re-used to reduce boilerplate. Any  field,  including
261        user defined fields is permitted within a macro.
262
263           $ notmuch config set squery.About &apos;(macro (name) (or (subject ,name) (List ,name)))&apos;
264           $ notmuch search --query=sexp &apos;(About notmuch)&apos;
265 </pre>
266
267 <h2>NOTES</h2>
268 <pre>
269        [1]  Technically  macros  impliment  lazy evaluation and lexical scope.
270             There is one top level scope containing all macro definitions, but
271             all parameter definitions are local to a given macro.
272
273        [2]  a.k.a. prefixes
274
275        [3]  a.k.a. probabilistic prefixes
276
277        [4]  a.k.a. boolean prefixes
278
279        [5]  Due  to the implemention of phrase fields in Xapian, regex queries
280             could only match individual words.
281
282        [6]  Due the the way <b>body</b> is implemented in notmuch, this  modifier  is
283             not supported in the <b>body</b> field.
284
285        [7]  Due  to the way recursive <b>path</b> queries are implemented in notmuch,
286             this modifier is not supported in the <b>path</b> field.
287 </pre>
288
289 <h2>AUTHOR</h2>
290 <pre>
291        Carl Worth and many others
292 </pre>
293
294 <h2>COPYRIGHT</h2>
295 <pre>
296        2009-2022, Carl Worth and many others
297 </pre>
298
299 <h2>0.35</h2>