Django-filter: 任何人都可以举一个与 Django/Postgres 原生全文搜索引擎一起使用的例子吗?

创建于 2019-02-16  ·  4评论  ·  资料来源: carltongibson/django-filter

这里完全是菜鸟....

https://docs.djangoproject.com/en/2.1/ref/contrib/postgres/search/#postgresql -fts-search-configuration

Documentation

最有用的评论

嗨@gotexis。 django-filter 基本上做了两件事:

  • 验证输入
  • 从这些输入构建查询

如果没有Filter类来构造您要查找的查询,您有两个选择:

如果您可以概括查询,编写自定义Filter将更可重用,但是Filter.method对于一次性实例很有用。 你会写这样的东西:

from django.contrib.postgres.search import SearchQuery, SearchVector
from djanog_filters import FilterSet, CharFilter

class F(FilterSet):
    # The model `field_name` is the field you want to search on and is passed to your `method`.
    # The `method` argument is the name of the method to call to perform filtering.
    search = CharFilter(field_name='body_text', method='search_fulltext')

    def search_fulltext(self, queryset, field_name, value):
        return queryset \
            .annotate(search=SearchVector(field_name)) \
            .filter(search=SearchQuery(value))

所有4条评论

嗨@gotexis。 django-filter 基本上做了两件事:

  • 验证输入
  • 从这些输入构建查询

如果没有Filter类来构造您要查找的查询,您有两个选择:

如果您可以概括查询,编写自定义Filter将更可重用,但是Filter.method对于一次性实例很有用。 你会写这样的东西:

from django.contrib.postgres.search import SearchQuery, SearchVector
from djanog_filters import FilterSet, CharFilter

class F(FilterSet):
    # The model `field_name` is the field you want to search on and is passed to your `method`.
    # The `method` argument is the name of the method to call to perform filtering.
    search = CharFilter(field_name='body_text', method='search_fulltext')

    def search_fulltext(self, queryset, field_name, value):
        return queryset \
            .annotate(search=SearchVector(field_name)) \
            .filter(search=SearchQuery(value))

哇谢谢你的帮助@rpkilby ,请给我更多的时间来消化这段代码......

现在我正在用与 django-filters 集成的 graphene-django 敲我的头。

我希望(理想情况下)用 GraphQL 实现这个

请注意,graphene-django 如何包装 django-filter 可能存在一些问题。 例如,见#927

@rpkilby是的,先生
看起来确实是有给予比节点不受欢迎的Python + GraphQL如何成为石墨烯的问题,再加上笔者放弃了这个项目,你不同意:)

最近的一个,也与 django-filter 相关,是他们的 DjangoFilterConnectionField 不适用于预取,导致大量查询。 一直调试失败,不知道大家有没有经验分享:)

此页面是否有帮助?
0 / 5 - 0 等级