diff options
author | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-05-13 18:51:26 -0400 |
---|---|---|
committer | Evgeny Fadeev <evgeny.fadeev@gmail.com> | 2012-05-13 18:51:26 -0400 |
commit | 521a574aa6c029649e18f4592f04990ab69c896c (patch) | |
tree | 81ab1f2d956223bb7f150c580b70a8059d3c6e77 /askbot/search | |
parent | 8e1ed3eb4a6347688ea68f40885dbbe29e5ffcb8 (diff) | |
download | askbot-521a574aa6c029649e18f4592f04990ab69c896c.tar.gz askbot-521a574aa6c029649e18f4592f04990ab69c896c.tar.bz2 askbot-521a574aa6c029649e18f4592f04990ab69c896c.zip |
search for users now works for the user profile
Diffstat (limited to 'askbot/search')
-rw-r--r-- | askbot/search/mysql.py | 54 | ||||
-rw-r--r-- | askbot/search/postgresql/__init__.py | 29 |
2 files changed, 83 insertions, 0 deletions
diff --git a/askbot/search/mysql.py b/askbot/search/mysql.py new file mode 100644 index 00000000..df86070d --- /dev/null +++ b/askbot/search/mysql.py @@ -0,0 +1,54 @@ +from django.db import connection + +SUPPORTS_FTS = None +HINT_TABLE = None +NO_FTS_WARNING = """ +!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +!! !! +!! WARNING: Your database engine does not support !! +!! full text search. Please switch to PostgresQL !! +!! or select MyISAM engine for MySQL !! +!! !! +!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! +""" + +def supports_full_text_search(hint_table = None): + """True if the database engine is MyISAM + hint_table - is the table that we look into to determine + whether database supports FTS or not. + """ + global SUPPORTS_FTS + global HINT_TABLE + if SUPPORTS_FTS is None: + cursor = connection.cursor() + if hint_table: + table_name = hint_table + HINT_TABLE = hint_table + else: + from askbot.models import Post + table_name = Post._meta.db_table + cursor.execute("SHOW CREATE TABLE %s" % table_name) + data = cursor.fetchone() + if 'ENGINE=MyISAM' in data[1]: + SUPPORTS_FTS = True + else: + SUPPORTS_FTS = False + return SUPPORTS_FTS + + question_index_sql = get_create_full_text_index_sql( + index_name, + table_namee, + ('title','text','tagnames',) + ) +def get_create_full_text_index_sql(index_name, table_name, column_list): + cursor = connection.cursor() + column_sql = '(%s)' % ','.join(column_list) + sql = 'CREATE FULLTEXT INDEX %s on %s %s' % (index_name, table_name, column_sql) + cursor.execute(question_index_sql) + return sql + else: + print NO_FTS_WARNING + +def get_drop_index_sql(index_name, table_name): + return 'ALTER TABLE %s DROP INDEX %s' % (table_name, index_name) + 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) |