Commits

Philipp Wassibauer  committed 347a80f

fulltext search with haystack

  • Participants
  • Parent commits 9f1533c

Comments (0)

Files changed (1)

File djangobb_forum/views.py

             search_in = request.GET.get('search_in')
             sort_by = request.GET.get('sort_by')
             sort_dir = request.GET.get('sort_dir')
-
-            if keywords and author:
-                if search_in == 'all':
-                    if forum == '0':
-                        query = 'user:%s AND (topic:%s OR body:%s)' % (author, keywords, keywords)
-                    else:
-                        query = 'user:%s AND forum:%s AND (topic:%s OR body:%s)' % (author, forum, keywords, keywords)
-                elif search_in == 'message':
-                    if forum == '0':
-                        query = 'user:%s AND body:%s' % (author, keywords)
-                    else:
-                        query = 'user:%s AND forum:%s AND body:%s' % (author, forum, keywords)
-                elif search_in == 'topic':
-                    if forum == '0':
-                        query = 'user:%s AND topic:%s' % (author, keywords)
-                    else:
-                        query = 'user:%s AND forum:%s AND topic:%s' % (author, forum, keywords)
-            elif keywords:
-                if search_in == 'all':
-                    if forum == '0':
-                        query = 'topic:%s OR body:%s' % (keywords, keywords)
-                    else:
-                        query = 'forum:%s AND (topic:%s OR body:%s)' % (forum, keywords, keywords)
-                elif search_in == 'message':
-                    if forum == '0':
-                        query = 'body:%s' % (keywords)
-                    else:
-                        query = 'forum:%s AND body:%s' % (forum, keywords)
-                elif search_in == 'topic':
-                    if forum == '0':
-                        query = 'topic:%s' % (keywords)
-                    else:
-                        query = 'forum:%s AND topic:%s' % (forum, keywords)
-            elif author:
-                if forum == '0':
-                    query = 'user:%s' % (author)
-                else:
-                    query = 'forum:%s AND user:%s' % (forum, author)
-            else:
-                return HttpResponseRedirect(reverse('djangobb:search'))
-
+            
+            from haystack.query import SearchQuerySet
+            posts = SearchQuerySet().models(Post)
+           
+            if keywords:
+                posts = posts.filter(content=keywords)
+                
+            if author:
+                user = User.objects.filter(username=author)
+                if user:
+                    posts = posts.filter(user=user[0].pk)
+                    
+            if forum:
+                posts = posts.filter(forum=forum)
+                
             order = {'0': 'created',
                      '1': 'user',
                      '2': 'topic',
             if sort_dir == 'DESC':
                 order = '-' + order
                 
-            from djangobb_forum.index import post_indexer
-            posts = post_indexer.search(query).order_by(order)
-
-            if 'topics' in request.GET['show_as']:
-                topics = []
-                for post in posts:
-                    if post.instance.topic not in topics:
-                        topics.append(post.instance.topic)
-                return {'paged_qs': topics}
-            elif 'posts' in request.GET['show_as']:
-                return {'paged_qs': posts,
-                        'TEMPLATE': 'forum/search_posts.html'
-                        }
-        return {'paged_qs': topics}
+            return {'posts': posts,
+                    'keywords': keywords,
+                    'TEMPLATE': 'forum/search_posts.html'}
+        return {'posts': posts}
     else:
         form = PostSearchForm()
         return {'categories': Category.objects.all(),