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

This commit is contained in:
Agate 2020-08-23 17:12:45 +02:00
parent 6bd77f24d6
commit 0c25370fff
3 changed files with 5 additions and 19 deletions

View File

@ -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):
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):
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"
) )
} }
) )

View File

@ -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",
[ [

View File

@ -0,0 +1 @@
Fixed broken search when using (, " or & chars (#1196)