Tags indexing and search (by id)

This commit is contained in:
evgeniy-st 2019-09-10 12:58:39 +03:00
parent 9fab4454a0
commit c58d3bae36
5 changed files with 21 additions and 0 deletions

View File

@ -178,6 +178,11 @@ class Establishment(ProjectBaseMixin, ImageMixin, TranslatedFieldsMixin):
def best_price_carte(self):
return 200
@property
def tags_indexing(self):
return [{'id': tag.metadata.id,
'label': tag.metadata.label} for tag in self.tags.all()]
class Position(BaseAttributes, TranslatedFieldsMixin):
"""Position model."""

View File

@ -2,6 +2,7 @@ from search_indexes.documents.establishment import EstablishmentDocument
from search_indexes.documents.news import NewsDocument
# todo: make signal to update documents on related fields
__all__ = [
'EstablishmentDocument',
'NewsDocument',

View File

@ -16,6 +16,12 @@ class EstablishmentDocument(Document):
name = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES)
description = fields.ObjectField(properties=OBJECT_FIELD_PROPERTIES)
tags = fields.ObjectField(
properties={
'id': fields.IntegerField(attr='id'),
'label': fields.ObjectField(attr='label')
},
multi=True)
class Django:
@ -32,3 +38,6 @@ class EstablishmentDocument(Document):
def prepare_description(self, instance):
return instance.description
def prepare_tags(self, instance):
return instance.tags_indexing

View File

@ -59,6 +59,7 @@ class EstablishmentDocumentSerializer(DocumentSerializer):
'price_level',
'name_translated',
'description_translated',
'tags',
)
@staticmethod

View File

@ -1,5 +1,6 @@
"""Search indexes app views."""
from rest_framework import permissions
from django_elasticsearch_dsl_drf.filter_backends import FilteringFilterBackend
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
from django_elasticsearch_dsl_drf.pagination import PageNumberPagination
from search_indexes import serializers, filters
@ -43,6 +44,7 @@ class EstablishmentDocumentViewSet(BaseDocumentViewSet):
ordering = ('id',)
filter_backends = [
FilteringFilterBackend,
filters.CustomSearchFilterBackend,
]
@ -57,3 +59,6 @@ class EstablishmentDocumentViewSet(BaseDocumentViewSet):
'name',
'description',
)
filter_fields = {
'tag': 'tags.id'
}