181 lines
5.0 KiB
Python
181 lines
5.0 KiB
Python
"""Search indexes app views."""
|
|
from rest_framework import permissions
|
|
from django_elasticsearch_dsl_drf import constants
|
|
from django_elasticsearch_dsl_drf.filter_backends import (
|
|
FilteringFilterBackend,
|
|
GeoSpatialFilteringFilterBackend,
|
|
DefaultOrderingFilterBackend,
|
|
)
|
|
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
|
|
from search_indexes import serializers, filters
|
|
from search_indexes.documents import EstablishmentDocument, NewsDocument
|
|
from utils.pagination import ProjectMobilePagination
|
|
|
|
|
|
class NewsDocumentViewSet(BaseDocumentViewSet):
|
|
"""News document ViewSet."""
|
|
|
|
document = NewsDocument
|
|
lookup_field = 'slug'
|
|
pagination_class = ProjectMobilePagination
|
|
permission_classes = (permissions.AllowAny,)
|
|
serializer_class = serializers.NewsDocumentSerializer
|
|
ordering = ('id',)
|
|
|
|
filter_backends = [
|
|
filters.CustomSearchFilterBackend,
|
|
FilteringFilterBackend,
|
|
]
|
|
|
|
search_fields = {
|
|
'title': {'fuzziness': 'auto:3,4'},
|
|
'subtitle': {'fuzziness': 'auto'},
|
|
'description': {'fuzziness': 'auto'},
|
|
}
|
|
translated_search_fields = (
|
|
'title',
|
|
'subtitle',
|
|
'description',
|
|
)
|
|
|
|
filter_fields = {
|
|
'tag': {
|
|
'field': 'tags.id',
|
|
'lookups': [
|
|
constants.LOOKUP_QUERY_IN,
|
|
]
|
|
},
|
|
'slug': 'slug',
|
|
'country_id': {
|
|
'field': 'country.id'
|
|
},
|
|
'country': {
|
|
'field': 'country.code'
|
|
},
|
|
}
|
|
|
|
|
|
class EstablishmentDocumentViewSet(BaseDocumentViewSet):
|
|
"""Establishment document ViewSet."""
|
|
|
|
document = EstablishmentDocument
|
|
lookup_field = 'slug'
|
|
pagination_class = ProjectMobilePagination
|
|
permission_classes = (permissions.AllowAny,)
|
|
serializer_class = serializers.EstablishmentDocumentSerializer
|
|
|
|
def get_queryset(self):
|
|
qs = super(EstablishmentDocumentViewSet, self).get_queryset()
|
|
qs = qs.filter('match', is_publish=True)
|
|
return qs
|
|
|
|
filter_backends = [
|
|
FilteringFilterBackend,
|
|
filters.CustomSearchFilterBackend,
|
|
GeoSpatialFilteringFilterBackend,
|
|
DefaultOrderingFilterBackend,
|
|
]
|
|
|
|
search_fields = {
|
|
'name': {'fuzziness': 'auto:3,4',
|
|
'boost': '2'},
|
|
'transliterated_name': {'fuzziness': 'auto:3,4',
|
|
'boost': '2'},
|
|
'index_name': {'fuzziness': 'auto:3,4',
|
|
'boost': '2'},
|
|
'description': {'fuzziness': 'auto'},
|
|
}
|
|
translated_search_fields = (
|
|
'description',
|
|
)
|
|
ordering = 'id'
|
|
filter_fields = {
|
|
'slug': 'slug',
|
|
'tag': {
|
|
'field': 'tags.id',
|
|
'lookups': [constants.LOOKUP_QUERY_IN]
|
|
},
|
|
'toque_number': {
|
|
'field': 'toque_number',
|
|
'lookups': [
|
|
constants.LOOKUP_FILTER_RANGE,
|
|
constants.LOOKUP_QUERY_GT,
|
|
constants.LOOKUP_QUERY_GTE,
|
|
constants.LOOKUP_QUERY_LT,
|
|
constants.LOOKUP_QUERY_LTE,
|
|
constants.LOOKUP_QUERY_IN,
|
|
]
|
|
},
|
|
'price_level': {
|
|
'field': 'price_level',
|
|
'lookups': [
|
|
constants.LOOKUP_FILTER_RANGE,
|
|
constants.LOOKUP_QUERY_GT,
|
|
constants.LOOKUP_QUERY_GTE,
|
|
constants.LOOKUP_QUERY_LT,
|
|
constants.LOOKUP_QUERY_LTE,
|
|
constants.LOOKUP_QUERY_IN,
|
|
]
|
|
},
|
|
'country_id': {
|
|
'field': 'address.city.country.id'
|
|
},
|
|
'country': {
|
|
'field': 'address.city.country.code'
|
|
},
|
|
'tags_id': {
|
|
'field': 'tags.id',
|
|
},
|
|
'tags_category_id': {
|
|
'field': 'tags.category.id',
|
|
'lookups': [
|
|
constants.LOOKUP_QUERY_IN,
|
|
],
|
|
},
|
|
'collection_type': {
|
|
'field': 'collections.collection_type'
|
|
},
|
|
'establishment_type': {
|
|
'field': 'establishment_type.id'
|
|
},
|
|
'establishment_subtypes': {
|
|
'field': 'establishment_subtypes.id'
|
|
},
|
|
'type': {
|
|
'field': 'establishment_type.index_name'
|
|
},
|
|
'subtype': {
|
|
'field': 'establishment_subtypes.index_name',
|
|
'lookups': [
|
|
constants.LOOKUP_QUERY_IN,
|
|
],
|
|
},
|
|
'works_noon': {
|
|
'field': 'works_noon',
|
|
'lookups': [
|
|
constants.LOOKUP_QUERY_IN,
|
|
],
|
|
},
|
|
'works_evening': {
|
|
'field': 'works_evening',
|
|
'lookups': [
|
|
constants.LOOKUP_QUERY_IN,
|
|
],
|
|
},
|
|
'works_now': {
|
|
'field': 'works_now',
|
|
'lookups': [
|
|
constants.LOOKUP_FILTER_TERM,
|
|
]
|
|
},
|
|
}
|
|
|
|
geo_spatial_filter_fields = {
|
|
'location': {
|
|
'field': 'address.coordinates',
|
|
'lookups': [
|
|
constants.LOOKUP_FILTER_GEO_BOUNDING_BOX,
|
|
]
|
|
}
|
|
}
|