我如何在 DRF 中搜索多项内容(全文、地理位置等)?

How can I do search for multiple things (full-text, geolocation etc) in DRF?

我在 Django-REST 中有一个模型,它具有名称、描述等,以及使用 GeoDjango 的地理定位。现在,我想要一个复杂的搜索,在名称和描述字段中进行全文搜索以及地理位置搜索(用户提供位置点和最大距离)。如果需要,我希望它们能够独立工作或一起工作。

我已经了解了如何进行全文搜索(此处:https://docs.djangoproject.com/en/2.2/ref/contrib/postgres/search/) and how to search based on distance(here: https://docs.djangoproject.com/en/2.2/ref/contrib/gis/db-api/)。

到目前为止我的代码(模型,无所谓,只要想到名称、描述和位置):

class SearchAuctions(APIView):
    permission_classes = [AllowAny]

    def get(self, request, format=None):
        """
        Return auctions after filtering.
        """
        items = AuctionItem.objects

        if 'textToSearch' in request.data.keys():
            textToSearch = request.data['textToSearch']
            items = AuctionItem.objects.annotate(
                search=SearchVector('name', 'description'),
            ).filter(search=textToSearch)

        itemSerializer = AuctionItemSerializer(items, many=True)

        return Response(itemSerializer.data)

不确定如何在过滤器之间创建链 link。我虽然提出了多个请求并找到了共同的元素,但我想那太慢了。

正如我在评论中提到的,您可以在不评估查询的情况下将过滤器应用于查询。 这允许您根据

等条件创建查询
class SearchAuctions(APIView):
    permission_classes = [AllowAny]

    def get(self, request, format=None):
        """
        Return auctions after filtering.
        """
        items = AuctionItem.objects

        if 'textToSearch' in request.data.keys():
            textToSearch = request.data['textToSearch']
            items = items.annotate(
                search=SearchVector('name', 'description'),
            ).filter(search=textToSearch)

        if 'locationToSearch' in request.data.keys():
            locationToSearch = request.data['locationToSearch']
            items = items.filter(location=locationToSearch)

        itemSerializer = AuctionItemSerializer(items, many=True)

        return Response(itemSerializer.data)