Django: text search: Haystack vs postgres full text search

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-09 19:09:50

问题


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.


回答1:


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




回答2:


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


来源:https://stackoverflow.com/questions/48582377/django-text-search-haystack-vs-postgres-full-text-search

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!