Debian Codegraph @

docs/reference/query.rst

raw
1 .. _queries:
2
3 Queries
4 =======
5
6 Many of beets' :doc:`commands <cli>` are built around **query strings:**
7 searches that select tracks and albums from your library. This page explains the
8 query string syntax, which is meant to vaguely resemble the syntax used by Web
9 search engines.
10
11 .. _keywordquery:
12
13 Keyword
14 -------
15
16 This command:
17
18 ::
19
20 $ beet list love
21
22 will show all tracks matching the query string ``love``. By default any
23 unadorned word like this matches in a track's title, artist, album name, album
24 artist, genre and comments. See below on how to search other fields.
25
26 For example, this is what I might see when I run the command above:
27
28 ::
29
30 Against Me! - Reinventing Axl Rose - I Still Love You Julie
31 Air - Love 2 - Do the Joy
32 Bag Raiders - Turbo Love - Shooting Stars
33 Bat for Lashes - Two Suns - Good Love
34 ...
35
36 .. _combiningqueries:
37
38 Combining Keywords
39 ------------------
40
41 Multiple keywords are implicitly joined with a Boolean "and." That is, if a
42 query has two keywords, it only matches tracks that contain *both* keywords. For
43 example, this command:
44
45 ::
46
47 $ beet ls magnetic tomorrow
48
49 matches songs from the album "The House of Tomorrow" by The Magnetic Fields in
50 my library. It *doesn't* match other songs by the Magnetic Fields, nor does it
51 match "Tomorrowland" by Walter Meego---those songs only have *one* of the two
52 keywords I specified.
53
54 Keywords can also be joined with a Boolean "or" using a comma. For example, the
55 command:
56
57 ::
58
59 $ beet ls magnetic tomorrow , beatles yesterday
60
61 will match both "The House of Tomorrow" by the Magnetic Fields, as well as
62 "Yesterday" by The Beatles. Note that the comma has to be followed by a space
63 (e.g., ``foo,bar`` will be treated as a single keyword, *not* as an OR-query).
64
65 .. _fieldsquery:
66
67 Specific Fields
68 ---------------
69
70 Sometimes, a broad keyword match isn't enough. Beets supports a syntax that lets
71 you query a specific field---only the artist, only the track title, and so on.
72 Just say ``field:value``, where ``field`` is the name of the thing you're trying
73 to match (such as ``artist``, ``album``, or ``title``) and ``value`` is the
74 keyword you're searching for.
75
76 For example, while this query:
77
78 ::
79
80 $ beet list dream
81
82 matches a lot of songs in my library, this more-specific query:
83
84 ::
85
86 $ beet list artist:dream
87
88 only matches songs by the artist The-Dream. One query I especially appreciate is
89 one that matches albums by year:
90
91 ::
92
93 $ beet list -a year:2012
94
95 Recall that ``-a`` makes the ``list`` command show albums instead of individual
96 tracks, so this command shows me all the releases I have from this year.
97
98 For multi-valued tags (such as ``artists``, ``albumartists``, ``remixers``,
99 ``lyricists``, ``composers``, or ``arrangers``), a regular expression search
100 must be used to search for a single value within the multi-valued tag.
101
102 Note that you can filter albums by querying tracks fields and vice versa:
103
104 ::
105
106 $ beet list -a title:love
107
108 and vice versa:
109
110 ::
111
112 $ beet list art_path::love
113
114 Phrases
115 -------
116
117 You can query for strings with spaces in them by quoting or escaping them using
118 your shell's argument syntax. For example, this command:
119
120 ::
121
122 $ beet list the rebel
123
124 shows several tracks in my library, but these (equivalent) commands:
125
126 ::
127
128 $ beet list "the rebel"
129 $ beet list the\ rebel
130
131 only match the track "The Rebel" by Buck 65. Note that the quotes and
132 backslashes are not part of beets' syntax; I'm just using the escaping
133 functionality of my shell (bash or zsh, for instance) to pass ``the rebel`` as a
134 single argument instead of two.
135
136 .. _exact-match:
137
138 Exact Matches
139 -------------
140
141 While ordinary queries perform *substring* matches, beets can also match whole
142 strings by adding either ``=`` (case-sensitive) or ``=~`` (ignore case) after
143 the field name's colon and before the expression:
144
145 ::
146
147 $ beet list artist:air
148 $ beet list artist:=~air
149 $ beet list artist:=AIR
150
151 The first query is a simple substring one that returns tracks by Air, AIR, and
152 Air Supply. The second query returns tracks by Air and AIR, since both are a
153 case-insensitive match for the entire expression, but does not return anything
154 by Air Supply. The third query, which requires a case-sensitive exact match,
155 returns tracks by AIR only.
156
157 Exact matches may be performed on phrases as well:
158
159 ::
160
161 $ beet list artist:=~"dave matthews"
162 $ beet list artist:="Dave Matthews"
163
164 Both of these queries return tracks by Dave Matthews, but not by Dave Matthews
165 Band.
166
167 To search for exact matches across *all* fields, just prefix the expression with
168 a single ``=`` or ``=~``:
169
170 ::
171
172 $ beet list =~crash
173 $ beet list ="American Football"
174
175 .. _regex:
176
177 Regular Expressions
178 -------------------
179
180 In addition to simple substring and exact matches, beets also supports regular
181 expression matching for more advanced queries. To run a regex query, use an
182 additional ``:`` between the field name and the expression:
183
184 ::
185
186 $ beet list "artist::Ann(a|ie)"
187
188 That query finds songs by Anna Calvi and Annie but not Annuals. Similarly, this
189 query prints the path to any file in my library that's missing a track title:
190
191 ::
192
193 $ beet list -p title::^$
194
195 To search *all* fields using a regular expression, just prefix the expression
196 with a single ``:``, like so:
197
198 ::
199
200 $ beet list ":Ho[pm]eless"
201
202 Regular expressions are case-sensitive and build on `Python's built-in
203 implementation <https://docs.python.org/3/library/re.html>`__. See Python's
204 documentation for specifics on regex syntax.
205
206 Most command-line shells will try to interpret common characters in regular
207 expressions, such as ``()[]|``. To type those characters, you'll need to escape
208 them (e.g., with backslashes or quotation marks, depending on your shell).
209
210 .. _numericquery:
211
212 Numeric Range Queries
213 ---------------------
214
215 For numeric fields, such as year, bitrate, and track, you can query using one-or
216 two-sided intervals. That is, you can find music that falls within a *range* of
217 values. To use ranges, write a query that has two dots (``..``) at the
218 beginning, middle, or end of a string of numbers. Dots in the beginning let you
219 specify a maximum (e.g., ``..7``); dots at the end mean a minimum (``4..``);
220 dots in the middle mean a range (``4..7``).
221
222 For example, this command finds all your albums that were released in the '90s:
223
224 ::
225
226 $ beet list -a year:1990..1999
227
228 and this command finds MP3 files with bitrates of 128k or lower:
229
230 ::
231
232 $ beet list format:MP3 bitrate:..128000
233
234 The ``length`` field also lets you use a "M:SS" format. For example, this query
235 finds tracks that are less than four and a half minutes in length:
236
237 ::
238
239 $ beet list length:..4:30
240
241 .. _datequery:
242
243 Date and Date Range Queries
244 ---------------------------
245
246 Date-valued fields, such as *added* and *mtime*, have a special query syntax
247 that lets you specify years, months, and days as well as ranges between dates.
248
249 Dates are written separated by hyphens, like ``year-month-day``, but the month
250 and day are optional. If you leave out the day, for example, you will get
251 matches for the whole month.
252
253 Date *intervals*, like the numeric intervals described above, are separated by
254 two dots (``..``). You can specify a start, an end, or both. An interval written
255 back to front, such as ``2010..2008``, means the same as ``2008..2010``.
256
257 Here is an example that finds all the albums added in 2008:
258
259 ::
260
261 $ beet ls -a 'added:2008'
262
263 Find all items added in the years 2008, 2009 and 2010:
264
265 ::
266
267 $ beet ls 'added:2008..2010'
268
269 Find all items added before the year 2010:
270
271 ::
272
273 $ beet ls 'added:..2009'
274
275 Find all items added on or after 2008-12-01 but before 2009-10-12:
276
277 ::
278
279 $ beet ls 'added:2008-12..2009-10-11'
280
281 Find all items with a file modification time between 2008-12-01 and 2008-12-03:
282
283 ::
284
285 $ beet ls 'mtime:2008-12-01..2008-12-02'
286
287 You can also add an optional time value to date queries, specifying hours,
288 minutes, and seconds.
289
290 Times are separated from dates by a space, an uppercase 'T' or a lowercase 't',
291 for example: ``2008-12-01T23:59:59``. If you specify a time, then the date must
292 contain a year, month, and day. The minutes and seconds are optional.
293
294 Here is an example that finds all items added on 2008-12-01 at or after 22:00
295 but before 23:00:
296
297 ::
298
299 $ beet ls 'added:2008-12-01T22'
300
301 To find all items added on or after 2008-12-01 at 22:45:
302
303 ::
304
305 $ beet ls 'added:2008-12-01T22:45..'
306
307 To find all items added on 2008-12-01, at or after 22:45:20 but before 22:45:41:
308
309 ::
310
311 $ beet ls 'added:2008-12-01T22:45:20..2008-12-01T22:45:40'
312
313 Here are example of the three ways to separate dates from times. All of these
314 queries do the same thing:
315
316 ::
317
318 $ beet ls 'added:2008-12-01T22:45:20'
319 $ beet ls 'added:2008-12-01t22:45:20'
320 $ beet ls 'added:2008-12-01 22:45:20'
321
322 You can also use *relative* dates. For example, ``-3w`` means three weeks ago,
323 and ``+4d`` means four days in the future. A relative date has three parts:
324
325 - Either ``+`` or ``-``, to indicate the past or the future. The sign is
326 optional; if you leave this off, it defaults to the future.
327 - A number.
328 - A letter indicating the unit: ``d``, ``w``, ``m`` or ``y``, meaning days,
329 weeks, months or years. (A "month" is always 30 days and a "year" is always
330 365 days.)
331
332 Here's an example that finds all the albums added since last week:
333
334 ::
335
336 $ beet ls -a 'added:-1w..'
337
338 And here's an example that lists items added in a two-week period starting four
339 weeks ago:
340
341 ::
342
343 $ beet ls 'added:-6w..-4w'
344
345 .. _not_query:
346
347 Query Term Negation
348 -------------------
349
350 Query terms can also be negated, acting like a Boolean "not," by prefixing them
351 with ``-`` or ``^``. This has the effect of returning all the items that do
352 **not** match the query term. For example, this command:
353
354 ::
355
356 $ beet list ^love
357
358 matches all the songs in the library that do not have "love" in any of their
359 fields.
360
361 Negation can be combined with the rest of the query mechanisms, so you can
362 negate specific fields, regular expressions, etc. For example, this command:
363
364 ::
365
366 $ beet list -a artist:dylan ^year:1980..1989 "^album::the(y)?"
367
368 matches all the albums with an artist containing "dylan", but excluding those
369 released in the eighties and those that have "the" or "they" on the title.
370
371 The syntax supports both ``^`` and ``-`` as synonyms because the latter
372 indicates flags on the command line. To use a minus sign in a command-line
373 query, use a double dash ``--`` to separate the options from the query:
374
375 ::
376
377 $ beet list -a -- artist:dylan -year:1980..1990 "-album::the(y)?"
378
379 .. _pathquery:
380
381 Path Queries
382 ------------
383
384 Sometimes it's useful to find all the items in your library that are
385 (recursively) inside a certain directory. Use the ``path:`` field to do this:
386
387 ::
388
389 $ beet list path:/my/music/directory
390 $ beet list path:Artist/Album
391 $ beet list path:Artist/Album/track.mp3
392
393 In fact, beets automatically recognizes any query term containing a path
394 separator (``/`` on POSIX systems) as a path query if that path exists, so this
395 command is equivalent as long as ``/my/music/directory`` exist:
396
397 ::
398
399 $ beet list /my/music/directory
400
401 The ``path:`` field accepts either an absolute path under the configured music
402 directory or a path relative to the library root.
403
404 Note that this only matches items that are *already in your library*, so a path
405 query won't necessarily find *all* the audio files in a directory---just the
406 ones you've already added to your beets library.
407
408 Path queries are case sensitive if the queried path is on a case-sensitive
409 filesystem.
410
411 Cover Art Queries
412 -----------------
413
414 You can query items by whether they have embedded cover art using the
415 ``has_cover_art`` field. This is useful for finding tracks that are missing
416 artwork without needing external tools.
417
418 To find all tracks with embedded cover art:
419
420 ..
421 code-block: shell
422
423 $ beet list has_cover_art:true
424
425 To find all tracks without embedded cover art:
426
427 ..
428 code-block: shell
429
430 $ beet list has_cover_art:false
431
432 This query checks the actual file for embedded images, so no re-importing or
433 external tagging needed.
434
435 .. _query-sort:
436
437 Sort Order
438 ----------
439
440 Queries can specify a sort order. Use the name of the ``field`` you want to sort
441 on, followed by a ``+`` or ``-`` sign to indicate ascending or descending sort.
442 For example, this command:
443
444 ::
445
446 $ beet list -a year+
447
448 will list all albums in chronological order. You can also specify several sort
449 orders, which will be used in the same order as they appear in your query:
450
451 ::
452
453 $ beet list -a genre+ year+
454
455 This command will sort all albums by genre and, in each genre, in chronological
456 order.
457
458 The ``artist`` and ``albumartist`` keys are special: they attempt to use their
459 corresponding ``artist_sort`` and ``albumartist_sort`` fields for sorting
460 transparently (but fall back to the ordinary fields when those are empty).
461
462 Lexicographic sorts are case insensitive by default, resulting in the following
463 sort order: ``Bar foo Qux``. This behavior can be changed with the
464 :ref:`sort_case_insensitive` configuration option. Case sensitive sort will
465 result in lower-case values being placed after upper-case values, e.g., ``Bar
466 Qux foo``.
467
468 Note that when sorting by fields that are not present on all items (such as
469 flexible fields, or those defined by plugins) in *ascending* order, the items
470 that lack that particular field will be listed at the *beginning* of the list.
471
472 You can set the default sorting behavior with the :ref:`sort_item` and
473 :ref:`sort_album` configuration options.