]> git.openstreetmap.org Git - osqa.git/blob - forum_modules/pgfulltext/handlers.py
Fixes OSQA 282, Error in related_questions.
[osqa.git] / forum_modules / pgfulltext / handlers.py
1 from django.db.models import Q
2 from forum.models.question import Question, QuestionManager
3 from forum.modules.decorators import decorate
4
5 @decorate(QuestionManager.search, needs_origin=False)
6 def question_search(self, keywords):
7     tsquery = " | ".join([k for k in keywords.split(' ') if k])
8     
9     return self.extra(
10                     tables = ['forum_rootnode_doc'],
11                     select={
12                         'ranking': """
13                                 rank_exact_matches(ts_rank_cd('{0.1, 0.2, 0.8, 1.0}'::float4[], "forum_rootnode_doc"."document", to_tsquery('english', %s), 32))
14                                 """,
15                     },
16                     where=["""
17                            "forum_rootnode_doc"."node_id" = "forum_node"."id" AND ("forum_rootnode_doc"."document" @@ to_tsquery('english', %s) OR
18                            "forum_node"."title" ILIKE '""" + keywords.replace("'",r"\'") + """%%')
19                            """],
20                     params=[tsquery],
21                     select_params=[tsquery],
22                     order_by=['-ranking']
23                 )
24