使用SearchVector在PSQL和Django中按主键搜索

时间:2018-06-19 08:44:55

标签: django psql

我有以下执行搜索的方法

def _search_parts(self, query: str, limit: int) -> List[Part]:

    search_query = SearchQuery(query, config='simple')
    if len(query.split(' ')) > 1:
        for q in query.split(' '):
            search_query |= SearchQuery('+'+q, config='simple')

    search_vector = SearchVector('name', weight='B') + \
                    SearchVector('part_with_finish__manufacturer_product_code', weight='A') + \
                    SearchVector('part_with_finish__product_code', weight='A') + \
                    SearchVector('id', weight='A') + \  # <---- THIS LINE
                    SearchVector('short_name', weight='A')

    search_rank = SearchRank(search_vector, search_query)

    parts = Part.objects.annotate(rank=search_rank).filter(rank__gt=0.0).order_by('-rank')[:limit]

SearchVector会为字段id

显示以下错误
DataError at /api/v1/search
invalid input syntax for integer: ""
LINE 1: ...| setweight(to_tsvector(COALESCE("app_part"."id", '')), 'A')...
                                                             ^

如何让它发挥作用?

我可以通过PK单独查询并合并结果,但我想知道是否可以使用单个查询来完成。

0 个答案:

没有答案