Establishment index
This commit is contained in:
parent
87efc50d94
commit
9fab4454a0
|
|
@ -1,6 +1,8 @@
|
|||
from search_indexes.documents.establishment import EstablishmentDocument
|
||||
from search_indexes.documents.news import NewsDocument
|
||||
|
||||
|
||||
__all__ = [
|
||||
'EstablishmentDocument',
|
||||
'NewsDocument',
|
||||
]
|
||||
34
apps/search_indexes/documents/establishment.py
Normal file
34
apps/search_indexes/documents/establishment.py
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
"""Establishment app documents."""
|
||||
from django.conf import settings
|
||||
from django_elasticsearch_dsl import Document, Index, fields
|
||||
from search_indexes.utils import OBJECT_FIELD_PROPERTIES
|
||||
from establishment import models
|
||||
|
||||
|
||||
EstablishmentIndex = Index(settings.ELASTICSEARCH_INDEX_NAMES.get(__name__,
|
||||
'establishment'))
|
||||
EstablishmentIndex.settings(number_of_shards=1, number_of_replicas=1)
|
||||
|
||||
|
||||
@EstablishmentIndex.doc_type
|
||||
class EstablishmentDocument(Document):
|
||||
"""Establishment document."""
|
||||
|
||||
name = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES)
|
||||
description = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES)
|
||||
|
||||
class Django:
|
||||
|
||||
model = models.Establishment
|
||||
fields = (
|
||||
'id',
|
||||
'public_mark',
|
||||
'toque_number',
|
||||
'price_level',
|
||||
)
|
||||
|
||||
def prepare_name(self, instance):
|
||||
return instance.name
|
||||
|
||||
def prepare_description(self, instance):
|
||||
return instance.description
|
||||
|
|
@ -5,11 +5,11 @@ from search_indexes.utils import OBJECT_FIELD_PROPERTIES
|
|||
from news import models
|
||||
|
||||
|
||||
NEWS_INDEX = Index(settings.ELASTICSEARCH_INDEX_NAMES[__name__])
|
||||
NEWS_INDEX.settings(number_of_shards=1, number_of_replicas=1)
|
||||
NewsIndex = Index(settings.ELASTICSEARCH_INDEX_NAMES.get(__name__, 'news'))
|
||||
NewsIndex.settings(number_of_shards=1, number_of_replicas=1)
|
||||
|
||||
|
||||
@NEWS_INDEX.doc_type
|
||||
@NewsIndex.doc_type
|
||||
class NewsDocument(Document):
|
||||
"""News document."""
|
||||
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
"""Search indexes serializers."""
|
||||
from rest_framework import serializers
|
||||
from django_elasticsearch_dsl_drf.serializers import DocumentSerializer
|
||||
from search_indexes.documents.news import NewsDocument
|
||||
from search_indexes.documents import EstablishmentDocument, NewsDocument
|
||||
from search_indexes.utils import get_translated_value
|
||||
|
||||
|
||||
|
|
@ -38,3 +38,33 @@ class NewsDocumentSerializer(DocumentSerializer):
|
|||
@staticmethod
|
||||
def get_description_translated(obj):
|
||||
return get_translated_value(obj.description)
|
||||
|
||||
|
||||
class EstablishmentDocumentSerializer(DocumentSerializer):
|
||||
"""Establishment document serializer."""
|
||||
|
||||
name_translated = serializers.SerializerMethodField(allow_null=True)
|
||||
description_translated = serializers.SerializerMethodField(allow_null=True)
|
||||
|
||||
class Meta:
|
||||
"""Meta class."""
|
||||
|
||||
document = EstablishmentDocument
|
||||
fields = (
|
||||
'id',
|
||||
'name',
|
||||
'description',
|
||||
'public_mark',
|
||||
'toque_number',
|
||||
'price_level',
|
||||
'name_translated',
|
||||
'description_translated',
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_name_translated(obj):
|
||||
return get_translated_value(obj.name)
|
||||
|
||||
@staticmethod
|
||||
def get_description_translated(obj):
|
||||
return get_translated_value(obj.description)
|
||||
|
|
|
|||
|
|
@ -5,5 +5,6 @@ from search_indexes import views
|
|||
|
||||
router = routers.SimpleRouter()
|
||||
router.register(r'news', views.NewsDocumentViewSet, basename='news')
|
||||
router.register(r'establishments', views.EstablishmentDocumentViewSet, basename='establishment')
|
||||
|
||||
urlpatterns = router.urls
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ from rest_framework import permissions
|
|||
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
|
||||
from django_elasticsearch_dsl_drf.pagination import PageNumberPagination
|
||||
from search_indexes import serializers, filters
|
||||
from search_indexes.documents import NewsDocument
|
||||
from search_indexes.documents import EstablishmentDocument, NewsDocument
|
||||
|
||||
|
||||
class NewsDocumentViewSet(BaseDocumentViewSet):
|
||||
|
|
@ -29,4 +29,31 @@ class NewsDocumentViewSet(BaseDocumentViewSet):
|
|||
'title',
|
||||
'subtitle',
|
||||
'description',
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
class EstablishmentDocumentViewSet(BaseDocumentViewSet):
|
||||
"""Establishment document ViewSet."""
|
||||
|
||||
document = EstablishmentDocument
|
||||
lookup_field = 'id'
|
||||
pagination_class = PageNumberPagination
|
||||
permission_classes = (permissions.AllowAny,)
|
||||
serializer_class = serializers.EstablishmentDocumentSerializer
|
||||
ordering = ('id',)
|
||||
|
||||
filter_backends = [
|
||||
filters.CustomSearchFilterBackend,
|
||||
]
|
||||
|
||||
search_fields = (
|
||||
'name',
|
||||
'description',
|
||||
'public_mark',
|
||||
'toque_number',
|
||||
'price_level',
|
||||
)
|
||||
translated_search_fields = (
|
||||
'name',
|
||||
'description',
|
||||
)
|
||||
|
|
|
|||
|
|
@ -65,4 +65,5 @@ ELASTICSEARCH_DSL = {
|
|||
|
||||
ELASTICSEARCH_INDEX_NAMES = {
|
||||
'search_indexes.documents.news': 'local_news',
|
||||
'search_indexes.documents.establishment': 'local_establishment',
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user