Django - 搜索与所有对象匹配 - 即使它们实际上不匹配

时间:2018-02-05 17:24:52

标签: django postgresql full-text-search

这是必须搜索的模型:

class BlockQuote(models.Model):
    debate = models.ForeignKey(Debate, related_name='quotes')
    speaker = models.ForeignKey(Speaker, related_name='quotes')
    text = models.TextField()

我的笔记本电脑上的数据库上有大约一千个实例(生产服务器上大约有50000个)

我正在创建一个'manage.py'函数,它将搜索数据库并返回其textfield包含关键字的所有'BlockQuote'对象。

我正在使用Django的(1.11)Postgres搜索选项来使用'rank'属性,这听起来像是派上用场的东西。 I used the official Django fulltext-search documentation for the code below

然而,当我运行此代码时,它与所有对象匹配,无论BlockQuote.text实际上是否包含查询字段。

def handle(self, *args, **options):
    vector = SearchVector('text')
    query = options['query'][0]
    Search_Instance = Search_Instance.objects.create(query=query)
    set = BlockQuote.objects.annotate(rank=SearchRank(vector, query)).order_by('-rank')

    for result in set:
        match = QueryMatch.objects.create(quote=result, query=Search_Instance)
        match.save()

有没有人知道我做错了什么?

1 个答案:

答案 0 :(得分:2)

我没有看到你真正过滤过。

BlockQuote.objects.annotate(...).filter(rank__gte=0.5)
相关问题