我有以下执行搜索的方法
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单独查询并合并结果,但我想知道是否可以使用单个查询来完成。