Skip to content

Instantly share code, notes, and snippets.

@maracuja
Last active August 29, 2015 14:07
Show Gist options
  • Save maracuja/259bdd3f3e58421b2237 to your computer and use it in GitHub Desktop.
Save maracuja/259bdd3f3e58421b2237 to your computer and use it in GitHub Desktop.
Test Output of ElasticSearch
from collections import defaultdict, OrderedDict
from wagtail.wagtailcore.models import Page
from lma.apps.manager.models import ManagerPage
from lma.apps.article.models import ArticlePage
from lma.apps.video.models import VideoPage
search_types = OrderedDict([
('manager', ('Manager Profiles', ManagerPage)),
('statement', ('Statements', ArticlePage)),
('article', ('Articles', ArticlePage)),
('video', ('Videos', VideoPage)),
])
def search_type_key(search_klass):
for k, v in search_types.iteritems():
if search_klass == v[1]:
return '{0}_results'.format(k)
return 'other'
def search(request):
""" A simple search dispatch function """
search_type = request.REQUEST.get('t', "")
search_query = request.REQUEST.get('q', "")
search_results = defaultdict(list)
_, search_klass = search_types.get(search_type, ('', Page))
if search_query:
results = search_klass.search(search_query)
count_results = results.count()
for index in range(0, count_results):
if isinstance(results[index].specific, ArticlePage):
if results[index].get_parent().title == 'Statements' and search_type in ['', 'statement']:
if search_type != '' or len(search_results['statement_results']) < 10:
search_results['statement_results'].append(results[index].specific)
elif results[index].get_parent().title != 'Statements' and search_type in ['', 'article']:
if search_type != '' or len(search_results['article_results']) < 10:
search_results['article_results'].append(results[index].specific)
else:
result_group = search_type_key(results[index].specific_class)
search_results[result_group].append(results[index].specific)
return search_query, dict(search_results)
class mock_request:
q = "wenger"
t = "manager"
def get(self, field_name, default):
if field_name == 'q':
return self.q
elif field_name == 't':
return self.t
else:
return default
class mock_django_request:
REQUEST = mock_request()
test_request = mock_django_request()
search(test_request)
class mock_request:
q = "wenger"
t = ""
def get(self, field_name, default):
if field_name == 'q':
return self.q
elif field_name == 't':
return self.t
else:
return default
class mock_django_request:
REQUEST = mock_request()
from search.models import search
test_request = mock_django_request()
search(test_request)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment