diff options
Diffstat (limited to 'askbot/search/postgresql/__init__.py')
-rw-r--r-- | askbot/search/postgresql/__init__.py | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/askbot/search/postgresql/__init__.py b/askbot/search/postgresql/__init__.py index a802a5eb..5b893129 100644 --- a/askbot/search/postgresql/__init__.py +++ b/askbot/search/postgresql/__init__.py @@ -19,3 +19,32 @@ def setup_full_text_search(script_path): cursor.execute(fts_init_query) finally: cursor.close() + +def run_full_text_search(query_set, query_text): + """runs full text search against the query set and + the search text. All words in the query text are + added to the search with the & operator - i.e. + the more terms in search, the narrower it is. + + It is also assumed that we ar searching in the same + table as the query set was built against, also + it is assumed that the table has text search vector + stored in the column called `text_search_vector`. + """ + table_name = query_set.model._meta.db_table + + rank_clause = 'ts_rank(' + table_name + \ + '.text_search_vector, plainto_tsquery(%s))' + + where_clause = table_name + '.text_search_vector @@ plainto_tsquery(%s)' + + search_query = '&'.join(query_text.split())#apply "AND" operator + extra_params = (search_query,) + extra_kwargs = { + 'select': {'relevance': rank_clause}, + 'where': [where_clause,], + 'params': extra_params, + 'select_params': extra_params, + } + + return query_set.extra(**extra_kwargs) |