Skip to content
Snippets Groups Projects
Commit 0c25370f authored by Agate's avatar Agate :speech_balloon:
Browse files

Fix #1196: Fixed broken search when using (, " or & chars

parent 6bd77f24
No related branches found
No related tags found
No related merge requests found
...@@ -60,9 +60,11 @@ def get_query(query_string, search_fields): ...@@ -60,9 +60,11 @@ def get_query(query_string, search_fields):
def get_fts_query(query_string, fts_fields=["body_text"], model=None): def get_fts_query(query_string, fts_fields=["body_text"], model=None):
search_type = "plain"
if query_string.startswith('"') and query_string.endswith('"'): if query_string.startswith('"') and query_string.endswith('"'):
# we pass the query directly to the FTS engine # we pass the query directly to the FTS engine
query_string = query_string[1:-1] query_string = query_string[1:-1]
search_type = "raw"
else: else:
parts = query_string.replace(":", "").split(" ") parts = query_string.replace(":", "").split(" ")
parts = ["{}:*".format(p) for p in parts if p] 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): ...@@ -86,7 +88,7 @@ def get_fts_query(query_string, fts_fields=["body_text"], model=None):
subquery = related_model.objects.filter( subquery = related_model.objects.filter(
**{ **{
lookup: SearchQuery( lookup: SearchQuery(
query_string, search_type="raw", config="english_nostop" query_string, search_type=search_type, config="english_nostop"
) )
} }
).values_list("pk", flat=True) ).values_list("pk", flat=True)
...@@ -95,7 +97,7 @@ def get_fts_query(query_string, fts_fields=["body_text"], model=None): ...@@ -95,7 +97,7 @@ def get_fts_query(query_string, fts_fields=["body_text"], model=None):
new_query = Q( new_query = Q(
**{ **{
field: SearchQuery( field: SearchQuery(
query_string, search_type="raw", config="english_nostop" query_string, search_type=search_type, config="english_nostop"
) )
} }
) )
......
...@@ -1374,23 +1374,6 @@ def test_search_get_fts_advanced(logged_in_api_client, factories): ...@@ -1374,23 +1374,6 @@ def test_search_get_fts_advanced(logged_in_api_client, factories):
assert response.data == expected 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( @pytest.mark.parametrize(
"route, factory_name", "route, factory_name",
[ [
......
Fixed broken search when using (, " or & chars (#1196)
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment