'Extracts the ElasticSearch index document for this instance.'

@classmethod
def extract_document(cls, pk=None, obj=None):

    from mkt.webapps.models import AppFeatures, attach_devices, attach_prices, attach_translations, RatingDescriptors, RatingInteractives
    if (obj is None):
        obj = cls.get_model().objects.get(pk=pk)
    for transform in (attach_devices, attach_prices, attach_tags, attach_translations):
        transform([obj])
    latest_version = obj.latest_version
    version = obj.current_version
    features = (version.features.to_dict() if version else AppFeatures().to_dict())
    try:
        status = (latest_version.statuses[0][1] if latest_version else None)
    except IndexError:
        status = None
    attrs = ('app_slug', 'bayesian_rating', 'created', 'default_locale', 'guid', 'icon_hash', 'id', 'is_disabled', 'is_offline', 'file_size', 'last_updated', 'modified', 'premium_type', 'promo_img_hash', 'status', 'tv_featured')
    d = dict(zip(attrs, attrgetter(*attrs)(obj)))
    d['app_type'] = obj.app_type_id
    d['author'] = obj.developer_name
    d['category'] = (obj.categories if obj.categories else [])
    d['content_ratings'] = (obj.get_content_ratings_by_body(es=True) or None)
    try:
        d['content_descriptors'] = obj.rating_descriptors.to_keys()
    except RatingDescriptors.DoesNotExist:
        d['content_descriptors'] = []
    d['current_version'] = (version.version if version else None)
    d['device'] = getattr(obj, 'device_ids', [])
    d['features'] = features
    d['has_public_stats'] = obj.public_stats
    try:
        d['interactive_elements'] = obj.rating_interactives.to_keys()
    except RatingInteractives.DoesNotExist:
        d['interactive_elements'] = []
    d['installs_allowed_from'] = (version.manifest.get('installs_allowed_from', ['*']) if version else ['*'])
    d['is_priority'] = obj.priority_review
    is_escalated = obj.escalationqueue_set.exists()
    d['is_escalated'] = is_escalated
    d['escalation_date'] = (obj.escalationqueue_set.get().created if is_escalated else None)
    is_rereviewed = obj.rereviewqueue_set.exists()
    d['is_rereviewed'] = is_rereviewed
    d['rereview_date'] = (obj.rereviewqueue_set.get().created if is_rereviewed else None)
    if latest_version:
        d['latest_version'] = {'status': status, 'is_privileged': latest_version.is_privileged, 'has_editor_comment': latest_version.has_editor_comment, 'has_info_request': latest_version.has_info_request, 'nomination_date': latest_version.nomination, 'created_date': latest_version.created}
    else:
        d['latest_version'] = {'status': None, 'is_privileged': None, 'has_editor_comment': None, 'has_info_request': None, 'nomination_date': None, 'created_date': None}
    d['manifest_url'] = obj.get_manifest_url()
    d['package_path'] = obj.get_package_path()
    d['name_sort'] = unicode(obj.name).lower()
    d['owners'] = [au.user.id for au in obj.addonuser_set.filter(role=mkt.AUTHOR_ROLE_OWNER)]
    d['previews'] = [{'filetype': p.filetype, 'modified': p.modified, 'id': p.id, 'sizes': p.sizes} for p in obj.previews.all()]
    try:
        p = obj.addonpremium.price
        d['price_tier'] = p.name
    except AddonPremium.DoesNotExist:
        d['price_tier'] = None
    d['ratings'] = {'average': obj.average_rating, 'count': obj.total_reviews}
    d['region_exclusions'] = obj.get_excluded_region_ids()
    d['reviewed'] = obj.versions.filter(deleted=False).aggregate(Min('reviewed')).get('reviewed__min')
    supported_locales = [obj.default_locale]
    other_locales = (filter(None, version.supported_locales.split(',')) if version else [])
    if other_locales:
        supported_locales.extend(other_locales)
    d['supported_locales'] = list(set(supported_locales))
    d['tags'] = getattr(obj, 'tags_list', [])
    if (obj.upsell and obj.upsell.premium.is_published()):
        upsell_obj = obj.upsell.premium
        d['upsell'] = {'id': upsell_obj.id, 'app_slug': upsell_obj.app_slug, 'icon_url': upsell_obj.get_icon_url(128), 'name': unicode(upsell_obj.name), 'region_exclusions': upsell_obj.get_excluded_region_ids()}
    d['versions'] = [dict(version=v.version, resource_uri=reverse_version(v)) for v in obj.versions.all()]
    for field in ('description', 'name'):
        d.update(cls.extract_field_translations(obj, field, include_field_for_search=True))
    for field in ('homepage', 'support_email', 'support_url'):
        d.update(cls.extract_field_translations(obj, field))
    if version:
        attach_trans_dict(version._meta.model, [version])
        d.update(cls.extract_field_translations(version, 'release_notes', db_field='releasenotes_id'))
    else:
        d['release_notes_translations'] = None
    d.update(cls.extract_popularity_trending_boost(obj))
    if ((DEVICE_GAIA.id in d['device']) and obj.is_published()):
        d['name_suggest'] = {'input': d['name'], 'output': unicode(obj.id), 'weight': int(d['boost']), 'payload': {'default_locale': d['default_locale'], 'icon_hash': d['icon_hash'], 'id': d['id'], 'manifest_url': d['manifest_url'], 'modified': d['modified'], 'name_translations': d['name_translations'], 'slug': d['app_slug']}}
    for field in ('name', 'description'):
        d.update(cls.extract_field_analyzed_translations(obj, field))
    return d
