Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Could anyone give an example to use with Django/Postgres native Full Text Search engine? #1039

Open
gotexis opened this issue Feb 16, 2019 · 7 comments

Comments

@gotexis
Copy link

gotexis commented Feb 16, 2019

Totally noob here....

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

@rpkilby
Copy link
Collaborator

rpkilby commented Feb 18, 2019

Hi @gotexis. django-filter basically does two things:

  • Validate inputs
  • Construct queries from those inputs

When there isn't a Filter class that constructs the query you're looking for, you have two options:

If you can generalize the query, writing a custom Filter would be more reusable, however Filter.method is useful for one-off instances. You would write something like:

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))

@gotexis
Copy link
Author

gotexis commented Mar 11, 2019

Wow thanks for your help @rpkilby, please give me some more days to digest this code...

Right now I am banging my heads with graphene-django which integrates with django-filters.

I hope to (ideally) implement this with GraphQL

@rpkilby
Copy link
Collaborator

rpkilby commented Mar 11, 2019

Note that there may be some issues with how graphene-django wraps django-filter. e.g.,see #927

@gotexis
Copy link
Author

gotexis commented Apr 12, 2019

@rpkilby Yes sir
It looks like there is indeed heaps of issues with Graphene given how unpopular Python+GraphQL has become as compared to Node, plus the author is giving up on the project, wouldn't you agree :)

One of the most recent one, also related to django-filter, is their DjangoFilterConnectionField doesn't work with prefetching, resulting in tons of queries. I have been failing to debug it, I wonder if you have any experience to share :)

@Pradhvan
Copy link

Just stumbled on this issue when searching how to use django-filter with Postgres Full Text search. Since the issue is already tagged Documentation should we add a how to guide on the approach mentioned by @rpkilby in the sub-section of the doc (I can take that part up)

@carltongibson
Copy link
Owner

Something in the Tips section would be great.

https://django-filter.readthedocs.io/en/stable/guide/tips.html

@Pradhvan
Copy link

Cool, will create a PR soon for this. 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants