Commit 40847db7 by Syed Hasan raza

Merge pull request #10153 from edx/shr/bug/PLAT-875-Courses_Appear_in_search_after_deletion

Delete course entry from CourseAboutSearchIndex
parents f475200a 3102788b
......@@ -640,3 +640,22 @@ class CourseAboutSearchIndexer(object):
"Successfully added %s course to the course discovery index",
course_id
)
@classmethod
def _get_location_info(cls, normalized_structure_key):
""" Builds location info dictionary """
return {"course": unicode(normalized_structure_key), "org": normalized_structure_key.org}
@classmethod
def remove_deleted_items(cls, structure_key):
""" Remove item from Course About Search_index """
searcher = SearchEngine.get_search_engine(cls.INDEX_NAME)
if not searcher:
return
response = searcher.search(
doc_type=cls.DISCOVERY_DOCUMENT_TYPE,
field_dictionary=cls._get_location_info(structure_key)
)
result_ids = [result["data"]["id"] for result in response["results"]]
searcher.remove(cls.DISCOVERY_DOCUMENT_TYPE, result_ids)
......@@ -17,7 +17,7 @@ from django.conf import settings
from course_modes.models import CourseMode
from xmodule.library_tools import normalize_key_for_search
from xmodule.modulestore import ModuleStoreEnum
from xmodule.modulestore.django import SignalHandler
from xmodule.modulestore.django import SignalHandler, modulestore
from xmodule.modulestore.edit_info import EditInfoMixin
from xmodule.modulestore.exceptions import ItemNotFoundError
from xmodule.modulestore.inheritance import InheritanceMixin
......@@ -335,6 +335,25 @@ class TestCoursewareSearchIndexer(MixedWithOptionsTestCase):
response = self.search()
self.assertEqual(response["total"], 5)
def _test_delete_course_from_search_index_after_course_deletion(self, store): # pylint: disable=invalid-name
"""
Test that course will also be delete from search_index after course deletion.
"""
self.DOCUMENT_TYPE = 'course_info' # pylint: disable=invalid-name
response = self.search()
self.assertEqual(response["total"], 0)
# index the course in search_index
self.reindex_course(store)
response = self.search()
self.assertEqual(response["total"], 1)
# delete the course and look course in search_index
modulestore().delete_course(self.course.id, self.user_id)
self.assertIsNone(modulestore().get_course(self.course.id))
response = self.search()
self.assertEqual(response["total"], 0)
def _test_deleting_item(self, store):
""" test deleting an item """
# Publish the vertical to start with
......@@ -604,6 +623,11 @@ class TestCoursewareSearchIndexer(MixedWithOptionsTestCase):
def test_course_location_null(self, store_type):
self._perform_test_using_store(store_type, self._test_course_location_null)
@ddt.data(*WORKS_WITH_STORES)
def test_delete_course_from_search_index_after_course_deletion(self, store_type):
""" Test for removing course from CourseAboutSearchIndexer """
self._perform_test_using_store(store_type, self._test_delete_course_from_search_index_after_course_deletion)
@patch('django.conf.settings.SEARCH_ENGINE', 'search.tests.utils.ForceRefreshElasticSearchEngine')
@ddt.ddt
......
......@@ -24,3 +24,7 @@ def _listen_for_course_delete(sender, course_key, **kwargs): # pylint: disable=
invalidates the corresponding CourseOverview cache entry if one exists.
"""
CourseOverview.objects.filter(id=course_key).delete()
# import CourseAboutSearchIndexer inline due to cyclic import
from cms.djangoapps.contentstore.courseware_index import CourseAboutSearchIndexer
# Delete course entry from Course About Search_index
CourseAboutSearchIndexer.remove_deleted_items(course_key)
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment