From 0c25370fffab7f2adc2204aa5b903ad0ad5f1a06 Mon Sep 17 00:00:00 2001 From: Agate Date: Sun, 23 Aug 2020 17:12:45 +0200 Subject: [PATCH] Fix #1196: Fixed broken search when using (, " or & chars --- api/funkwhale_api/common/search.py | 6 ++++-- api/tests/music/test_views.py | 17 ----------------- changes/changelog.d/1196.bugfix | 1 + 3 files changed, 5 insertions(+), 19 deletions(-) create mode 100644 changes/changelog.d/1196.bugfix diff --git a/api/funkwhale_api/common/search.py b/api/funkwhale_api/common/search.py index deb2607f9..06f2d02a2 100644 --- a/api/funkwhale_api/common/search.py +++ b/api/funkwhale_api/common/search.py @@ -60,9 +60,11 @@ def get_query(query_string, search_fields): def get_fts_query(query_string, fts_fields=["body_text"], model=None): + search_type = "plain" if query_string.startswith('"') and query_string.endswith('"'): # we pass the query directly to the FTS engine query_string = query_string[1:-1] + search_type = "raw" else: parts = query_string.replace(":", "").split(" ") parts = ["{}:*".format(p) for p in parts if p] @@ -86,7 +88,7 @@ def get_fts_query(query_string, fts_fields=["body_text"], model=None): subquery = related_model.objects.filter( **{ lookup: SearchQuery( - query_string, search_type="raw", config="english_nostop" + query_string, search_type=search_type, config="english_nostop" ) } ).values_list("pk", flat=True) @@ -95,7 +97,7 @@ def get_fts_query(query_string, fts_fields=["body_text"], model=None): new_query = Q( **{ field: SearchQuery( - query_string, search_type="raw", config="english_nostop" + query_string, search_type=search_type, config="english_nostop" ) } ) diff --git a/api/tests/music/test_views.py b/api/tests/music/test_views.py index 3ab5812f2..673bc13ab 100644 --- a/api/tests/music/test_views.py +++ b/api/tests/music/test_views.py @@ -1374,23 +1374,6 @@ def test_search_get_fts_advanced(logged_in_api_client, factories): assert response.data == expected -def test_search_get_fts_stop_words(logged_in_api_client, factories): - artist = factories["music.Artist"](name="she") - factories["music.Artist"](name="something else") - - url = reverse("api:v1:search") - expected = { - "artists": [serializers.ArtistWithAlbumsSerializer(artist).data], - "albums": [], - "tracks": [], - "tags": [], - } - response = logged_in_api_client.get(url, {"q": "sh"}) - - assert response.status_code == 200 - assert response.data == expected - - @pytest.mark.parametrize( "route, factory_name", [ diff --git a/changes/changelog.d/1196.bugfix b/changes/changelog.d/1196.bugfix new file mode 100644 index 000000000..3cd0e29ed --- /dev/null +++ b/changes/changelog.d/1196.bugfix @@ -0,0 +1 @@ +Fixed broken search when using (, " or & chars (#1196) \ No newline at end of file