From f71e503f6cc006cf702fd30539b22f899342141d Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Wed, 17 Aug 2022 16:05:33 -0700 Subject: [PATCH 1/3] Change the prefix for exact match queries PR #4251 added exact match queries, which are great, but it was subsequently pointed out that the `~` query prefix was already in use: https://github.com/beetbox/beets/pull/4251#issuecomment-1069455483 So this changes the prefix from `~` to `=~`. A little longer, but hopefully it makes the relationship to the similarly-new `=` prefix obvious. --- beets/library.py | 2 +- docs/changelog.rst | 4 +++- docs/reference/query.rst | 14 ++++++++------ 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/beets/library.py b/beets/library.py index 3b8a856854..c754eaa019 100644 --- a/beets/library.py +++ b/beets/library.py @@ -1387,7 +1387,7 @@ def parse_query_parts(parts, model_cls): # Get query types and their prefix characters. prefixes = { ':': dbcore.query.RegexpQuery, - '~': dbcore.query.StringQuery, + '=~': dbcore.query.StringQuery, '=': dbcore.query.MatchQuery, } prefixes.update(plugins.queries()) diff --git a/docs/changelog.rst b/docs/changelog.rst index d21a55d371..9363ee2503 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -25,7 +25,9 @@ New features: * :doc:`/plugins/kodiupdate`: Now supports multiple kodi instances :bug:`4101` * Add the item fields ``bitrate_mode``, ``encoder_info`` and ``encoder_settings``. -* Add query prefixes ``=`` and ``~``. +* Add :ref:`exact match ` queries, using the prefixes ``=`` and + ``=~``. + :bug:`4251` * :doc:`/plugins/discogs`: Permit appending style to genre * :doc:`/plugins/convert`: Add a new `auto_keep` option that automatically converts files but keeps the *originals* in the library. diff --git a/docs/reference/query.rst b/docs/reference/query.rst index 75fac3015f..955bdf57da 100644 --- a/docs/reference/query.rst +++ b/docs/reference/query.rst @@ -93,15 +93,17 @@ backslashes are not part of beets' syntax; I'm just using the escaping functionality of my shell (bash or zsh, for instance) to pass ``the rebel`` as a single argument instead of two. +.. _exact-match: + Exact Matches ------------- While ordinary queries perform *substring* matches, beets can also match whole -strings by adding either ``=`` (case-sensitive) or ``~`` (ignore case) after the -field name's colon and before the expression:: +strings by adding either ``=`` (case-sensitive) or ``=~`` (ignore case) after +the field name's colon and before the expression:: $ beet list artist:air - $ beet list artist:~air + $ beet list artist:=~air $ beet list artist:=AIR The first query is a simple substring one that returns tracks by Air, AIR, and @@ -112,16 +114,16 @@ returns tracks by AIR only. Exact matches may be performed on phrases as well:: - $ beet list artist:~"dave matthews" + $ beet list artist:=~"dave matthews" $ beet list artist:="Dave Matthews" Both of these queries return tracks by Dave Matthews, but not by Dave Matthews Band. To search for exact matches across *all* fields, just prefix the expression with -a single ``=`` or ``~``:: +a single ``=`` or ``=~``:: - $ beet list ~crash + $ beet list =~crash $ beet list ="American Football" .. _regex: From 495c8accc07041718914b085884fd11e76758f65 Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Wed, 17 Aug 2022 16:11:16 -0700 Subject: [PATCH 2/3] Update exact query prefix tests --- test/test_query.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/test_query.py b/test/test_query.py index 8a9043fa33..3a8a7844df 100644 --- a/test/test_query.py +++ b/test/test_query.py @@ -150,7 +150,7 @@ def test_get_one_keyed_exact(self): self.assert_items_matched(results, ['beets 4 eva']) def test_get_one_keyed_exact_nocase(self): - q = 'genre:~"hard rock"' + q = 'genre:=~"hard rock"' results = self.lib.items(q) self.assert_items_matched(results, ['beets 4 eva']) @@ -220,7 +220,7 @@ def test_key_case_insensitive(self): self.assert_items_matched(results, ['beets 4 eva']) def test_keyed_matches_exact_nocase(self): - q = 'genre:~rock' + q = 'genre:=~rock' results = self.lib.items(q) self.assert_items_matched(results, [ 'foo bar', From 32ce44f589e231b89323a4ff5e0847026e830d5f Mon Sep 17 00:00:00 2001 From: Adrian Sampson Date: Wed, 17 Aug 2022 16:25:17 -0700 Subject: [PATCH 3/3] One more test fix --- test/test_query.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/test_query.py b/test/test_query.py index 3a8a7844df..3c6d6f70a1 100644 --- a/test/test_query.py +++ b/test/test_query.py @@ -170,7 +170,7 @@ def test_get_one_unkeyed_exact(self): self.assert_items_matched(results, ['foo bar']) def test_get_one_unkeyed_exact_nocase(self): - q = '~"hard rock"' + q = '=~"hard rock"' results = self.lib.items(q) self.assert_items_matched(results, ['beets 4 eva'])