Santhosh
Santhosh

Reputation: 11774

Django: text search: Haystack vs postgres full text search

I am using Django 2.0

I have posts with title and description. For the first time i am trying to implement search functionality.

I found after searching the following options:

Haystack and postgres full text search (https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/search/)

Which is the suggested one to work with.

Upvotes: 3

Views: 1141

Answers (2)

jeanmw
jeanmw

Reputation: 466

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

Upvotes: 1

Paolo Melchiorre
Paolo Melchiorre

Reputation: 6112

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

Upvotes: 3

Related Questions