问题
I am experiencing a problem I hope someone can enlighten me about.
I need to perform a full-text search on a table with multiple columns and sorting the results by rank, using Django 2 and PostgreSQL 10.1. So far this is my code:
from django.contrib.postgres.search import SearchQuery, SearchRank, SearchVector
# other unrelated stuff here
vector = SearchVector("very_important_field", weight="A") + \
SearchVector("tags", weight="A") + \
SearchVector("also_important_field", weight="B") + \
SearchVector("not_so_important_field", weight="C") + \
SearchVector("not_important_field", weight="D")
query = SearchQuery(search_string, config="italian")
rank = SearchRank(vector, query, weights=[0.4, 0.6, 0.8, 1.0]). # D, C, B, A
full_text_search_qs = MyEntry.objects.annotate(rank=rank).filter(rank__gte=0.4).order_by("-rank")
The strange fact is that some search_string return nothing as a result, while if I perform a simple query such as
SELECT * FROM my_entry
WHERE very_important_field LIKE '%search_string%'
OR tags LIKE '%search_string%'
OR ...
I get tons of results, also if I limit the search just to the important fields. I already tried to play with filter(rank_gte=0.4) and also removing it. Still same result.
As of now I am not using GIN o GIST indexing, since I am just experimenting.
Any clues about what am I missing? Thanks in advance
回答1:
I think I found the problem.
The keyword argument on SearchQuery()
config='italian'
didn't seem to work. To solve this problem I issued on my db:
ALTER DATABASE my_db SET default_text_search_config = 'pg_catalog.italian';
And everything now works more than fine.
I hope this can help someone else, too.
来源:https://stackoverflow.com/questions/48731537/django-2-postgresql-fulltext-search-not-matching-what-it-should