[Django]-Django: text search: Haystack vs postgres full text search

2👍

✅

I can suggest to use the PostgreSQL Full-Text Search with Django.

The official documentations is quite good.

If you want more information and motivation about my suggestion you can read an article I wrote on this subject: Full-Text Search in Django with PostgreSQL

1👍

FYI the SearchVector/SearchQuery approach actually does not catch all cases, for example partial words (see https://www.fusionbox.com/blog/detail/partial-word-search-with-postgres-full-text-search-in-django/632/ for reference). You can implement your own without much trouble, depending on your constraints.
example, within a viewsets’ get_queryset method:

    ...other params...

            search_terms = self.request.GET.get('q')
            if search_terms:
                # remove possible other delimiters and other chars
                # that could interfere
                cleaned_terms = re.sub(r'[!\'()|&;,]', ' ', search_terms).strip()
                if cleaned_terms:
                    # Check against all the params we want
                    # apply to previous terms' filtered results
                    q = reduce(
                        lambda p, n: p & n,
                        map(
                            lambda word:
                                Q(your_property__icontains=word) | Q(
                                    second_property__icontains=word) | Q(
                                    third_property__icontains=word)
                            cleaned_terms.split()
                        )
                    )
                    qs = YourModel.objects.filter(q)
           return qs

Leave a comment