"""News app documents.""" from django.conf import settings from django_elasticsearch_dsl import Document, Index, fields from search_indexes.utils import OBJECT_FIELD_PROPERTIES from news import models NewsIndex = Index(settings.ELASTICSEARCH_INDEX_NAMES.get(__name__, 'news')) NewsIndex.settings(number_of_shards=1, number_of_replicas=1) @NewsIndex.doc_type class NewsDocument(Document): """News document.""" news_type = fields.ObjectField(properties={'id': fields.IntegerField(), 'name': fields.KeywordField()}) title = fields.ObjectField(attr='title_indexing', properties=OBJECT_FIELD_PROPERTIES) subtitle = fields.ObjectField(attr='subtitle_indexing', properties=OBJECT_FIELD_PROPERTIES) description = fields.ObjectField(attr='description_indexing', properties=OBJECT_FIELD_PROPERTIES) country = fields.ObjectField(properties={'id': fields.IntegerField(), 'code': fields.KeywordField()}) web_url = fields.KeywordField(attr='web_url') image_url = fields.KeywordField(attr='image_url') preview_image_url = fields.KeywordField(attr='preview_image_url') tags = fields.ObjectField( properties={ 'id': fields.IntegerField(attr='id'), 'label': fields.ObjectField(attr='label_indexing', properties=OBJECT_FIELD_PROPERTIES), 'value': fields.KeywordField() }, multi=True) visible_tags = fields.ObjectField( properties={ 'id': fields.IntegerField(attr='id'), 'label': fields.ObjectField(attr='label_indexing', properties=OBJECT_FIELD_PROPERTIES), }, multi=True) favorites_for_users = fields.ListField(field=fields.IntegerField()) class Django: model = models.News fields = ( 'id', 'start', 'end', 'slug', 'state', 'is_highlighted', 'template', ) related_models = [models.NewsType] def get_queryset(self): return super().get_queryset().published().with_base_related() def get_instances_from_related(self, related_instance): """If related_models is set, define how to retrieve the Car instance(s) from the related model. The related_models option should be used with caution because it can lead in the index to the updating of a lot of items. """ if isinstance(related_instance, models.NewsType): return related_instance.news_set.all()