Django 2 + PostgreSQL FullText搜索不匹配它应该 [英] Django 2 + PostgreSQL FullText search not matching what it should
问题描述
我需要在包含多列的表格上执行全文搜索并对结果进行排序通过排名,使用Django 2和PostgreSQL 10.1。
到目前为止,这是我的代码:
from django.contrib.postgres.search import SearchQuery,SearchRank,SearchVector
#其他无关的东西在这里
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, (搜索字符串,配置=意大利)
等级= SearchRank(向量,查询,权重= [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)
奇怪的事实是,有些search_string没有返回任何结果,而如果我执行一个简单的查询,比如
SELECT * FROM my_entry
WHERE very_important_field LIKE'%search_string%'
OR标签LIKE'%search_string%'
OR ...
如果我将搜索范围限制在重要字段,我会得到大量结果。
我已经尝试使用过滤器(rank_gte = 0.4)并将其删除。目前我还没有使用GIN或GIST索引,因为我只是在试验。
p>关于我错过了什么的任何线索?
提前致谢
我想我发现了这个问题。
SearchQuery()上的关键字参数
config ='italian'
似乎不起作用。为了解决这个问题,我在我的db上发布了:
ALTER DATABASE my_db SET default_text_search_config ='pg_catalog.italian';
现在所有东西都可以正常工作。
<我希望这可以帮助别人。
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
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.
这篇关于Django 2 + PostgreSQL FullText搜索不匹配它应该的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!