代码之家  ›  专栏  ›  技术社区  ›  Santhosh

Django:文本搜索:Haystack vs postgres全文搜索

  •  3
  • Santhosh  · 技术社区  · 7 年前

    我正在使用Django 2.0

    我有标题和描述的帖子。这是我第一次尝试实现搜索功能。

    我在搜索以下选项后发现:

    Haystack和postgres全文搜索( https://docs.djangoproject.com/en/2.0/ref/contrib/postgres/search/ )

    这是建议使用的。

    2 回复  |  直到 7 年前
        1
  •  2
  •   Paolo Melchiorre    7 年前

    我可以建议在Django中使用PostgreSQL全文搜索。

    官方文件比较好。

    如果你想了解我的建议的更多信息和动机,你可以阅读我写的一篇关于这个主题的文章: Full-Text Search in Django with PostgreSQL

        2
  •  1
  •   jeanmw    7 年前

    仅供参考,SearchVector/SearchQuery方法实际上并不能涵盖所有情况,例如部分单词(请参见 https://www.fusionbox.com/blog/detail/partial-word-search-with-postgres-full-text-search-in-django/632/ 以供参考)。根据您的限制,您可以轻松实现自己的。 例如,在viewsets的get\u queryset方法中:

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