python - 使用两个模型进行全文搜索

标签 python django postgresql full-text-search django-queryset

我有两个模型 ItemOwner:

class Item(models.Model):
    name = models.CharField(max_length=255)
    owner = models.ForeignKey(
        Owner, related_name='owner_items',
        on_delete=models.CASCADE,
    )
    is_featured = models.BooleanField(
        choices=CHOICES, default=BOOL_NO
    )
    # ...

我在项目名称和描述字段中进行全文搜索(基于 Django 文档)。 PostgreSQL 版本为 10。

search_vectors = (
    SearchVector('name', weight='A', config='english') +
    SearchVector('description', weight='B', config='english')
)
terms = [SearchQuery(term) for term in keyword.split()]
search_query = functools.reduce(operator.or_, terms)
search_rank = SearchRank(
    search_vectors, search_query, weights=[0.2, 0.4, 0.6, 1]
)
qs = Item.objects.all().annotate(
    rank=search_rank
).filter(rank__gte=0.2).order_by('-rank')

我还想介绍等式中的 Ownername 字段,如果 is_featured

在执行此搜索之前,我有 Owner 实例模型。

最佳答案

您可以在 search_vector 中添加来自类 Owner 的字段 name,可能具有不同的 weight 和相同的 config (这只是一个假设,因为您没有指定 Owner 模型定义或数据)

使用 is_featured 来提升您的 rank 的方法可以是注释 1 (您可以使用其他值) 如果 True 然后将其添加到 SearchRank 结果中。

from django.db import models
from django.db.models import Case, Value, When
from django.contrib.postgres.search import (
    SearchQuery, SearchRank, SearchVector,
)

search_vectors = (
    SearchVector('name', weight='A', config='english') +
    SearchVector('description', weight='B', config='english') +
    SearchVector('owner__name', weight='C', config='english')
)
terms = [SearchQuery(term) for term in keyword.split()]
search_query = functools.reduce(operator.or_, terms)
search_rank = SearchRank(
    search_vectors, search_query, weights=[0.2, 0.4, 0.6, 1]
)
qs = Item.objects.all().annotate(
    featured_boost=Case(
        When(is_featured=True, then=Value(1)),
        default=Value(0),
        output_field=models.IntegerField(),
    )
).annotate(
    rank=search_rank + featured_boost
).filter(rank__gte=0.2).order_by('-rank')

关于python - 使用两个模型进行全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55351016/

相关文章:

python - 从备份恢复表后出现完整性错误,PostgreSQL + Django

postgresql - 无法使用Kotlin和Spring数据保留数据

python - Python 单击命令的功能标志

python - 在pygal中合并并比较两组堆叠数据

python - 如何解决 psycopg2.ProgrammingError : column reference "account_analytic_id" is ambiguous in odoo

django - 如何在没有迭代的情况下为查询集从多对多中删除对象?

python 或运算符(operator)模块

Django:运行数据迁移时出现 "column <whatever> does not exist"

sql - 合并两个表,按比例交错两个表的选择查询结果

postgresql - 确定 PostgreSQL 中有问题的查询