diff options
author | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2012-06-20 15:07:53 -0600 |
---|---|---|
committer | Adolfo Fitoria <adolfo.fitoria@gmail.com> | 2012-06-20 15:07:53 -0600 |
commit | d237df28f59bf7753e1ab370a7e5bd4628add81d (patch) | |
tree | eb1df3ad34ab6889d441bdeb89d8f1ed45cffeaa | |
parent | ea4b1eb15de0d382a99ebcfe70e3c11ac83d49e4 (diff) | |
download | askbot-d237df28f59bf7753e1ab370a7e5bd4628add81d.tar.gz askbot-d237df28f59bf7753e1ab370a7e5bd4628add81d.tar.bz2 askbot-d237df28f59bf7753e1ab370a7e5bd4628add81d.zip |
added haystack user index
-rw-r--r-- | askbot/models/__init__.py | 33 | ||||
-rw-r--r-- | askbot/search/haystack/__init__.py | 26 |
2 files changed, 38 insertions, 21 deletions
diff --git a/askbot/models/__init__.py b/askbot/models/__init__.py index 621ae6df..03ab218b 100644 --- a/askbot/models/__init__.py +++ b/askbot/models/__init__.py @@ -59,21 +59,26 @@ def get_users_by_text_query(search_query): """Runs text search in user names and profile. For postgres, search also runs against user group names. """ - import askbot - if 'postgresql_psycopg2' in askbot.get_database_engine_name(): - from askbot.search import postgresql - return postgresql.run_full_text_search(User.objects.all(), search_query) + if django_settings.ENABLE_HAYSTACK_SEARCH: + from askbot.search.haystack import AskbotSearchQuerySet + qs = AskbotSearchQuerySet().filter(content=search_query).models(User).get_django_queryset(User) + return qs else: - return User.objects.filter( - models.Q(username__icontains=search_query) | - models.Q(about__icontains=search_query) - ) - #if askbot.get_database_engine_name().endswith('mysql') \ - # and mysql.supports_full_text_search(): - # return User.objects.filter( - # models.Q(username__search = search_query) | - # models.Q(about__search = search_query) - # ) + import askbot + if 'postgresql_psycopg2' in askbot.get_database_engine_name(): + from askbot.search import postgresql + return postgresql.run_full_text_search(User.objects.all(), search_query) + else: + return User.objects.filter( + models.Q(username__icontains=search_query) | + models.Q(about__icontains=search_query) + ) + #if askbot.get_database_engine_name().endswith('mysql') \ + # and mysql.supports_full_text_search(): + # return User.objects.filter( + # models.Q(username__search = search_query) | + # models.Q(about__search = search_query) + # ) User.add_to_class( 'status', diff --git a/askbot/search/haystack/__init__.py b/askbot/search/haystack/__init__.py index 62965645..ad48fad5 100644 --- a/askbot/search/haystack/__init__.py +++ b/askbot/search/haystack/__init__.py @@ -4,7 +4,7 @@ try: except ImportError: pass -from askbot.models import Post, Thread, Tag, User +from askbot.models import Post, Thread, User class ThreadIndex(indexes.SearchIndex): text = indexes.CharField(document=True, use_template=True) @@ -28,18 +28,30 @@ class PostIndex(indexes.SearchIndex): def index_queryset(self): return Post.objects.filter(deleted=False) +class UserIndex(indexes.SearchIndex): + text = indexes.CharField(document=True, use_template=True) + + def index_queryset(self): + return User.objects.all() + site.register(Post, PostIndex) site.register(Thread, ThreadIndex) +site.register(User, UserIndex) class AskbotSearchQuerySet(SearchQuerySet): - #def get_django_queryset(self, model_klass=Thread): - def get_django_queryset(self): + def get_django_queryset(self, model_klass=Thread): + '''dirty hack because models() method from the + SearchQuerySet does not work </3''' id_list = [] for r in self: - if getattr(r, 'thread_id'): - id_list.append(r.thread_id) - else: + if r.model_name in ['thread','post'] \ + and model_klass._meta.object_name.lower() == 'thread': + if getattr(r, 'thread_id'): + id_list.append(r.thread_id) + else: + id_list.append(r.pk) + elif r.model_name == model_klass._meta.object_name.lower(): id_list.append(r.pk) - return Thread.objects.filter(id__in=set(id_list)) + return model_klass.objects.filter(id__in=set(id_list)) |