courseware_index.py 27 KB
Newer Older
1 2
""" Code to allow module store to interface with courseware index """
from __future__ import absolute_import
3
from abc import ABCMeta, abstractmethod
4 5
from datetime import timedelta
import logging
6
import re
7
from six import add_metaclass
8 9

from django.conf import settings
10
from django.utils.translation import ugettext_lazy, ugettext as _
11
from django.core.urlresolvers import resolve
12

13
from contentstore.course_group_config import GroupConfiguration
14
from course_modes.models import CourseMode
15
from eventtracking import tracker
16
from openedx.core.lib.courses import course_image_url
17 18
from search.search_engine_base import SearchEngine
from xmodule.annotator_mixin import html_to_text
19
from xmodule.modulestore import ModuleStoreEnum
20
from xmodule.library_tools import normalize_key_for_search
21 22 23 24 25 26 27 28 29 30 31

# REINDEX_AGE is the default amount of time that we look back for changes
# that might have happened. If we are provided with a time at which the
# indexing is triggered, then we know it is safe to only index items
# recently changed at that time. This is the time period that represents
# how far back from the trigger point to look back in order to index
REINDEX_AGE = timedelta(0, 60)  # 60 seconds

log = logging.getLogger('edx.modulestore')


32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
def strip_html_content_to_text(html_content):
    """ Gets only the textual part for html content - useful for building text to be searched """
    # Removing HTML-encoded non-breaking space characters
    text_content = re.sub(r"(\s| |//)+", " ", html_to_text(html_content))
    # Removing HTML CDATA
    text_content = re.sub(r"<!\[CDATA\[.*\]\]>", "", text_content)
    # Removing HTML comments
    text_content = re.sub(r"<!--.*-->", "", text_content)

    return text_content


def indexing_is_enabled():
    """
    Checks to see if the indexing feature is enabled
    """
    return settings.FEATURES.get('ENABLE_COURSEWARE_INDEX', False)


51 52 53 54 55 56 57 58
class SearchIndexingError(Exception):
    """ Indicates some error(s) occured during indexing """

    def __init__(self, message, error_list):
        super(SearchIndexingError, self).__init__(message)
        self.error_list = error_list


59
@add_metaclass(ABCMeta)
E. Kolpakov committed
60
class SearchIndexerBase(object):
61
    """
62
    Base class to perform indexing for courseware or library search from different modulestores
63
    """
64
    __metaclass__ = ABCMeta
65

66 67
    INDEX_NAME = None
    DOCUMENT_TYPE = None
68
    ENABLE_INDEXING_KEY = None
69 70 71 72 73 74 75

    INDEX_EVENT = {
        'name': None,
        'category': None
    }

    @classmethod
76 77 78 79 80 81 82
    def indexing_is_enabled(cls):
        """
        Checks to see if the indexing feature is enabled
        """
        return settings.FEATURES.get(cls.ENABLE_INDEXING_KEY, False)

    @classmethod
83
    @abstractmethod
84
    def normalize_structure_key(cls, structure_key):
85 86 87
        """ Normalizes structure key for use in indexing """

    @classmethod
88
    @abstractmethod
89
    def _fetch_top_level(cls, modulestore, structure_key):
90 91 92
        """ Fetch the item from the modulestore location """

    @classmethod
93
    @abstractmethod
94
    def _get_location_info(cls, normalized_structure_key):
95 96 97
        """ Builds location info dictionary """

    @classmethod
98
    def _id_modifier(cls, usage_id):
99 100 101 102 103 104 105 106 107 108 109 110
        """ Modifies usage_id to submit to index """
        return usage_id

    @classmethod
    def remove_deleted_items(cls, searcher, structure_key, exclude_items):
        """
        remove any item that is present in the search index that is not present in updated list of indexed items
        as we find items we can shorten the set of items to keep
        """
        response = searcher.search(
            doc_type=cls.DOCUMENT_TYPE,
            field_dictionary=cls._get_location_info(structure_key),
111
            exclude_dictionary={"id": list(exclude_items)}
112 113
        )
        result_ids = [result["data"]["id"] for result in response["results"]]
114
        searcher.remove(cls.DOCUMENT_TYPE, result_ids)
115

116
    @classmethod
117
    def index(cls, modulestore, structure_key, triggered_at=None, reindex_age=REINDEX_AGE):
118 119 120 121
        """
        Process course for indexing

        Arguments:
122 123
        modulestore - modulestore object to use for operations

124
        structure_key (CourseKey|LibraryKey) - course or library identifier
125 126 127 128 129 130 131 132 133 134 135 136

        triggered_at (datetime) - provides time at which indexing was triggered;
            useful for index updates - only things changed recently from that date
            (within REINDEX_AGE above ^^) will have their index updated, others skip
            updating their index but are still walked through in order to identify
            which items may need to be removed from the index
            If None, then a full reindex takes place

        Returns:
        Number of items that have been added to the index
        """
        error_list = []
137
        searcher = SearchEngine.get_search_engine(cls.INDEX_NAME)
138 139 140
        if not searcher:
            return

141
        structure_key = cls.normalize_structure_key(structure_key)
142
        location_info = cls._get_location_info(structure_key)
143

144
        # Wrap counter in dictionary - otherwise we seem to lose scope inside the embedded function `prepare_item_index`
145 146 147 148 149 150 151 152 153 154
        indexed_count = {
            "count": 0
        }

        # indexed_items is a list of all the items that we wish to remain in the
        # index, whether or not we are planning to actually update their index.
        # This is used in order to build a query to remove those items not in this
        # list - those are ready to be destroyed
        indexed_items = set()

155 156 157 158 159
        # items_index is a list of all the items index dictionaries.
        # it is used to collect all indexes and index them using bulk API,
        # instead of per item index API call.
        items_index = []

160 161 162 163 164 165
        def get_item_location(item):
            """
            Gets the version agnostic item location
            """
            return item.location.version_agnostic().replace(branch=None)

166
        def prepare_item_index(item, skip_index=False, groups_usage_info=None):
167
            """
168
            Add this item to the items_index and indexed_items list
169 170 171 172 173 174 175 176

            Arguments:
            item - item to add to index, its children will be processed recursively

            skip_index - simply walk the children in the tree, the content change is
                older than the REINDEX_AGE window and would have been already indexed.
                This should really only be passed from the recursive child calls when
                this method has determined that it is safe to do so
177 178 179

            Returns:
            item_content_groups - content groups assigned to indexed item
180 181 182 183 184 185 186
            """
            is_indexable = hasattr(item, "index_dictionary")
            item_index_dictionary = item.index_dictionary() if is_indexable else None
            # if it's not indexable and it does not have children, then ignore
            if not item_index_dictionary and not item.has_children:
                return

187
            item_content_groups = None
188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

            if item.category == "split_test":
                split_partition = item.get_selected_partition()
                for split_test_child in item.get_children():
                    if split_partition:
                        for group in split_partition.groups:
                            group_id = unicode(group.id)
                            child_location = item.group_id_to_child.get(group_id, None)
                            if child_location == split_test_child.location:
                                groups_usage_info.update({
                                    unicode(get_item_location(split_test_child)): [group_id],
                                })
                                for component in split_test_child.get_children():
                                    groups_usage_info.update({
                                        unicode(get_item_location(component)): [group_id]
                                    })

205
            if groups_usage_info:
206
                item_location = get_item_location(item)
207 208
                item_content_groups = groups_usage_info.get(unicode(item_location), None)

209
            item_id = unicode(cls._id_modifier(item.scope_ids.usage_id))
210 211 212 213 214
            indexed_items.add(item_id)
            if item.has_children:
                # determine if it's okay to skip adding the children herein based upon how recently any may have changed
                skip_child_index = skip_index or \
                    (triggered_at is not None and (triggered_at - item.subtree_edited_on) > reindex_age)
215
                children_groups_usage = []
216
                for child_item in item.get_children():
217
                    if modulestore.has_published_version(child_item):
218
                        children_groups_usage.append(
219
                            prepare_item_index(
220 221 222 223 224 225 226
                                child_item,
                                skip_index=skip_child_index,
                                groups_usage_info=groups_usage_info
                            )
                        )
                if None in children_groups_usage:
                    item_content_groups = None
227 228 229 230 231 232 233 234 235 236 237 238

            if skip_index or not item_index_dictionary:
                return

            item_index = {}
            # if it has something to add to the index, then add it
            try:
                item_index.update(location_info)
                item_index.update(item_index_dictionary)
                item_index['id'] = item_id
                if item.start:
                    item_index['start_date'] = item.start
239
                item_index['content_groups'] = item_content_groups if item_content_groups else None
240
                item_index.update(cls.supplemental_fields(item))
241
                items_index.append(item_index)
242
                indexed_count["count"] += 1
243
                return item_content_groups
244 245 246 247 248 249 250
            except Exception as err:  # pylint: disable=broad-except
                # broad exception so that index operation does not fail on one item of many
                log.warning('Could not index item: %s - %r', item.location, err)
                error_list.append(_('Could not index item: {}').format(item.location))

        try:
            with modulestore.branch_setting(ModuleStoreEnum.RevisionOption.published_only):
251
                structure = cls._fetch_top_level(modulestore, structure_key)
252
                groups_usage_info = cls.fetch_group_usage(modulestore, structure)
253 254 255 256 257

                # First perform any additional indexing from the structure object
                cls.supplemental_index_information(modulestore, structure)

                # Now index the content
258
                for item in structure.get_children():
259 260
                    prepare_item_index(item, groups_usage_info=groups_usage_info)
                searcher.index(cls.DOCUMENT_TYPE, items_index)
261
                cls.remove_deleted_items(searcher, structure_key, indexed_items)
262 263 264 265
        except Exception as err:  # pylint: disable=broad-except
            # broad exception so that index operation does not prevent the rest of the application from working
            log.exception(
                "Indexing error encountered, courseware index may be out of date %s - %r",
266
                structure_key,
267 268 269 270 271
                err
            )
            error_list.append(_('General indexing error occurred'))

        if error_list:
louyihua committed
272
            raise SearchIndexingError('Error(s) present during indexing', error_list)
273 274 275 276

        return indexed_count["count"]

    @classmethod
277
    def _do_reindex(cls, modulestore, structure_key):
278
        """
279 280
        (Re)index all content within the given structure (course or library),
        tracking the fact that a full reindex has taken place
281
        """
282
        indexed_count = cls.index(modulestore, structure_key)
283
        if indexed_count:
284
            cls._track_index_request(cls.INDEX_EVENT['name'], cls.INDEX_EVENT['category'], indexed_count)
285 286 287
        return indexed_count

    @classmethod
288
    def _track_index_request(cls, event_name, category, indexed_count):
289 290 291 292
        """Track content index requests.

        Arguments:
            event_name (str):  Name of the event to be logged.
E. Kolpakov committed
293
            category (str): category of indexed items
294
            indexed_count (int): number of indexed items
295 296 297 298 299 300
        Returns:
            None

        """
        data = {
            "indexed_count": indexed_count,
301
            'category': category,
302 303 304 305 306 307
        }

        tracker.emit(
            event_name,
            data
        )
308

309
    @classmethod
310 311 312 313 314 315 316
    def fetch_group_usage(cls, modulestore, structure):  # pylint: disable=unused-argument
        """
        Base implementation of fetch group usage on course/library.
        """
        return None

    @classmethod
317 318 319 320 321 322 323 324 325 326 327 328 329 330
    def supplemental_index_information(cls, modulestore, structure):
        """
        Perform any supplemental indexing given that the structure object has
        already been loaded. Base implementation performs no operation.

        Arguments:
            modulestore - modulestore object used during the indexing operation
            structure - structure object loaded during the indexing job

        Returns:
            None
        """
        pass

331 332 333 334 335 336 337 338
    @classmethod
    def supplemental_fields(cls, item):  # pylint: disable=unused-argument
        """
        Any supplemental fields that get added to the index for the specified
        item. Base implementation returns an empty dictionary
        """
        return {}

339

E. Kolpakov committed
340
class CoursewareSearchIndexer(SearchIndexerBase):
341 342 343
    """
    Class to perform indexing for courseware search from different modulestores
    """
344 345
    INDEX_NAME = "courseware_index"
    DOCUMENT_TYPE = "courseware_content"
346
    ENABLE_INDEXING_KEY = 'ENABLE_COURSEWARE_INDEX'
347 348 349 350 351 352

    INDEX_EVENT = {
        'name': 'edx.course.index.reindexed',
        'category': 'courseware_index'
    }

353
    UNNAMED_MODULE_NAME = ugettext_lazy("(Unnamed)")
354

355
    @classmethod
356
    def normalize_structure_key(cls, structure_key):
357 358 359 360 361
        """ Normalizes structure key for use in indexing """
        return structure_key

    @classmethod
    def _fetch_top_level(cls, modulestore, structure_key):
362 363 364 365
        """ Fetch the item from the modulestore location """
        return modulestore.get_course(structure_key, depth=None)

    @classmethod
366
    def _get_location_info(cls, normalized_structure_key):
367
        """ Builds location info dictionary """
368
        return {"course": unicode(normalized_structure_key), "org": normalized_structure_key.org}
369 370 371 372 373 374 375 376

    @classmethod
    def do_course_reindex(cls, modulestore, course_key):
        """
        (Re)index all content within the given course, tracking the fact that a full reindex has taken place
        """
        return cls._do_reindex(modulestore, course_key)

377
    @classmethod
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
    def fetch_group_usage(cls, modulestore, structure):
        groups_usage_dict = {}
        groups_usage_info = GroupConfiguration.get_content_groups_usage_info(modulestore, structure).items()
        groups_usage_info.extend(
            GroupConfiguration.get_content_groups_items_usage_info(
                modulestore,
                structure
            ).items()
        )
        if groups_usage_info:
            for name, group in groups_usage_info:
                for module in group:
                    view, args, kwargs = resolve(module['url'])  # pylint: disable=unused-variable
                    usage_key_string = unicode(kwargs['usage_key_string'])
                    if groups_usage_dict.get(usage_key_string, None):
                        groups_usage_dict[usage_key_string].append(name)
                    else:
                        groups_usage_dict[usage_key_string] = [name]
        return groups_usage_dict

    @classmethod
399 400 401 402 403 404
    def supplemental_index_information(cls, modulestore, structure):
        """
        Perform additional indexing from loaded structure object
        """
        CourseAboutSearchIndexer.index_about_information(modulestore, structure)

405 406 407 408 409 410 411 412 413 414 415 416 417 418 419
    @classmethod
    def supplemental_fields(cls, item):
        """
        Add location path to the item object

        Once we've established the path of names, the first name is the course
        name, and the next 3 names are the navigable path within the edx
        application. Notice that we stop at that level because a full path to
        deep children would be confusing.
        """
        location_path = []
        parent = item
        while parent is not None:
            path_component_name = parent.display_name
            if not path_component_name:
420
                path_component_name = unicode(cls.UNNAMED_MODULE_NAME)
421 422 423 424 425 426 427 428
            location_path.append(path_component_name)
            parent = parent.get_parent()
        location_path.reverse()
        return {
            "course_name": location_path[0],
            "location": location_path[1:4]
        }

429

E. Kolpakov committed
430
class LibrarySearchIndexer(SearchIndexerBase):
431 432 433
    """
    Base class to perform indexing for library search from different modulestores
    """
434 435
    INDEX_NAME = "library_index"
    DOCUMENT_TYPE = "library_content"
436
    ENABLE_INDEXING_KEY = 'ENABLE_LIBRARY_INDEX'
437 438 439 440 441 442 443

    INDEX_EVENT = {
        'name': 'edx.library.index.reindexed',
        'category': 'library_index'
    }

    @classmethod
444
    def normalize_structure_key(cls, structure_key):
445
        """ Normalizes structure key for use in indexing """
446
        return normalize_key_for_search(structure_key)
447 448 449

    @classmethod
    def _fetch_top_level(cls, modulestore, structure_key):
450 451 452 453
        """ Fetch the item from the modulestore location """
        return modulestore.get_library(structure_key, depth=None)

    @classmethod
454
    def _get_location_info(cls, normalized_structure_key):
455
        """ Builds location info dictionary """
456
        return {"library": unicode(normalized_structure_key)}
457 458

    @classmethod
459
    def _id_modifier(cls, usage_id):
460 461 462 463 464 465 466 467
        """ Modifies usage_id to submit to index """
        return usage_id.replace(library_key=(usage_id.library_key.replace(version_guid=None, branch=None)))

    @classmethod
    def do_library_reindex(cls, modulestore, library_key):
        """
        (Re)index all content within the given library, tracking the fact that a full reindex has taken place
        """
468
        return cls._do_reindex(modulestore, library_key)
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570


class AboutInfo(object):
    """ About info structure to contain
       1) Property name to use
       2) Where to add in the index (using flags above)
       3) Where to source the properties value
    """
    # Bitwise Flags for where to index the information
    #
    # ANALYSE - states that the property text contains content that we wish to be able to find matched within
    #   e.g. "joe" should yield a result for "I'd like to drink a cup of joe"
    #
    # PROPERTY - states that the property text should be a property of the indexed document, to be returned with the
    # results: search matches will only be made on exact string matches
    #   e.g. "joe" will only match on "joe"
    #
    # We are using bitwise flags because one may want to add the property to EITHER or BOTH parts of the index
    #   e.g. university name is desired to be analysed, so that a search on "Oxford" will match
    #   property values "University of Oxford" and "Oxford Brookes University",
    #   but it is also a useful property, because within a (future) filtered search a user
    #   may have chosen to filter courses from "University of Oxford"
    #
    # see https://wiki.python.org/moin/BitwiseOperators for information about bitwise shift operator used below
    #
    ANALYSE = 1 << 0  # Add the information to the analysed content of the index
    PROPERTY = 1 << 1  # Add the information as a property of the object being indexed (not analysed)

    def __init__(self, property_name, index_flags, source_from):
        self.property_name = property_name
        self.index_flags = index_flags
        self.source_from = source_from

    def get_value(self, **kwargs):
        """ get the value for this piece of information, using the correct source """
        return self.source_from(self, **kwargs)

    def from_about_dictionary(self, **kwargs):
        """ gets the value from the kwargs provided 'about_dictionary' """
        about_dictionary = kwargs.get('about_dictionary', None)
        if not about_dictionary:
            raise ValueError("Context dictionary does not contain expected argument 'about_dictionary'")

        return about_dictionary.get(self.property_name, None)

    def from_course_property(self, **kwargs):
        """ gets the value from the kwargs provided 'course' """
        course = kwargs.get('course', None)
        if not course:
            raise ValueError("Context dictionary does not contain expected argument 'course'")

        return getattr(course, self.property_name, None)

    def from_course_mode(self, **kwargs):
        """ fetches the available course modes from the CourseMode model """
        course = kwargs.get('course', None)
        if not course:
            raise ValueError("Context dictionary does not contain expected argument 'course'")

        return [mode.slug for mode in CourseMode.modes_for_course(course.id)]

    # Source location options - either from the course or the about info
    FROM_ABOUT_INFO = from_about_dictionary
    FROM_COURSE_PROPERTY = from_course_property
    FROM_COURSE_MODE = from_course_mode


class CourseAboutSearchIndexer(object):
    """
    Class to perform indexing of about information from course object
    """
    DISCOVERY_DOCUMENT_TYPE = "course_info"
    INDEX_NAME = CoursewareSearchIndexer.INDEX_NAME

    # List of properties to add to the index - each item in the list is an instance of AboutInfo object
    ABOUT_INFORMATION_TO_INCLUDE = [
        AboutInfo("advertised_start", AboutInfo.PROPERTY, AboutInfo.FROM_COURSE_PROPERTY),
        AboutInfo("announcement", AboutInfo.PROPERTY, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("start", AboutInfo.PROPERTY, AboutInfo.FROM_COURSE_PROPERTY),
        AboutInfo("end", AboutInfo.PROPERTY, AboutInfo.FROM_COURSE_PROPERTY),
        AboutInfo("effort", AboutInfo.PROPERTY, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("display_name", AboutInfo.ANALYSE, AboutInfo.FROM_COURSE_PROPERTY),
        AboutInfo("overview", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("title", AboutInfo.ANALYSE | AboutInfo.PROPERTY, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("university", AboutInfo.ANALYSE | AboutInfo.PROPERTY, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("number", AboutInfo.ANALYSE | AboutInfo.PROPERTY, AboutInfo.FROM_COURSE_PROPERTY),
        AboutInfo("short_description", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("description", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("key_dates", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("video", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("course_staff_short", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("course_staff_extended", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("requirements", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("syllabus", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("textbook", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("faq", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("more_info", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("ocw_links", AboutInfo.ANALYSE, AboutInfo.FROM_ABOUT_INFO),
        AboutInfo("enrollment_start", AboutInfo.PROPERTY, AboutInfo.FROM_COURSE_PROPERTY),
        AboutInfo("enrollment_end", AboutInfo.PROPERTY, AboutInfo.FROM_COURSE_PROPERTY),
        AboutInfo("org", AboutInfo.PROPERTY, AboutInfo.FROM_COURSE_PROPERTY),
        AboutInfo("modes", AboutInfo.PROPERTY, AboutInfo.FROM_COURSE_MODE),
571
        AboutInfo("language", AboutInfo.PROPERTY, AboutInfo.FROM_COURSE_PROPERTY),
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630
    ]

    @classmethod
    def index_about_information(cls, modulestore, course):
        """
        Add the given course to the course discovery index

        Arguments:
        modulestore - modulestore object to use for operations

        course - course object from which to take properties, locate about information
        """
        searcher = SearchEngine.get_search_engine(cls.INDEX_NAME)
        if not searcher:
            return

        course_id = unicode(course.id)
        course_info = {
            'id': course_id,
            'course': course_id,
            'content': {},
            'image_url': course_image_url(course),
        }

        # load data for all of the 'about' modules for this course into a dictionary
        about_dictionary = {
            item.location.name: item.data
            for item in modulestore.get_items(course.id, qualifiers={"category": "about"})
        }

        about_context = {
            "course": course,
            "about_dictionary": about_dictionary,
        }

        for about_information in cls.ABOUT_INFORMATION_TO_INCLUDE:
            # Broad exception handler so that a single bad property does not scupper the collection of others
            try:
                section_content = about_information.get_value(**about_context)
            except:  # pylint: disable=bare-except
                section_content = None
                log.warning(
                    "Course discovery could not collect property %s for course %s",
                    about_information.property_name,
                    course_id,
                    exc_info=True,
                )

            if section_content:
                if about_information.index_flags & AboutInfo.ANALYSE:
                    analyse_content = section_content
                    if isinstance(section_content, basestring):
                        analyse_content = strip_html_content_to_text(section_content)
                    course_info['content'][about_information.property_name] = analyse_content
                if about_information.index_flags & AboutInfo.PROPERTY:
                    course_info[about_information.property_name] = section_content

        # Broad exception handler to protect around and report problems with indexing
        try:
631
            searcher.index(cls.DISCOVERY_DOCUMENT_TYPE, [course_info])
632 633 634 635 636 637 638 639 640 641 642
        except:  # pylint: disable=bare-except
            log.exception(
                "Course discovery indexing error encountered, course discovery index may be out of date %s",
                course_id,
            )
            raise

        log.debug(
            "Successfully added %s course to the course discovery index",
            course_id
        )
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661

    @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)