Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unified 755726 #660

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions apps/forums/models.py
Expand Up @@ -178,6 +178,10 @@ def update_last_post(self, exclude_post=None):
# If self.last_post is None, and this was called from Post.delete,
# then Post.delete will erase the thread, as well.

@classmethod
def get_query_fields(cls):
return ['post_title__text', 'post_content__text']

@classmethod
def get_mapping(cls):
return {
Expand Down
6 changes: 6 additions & 0 deletions apps/questions/models.py
Expand Up @@ -298,6 +298,12 @@ def my_tags(self):
cache.add(cache_key, tags, CACHE_TIMEOUT)
return tags

@classmethod
def get_query_fields(cls):
return ['question_title__text',
'question_content__text',
'question_answer_content__text']

@classmethod
def get_mapping(cls):
return {
Expand Down
3 changes: 3 additions & 0 deletions apps/search/es_utils.py
Expand Up @@ -40,6 +40,9 @@
log = logging.getLogger('search.es')


F = elasticutils.F


class Sphilastic(elasticutils.S):
"""Shim around elasticutils' S which makes it look like oedipus.S

Expand Down
4 changes: 4 additions & 0 deletions apps/search/models.py
Expand Up @@ -106,6 +106,10 @@ def unindex_later(self):
"""Register myself to be unindexed at the end of the request."""
_local_tasks().add((unindex_task.delay, (self.__class__, (self.id,))))

@classmethod
def get_query_fields(cls):
return []

@classmethod
def get_indexable(cls):
# Some models have a gazillion instances. So we want to go
Expand Down
5 changes: 4 additions & 1 deletion apps/search/templates/search/includes/macros.html
Expand Up @@ -3,6 +3,9 @@
{% macro search_engine() -%}
{# Must be imported with context #}
<input type="hidden" name="e" value="{% if waffle.flag('elasticsearch') %}es{% else %}sph{% endif %}" />
{% if 'esunified' in request.GET and request.GET['esunified'] %}
<input type="hidden" name="esunified" value="1" />
{% endif %}
{%- endmacro %}

{% macro basic_search_form(primary=False) -%}
Expand All @@ -20,4 +23,4 @@
<input type="submit" class="search-refine-submit btn-large" value="{{ _('Search') }}">
</form>
</div>
{%- endmacro %}
{%- endmacro %}
247 changes: 247 additions & 0 deletions apps/search/tests/test_es.py
Expand Up @@ -462,6 +462,253 @@ def test_forums_thread_created(self):
eq_(content['total'], 0)


class ElasticSearchUnifiedViewTests(ElasticTestCase):
client_class = LocalizingClient

def setUp(self):
super(ElasticSearchUnifiedViewTests, self).setUp()
Flag.objects.create(name='esunified', everyone=True)

def test_front_page_search_for_questions(self):
"""This tests whether doing a search from the front page returns
question results.

Bug #709202.

"""
# Create a question with an answer with an answervote that
# marks the answer as helpful. The question should have the
# "desktop" tag.
ques = question(
title=u'audio fails',
content=u'my audio dont work.')
ques.save()

ques.tags.add(u'desktop')

ans = answer(
question=ques,
content=u'You need to turn your volume up.')
ans.save()

ansvote = answervote(
answer=ans,
helpful=True)
ansvote.save()

self.refresh()

# This is the search that you get when you start on the sumo
# homepage and do a search from the box with two differences:
# first, we do it in json since it's easier to deal with
# testing-wise and second, we search for 'audio' since we have
# data for that.
response = self.client.get(reverse('search'), {
'q_tags': 'desktop', 'product': 'desktop', 'q': 'audio',
'format': 'json'
})

eq_(200, response.status_code)

content = json.loads(response.content)
eq_(content['total'], 1)

# This is another search that picks up results based on the
# answer_content. answer_content is in a string array, so
# this makes sure that works.
response = self.client.get(reverse('search'), {
'q_tags': 'desktop', 'product': 'desktop', 'q': 'volume',
'format': 'json'
})

eq_(200, response.status_code)

content = json.loads(response.content)
eq_(content['total'], 1)

def test_front_page_search_for_wiki(self):
"""This tests whether doing a search from the front page returns
wiki document results.

Bug #709202.

"""
doc = document(
title=u'How to fix your audio',
locale=u'en-US',
category=10)
doc.save()

doc.tags.add(u'desktop')

rev = revision(
document=doc,
summary=u'Volume.',
content=u'Turn up the volume.',
is_approved=True)
rev.save()

self.refresh()

# This is the search that you get when you start on the sumo
# homepage and do a search from the box with two differences:
# first, we do it in json since it's easier to deal with
# testing-wise and second, we search for 'audio' since we have
# data for that.
response = self.client.get(reverse('search'), {
'q_tags': 'desktop', 'product': 'desktop', 'q': 'audio',
'format': 'json'
})

eq_(200, response.status_code)

content = json.loads(response.content)
eq_(content['total'], 1)

def test_advanced_search_for_wiki_no_query(self):
"""Tests advanced search with no query"""
doc = document(
title=u'How to fix your audio',
locale=u'en-US',
category=10)
doc.save()

doc.tags.add(u'desktop')

rev = revision(
document=doc,
summary=u'Volume.',
content=u'Turn up the volume.',
is_approved=True)
rev.save()

self.refresh()

response = self.client.get(reverse('search'), {
'q': '', 'tags': 'desktop', 'w': '1', 'a': '1',
'format': 'json'
})

eq_(200, response.status_code)

content = json.loads(response.content)
eq_(content['total'], 1)

def test_advanced_search_questions_sortby(self):
"""Tests advanced search for questions with a sortby"""
question(title=u'tags tags tags', save=True)

self.refresh()

# Advanced search for questions with sortby set to 3 which is
# '-replies' which is different between Sphinx and ES.
response = self.client.get(reverse('search'), {
'q': 'tags', 'tags': 'desktop', 'w': '2', 'a': '1', 'sortby': '3',
'format': 'json'
})

eq_(200, response.status_code)

content = json.loads(response.content)
eq_(content['total'], 1)

def test_forums_search(self):
"""This tests whether forum posts show up in searches."""
thread1 = thread(
title=u'Why don\'t we spell crash backwards?')
thread1.save()

post1 = post(
thread=thread1,
content=u'What, like hsarc? That\'s silly.')
post1.save()

self.refresh()

response = self.client.get(reverse('search'), {
'author': '', 'created': '0', 'created_date': '',
'updated': '0', 'updated_date': '', 'sortby': '0',
'a': '1', 'w': '4', 'q': 'hsarc',
'format': 'json'
})

eq_(200, response.status_code)

content = json.loads(response.content)
eq_(content['total'], 1)

def test_forums_thread_created(self):
post_created_ds = datetime.datetime(2010, 1, 1, 12, 00)
thread1 = thread(
title=u'Why don\'t we spell crash backwards?',
created=post_created_ds)
thread1.save()

post1 = post(
thread=thread1,
content=u'What, like hsarc? That\'s silly.',
created=(post_created_ds + datetime.timedelta(hours=1)))
post1.save()

self.refresh()

# The thread/post should not show up in results for items
# created AFTER 1/12/2010.
response = self.client.get(reverse('search'), {
'author': '', 'created': '2', 'created_date': '01/12/2010',
'updated': '0', 'updated_date': '', 'sortby': '0',
'a': '1', 'w': '4', 'q': 'hsarc',
'format': 'json'
})

eq_(200, response.status_code)

content = json.loads(response.content)
eq_(content['total'], 0)

# The thread/post should show up in results for items created
# AFTER 1/1/2010.
response = self.client.get(reverse('search'), {
'author': '', 'created': '2', 'created_date': '01/01/2010',
'updated': '0', 'updated_date': '', 'sortby': '0',
'a': '1', 'w': '4', 'q': 'hsarc',
'format': 'json'
})

eq_(200, response.status_code)

content = json.loads(response.content)
eq_(content['total'], 1)

# The thread/post should show up in results for items created
# BEFORE 1/12/2010.
response = self.client.get(reverse('search'), {
'author': '', 'created': '1', 'created_date': '01/12/2010',
'updated': '0', 'updated_date': '', 'sortby': '0',
'a': '1', 'w': '4', 'q': 'hsarc',
'format': 'json'
})

eq_(200, response.status_code)

content = json.loads(response.content)
eq_(content['total'], 1)

# The thread/post should NOT show up in results for items
# created BEFORE 12/31/2009.
response = self.client.get(reverse('search'), {
'author': '', 'created': '1', 'created_date': '12/31/2009',
'updated': '0', 'updated_date': '', 'sortby': '0',
'a': '1', 'w': '4', 'q': 'hsarc',
'format': 'json'
})

eq_(200, response.status_code)

content = json.loads(response.content)
eq_(content['total'], 0)


class ElasticSearchUtilsTests(ElasticTestCase):
def test_get_documents(self):
q = question(save=True)
Expand Down