gault-millau/apps/search_indexes/documents/news.py
Dmitriy Kuzmenko 1e1b09660d fix news_type
2019-09-20 15:55:37 +03:00

57 lines
1.8 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
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.NestedField(properties={
'id': fields.IntegerField(),
'name': fields.KeywordField()
})
title = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES)
subtitle = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES)
description = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES)
country = fields.NestedField(properties={
'id': fields.IntegerField(),
'code': fields.KeywordField()
})
web_url = fields.KeywordField(attr='web_url')
class Django:
model = models.News
fields = (
'id',
'playlist',
)
related_models = [models.NewsType]
def get_queryset(self):
return super().get_queryset().published()
def prepare_title(self, instance):
return instance.title
def prepare_subtitle(self, instance):
return instance.subtitle
def prepare_description(self, instance):
return instance.description
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()