Skip to content

Commit

Permalink
[bug 721184, 716999] Fixes wiki indexing
Browse files Browse the repository at this point in the history
Actively remove from the index and prevent from being indexed wiki documents
that are redirects or have no revisions.
  • Loading branch information
willkg committed Jan 25, 2012
1 parent 53f3b1c commit dec3db8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 1 deletion.
10 changes: 10 additions & 0 deletions apps/wiki/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,16 @@ def extract_document(self):
d['current'] = None
return d

@classmethod
def index(cls, document, **kwargs):
# If there are no revisions or the current revision is a redirect,
# we want to remove it from the index.
if (document['current'] is None or
document['content'].startswith(REDIRECT_HTML)):
cls.unindex(document['id'])
return
super(cls, cls).index(document, **kwargs)


register_for_indexing(Document, 'wiki')
register_for_indexing(
Expand Down
37 changes: 36 additions & 1 deletion apps/wiki/tests/test_es.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,17 @@
from nose.tools import eq_

from sumo.tests import ElasticTestCase
from wiki.tests import document
from wiki.tests import document, revision
from wiki.models import Document
from wiki.config import REDIRECT_CONTENT


class TestPostUpdate(ElasticTestCase):
def test_add_and_delete(self):
"""Adding a doc should add it to the search index; deleting should
delete it."""
doc = document(save=True)
revision(document=doc, is_approved=True, save=True)
self.refresh()
eq_(elasticutils.S(Document).count(), 1)

Expand All @@ -21,12 +23,14 @@ def test_add_and_delete(self):
def test_translations_get_parent_tags(self):
doc1 = document(title=u'Audio too loud')
doc1.save()
revision(document=doc1, is_approved=True, save=True)
doc1.tags.add(u'desktop')
doc1.tags.add(u'windows')

doc2 = document(title=u'Audio too loud bork bork',
parent=doc1)
doc2.save()
revision(document=doc2, is_approved=True, save=True)
doc2.tags.add(u'badtag')

# Verify the parent has the right tags.
Expand All @@ -45,6 +49,7 @@ def test_wiki_tags(self):
tag = u'hiphop'
eq_(elasticutils.S(Document).filter(tag=tag).count(), 0)
doc = document(save=True)
revision(document=doc, is_approved=True, save=True)
self.refresh()
eq_(elasticutils.S(Document).filter(tag=tag).count(), 0)
doc.tags.add(tag)
Expand All @@ -58,3 +63,33 @@ def test_wiki_tags(self):
eq_(elasticutils.S(Document).filter().count(), 1)

eq_(elasticutils.S(Document).filter(tag=tag).count(), 0)

def test_wiki_no_revisions(self):
"""Don't index documents without approved revisions"""
# Create a document with no revisions and make sure the
# document is not in the index.
doc = document(save=True)
self.refresh()
eq_(elasticutils.S(Document).count(), 0)
# Create a revision that's not approved and make sure the
# document is still not in the index.
revision(document=doc, is_approved=False, save=True)
self.refresh()
eq_(elasticutils.S(Document).count(), 0)

def test_wiki_redirects(self):
"""Make sure we don't index redirects"""
# First create a revision that doesn't have a redirect and
# make sure it's in the index.
doc = document(title=u'wool hats')
doc.save()
revision(document=doc, is_approved=True, save=True)
self.refresh()
eq_(elasticutils.S(Document).query('wool').count(), 1)

# Now create a revision that is a redirect and make sure the
# document is removed from the index.
revision(document=doc, content=REDIRECT_CONTENT, is_approved=True,
save=True)
self.refresh()
eq_(elasticutils.S(Document).query('wool').count(), 0)

0 comments on commit dec3db8

Please sign in to comment.