gault-millau/apps/search_indexes/documents/news.py
2020-01-22 20:23:46 +03:00

76 lines
3.0 KiB
Python

"""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
from json import dumps
NewsIndex = Index(settings.ELASTICSEARCH_INDEX_NAMES.get(__name__, 'news'))
NewsIndex.settings(number_of_shards=10, 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)
slugs = fields.KeywordField()
backoffice_title = fields.TextField(analyzer='english')
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())
start = fields.DateField(attr='publication_datetime')
has_any_desc_active = fields.BooleanField()
def prepare_slugs(self, instance):
return dumps(instance.slugs or {})
class Django:
model = models.News
fields = (
'id',
'end',
'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) and hasattr(related_instance, 'news'):
return related_instance.news.all()