258 lines
7.2 KiB
Python
258 lines
7.2 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 search_indexes.documents.product import ProductDocument
|
|
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
|
|
|
|
filter_backends = [
|
|
filters.CustomSearchFilterBackend,
|
|
FilteringFilterBackend,
|
|
]
|
|
|
|
search_fields = {
|
|
'title': {'fuzziness': 'auto:2,5',
|
|
'boost': 3},
|
|
'subtitle': {'fuzziness': 'auto:2,5',
|
|
'boost': 2},
|
|
'description': {'fuzziness': 'auto:2,5'},
|
|
}
|
|
translated_search_fields = (
|
|
'title',
|
|
'subtitle',
|
|
'description',
|
|
)
|
|
|
|
filter_fields = {
|
|
'tag': {
|
|
'field': 'tags.id',
|
|
'lookups': [
|
|
constants.LOOKUP_QUERY_IN,
|
|
constants.LOOKUP_QUERY_EXCLUDE
|
|
]
|
|
},
|
|
'tag_value': {
|
|
'field': 'tags.value',
|
|
'lookups': [
|
|
constants.LOOKUP_QUERY_IN,
|
|
constants.LOOKUP_QUERY_EXCLUDE
|
|
]
|
|
},
|
|
'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:2,5',
|
|
'boost': 4},
|
|
'description': {'fuzziness': 'auto:2,5'},
|
|
}
|
|
translated_search_fields = (
|
|
'description',
|
|
)
|
|
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_at_weekday': {
|
|
'field': 'works_at_weekday',
|
|
'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,
|
|
]
|
|
}
|
|
}
|
|
|
|
|
|
class ProductDocumentViewSet(BaseDocumentViewSet):
|
|
"""Product document ViewSet."""
|
|
|
|
document = ProductDocument
|
|
# lookup_field = 'slug'
|
|
pagination_class = ProjectMobilePagination
|
|
permission_classes = (permissions.AllowAny,)
|
|
serializer_class = serializers.ProductDocumentSerializer
|
|
|
|
# def get_queryset(self):
|
|
# qs = super(ProductDocumentViewSet, self).get_queryset()
|
|
# qs = qs.filter('match', is_publish=True)
|
|
# return qs
|
|
|
|
filter_backends = [
|
|
FilteringFilterBackend,
|
|
filters.CustomSearchFilterBackend,
|
|
# GeoSpatialFilteringFilterBackend,
|
|
# DefaultOrderingFilterBackend,
|
|
]
|
|
|
|
search_fields = {
|
|
'name': {'fuzziness': 'auto:2,5',
|
|
'boost': 4},
|
|
'transliterated_name': {'fuzziness': 'auto:2,5',
|
|
'boost': 3},
|
|
'description': {'fuzziness': 'auto:2,5'},
|
|
}
|
|
|
|
translated_search_fields = (
|
|
'description',
|
|
)
|
|
|
|
filter_fields = {
|
|
'slug': 'slug',
|
|
'tags_id': {
|
|
'field': 'tags.id',
|
|
'lookups': [constants.LOOKUP_QUERY_IN]
|
|
},
|
|
'wine_colors_id': {
|
|
'field': 'wine_colors.id',
|
|
'lookups': [
|
|
constants.LOOKUP_QUERY_IN,
|
|
constants.LOOKUP_QUERY_EXCLUDE,
|
|
]
|
|
},
|
|
'wine_from_country_code': {
|
|
'field': 'wine_region.country.code',
|
|
},
|
|
'for_establishment': {
|
|
'field': 'establishment.slug',
|
|
},
|
|
'product_type': {
|
|
'field': 'product_type.index_name',
|
|
},
|
|
'subtype': {
|
|
'field': 'subtypes.index_name',
|
|
'lookups': [
|
|
constants.LOOKUP_QUERY_IN,
|
|
constants.LOOKUP_QUERY_EXCLUDE,
|
|
]
|
|
}
|
|
}
|
|
# geo_spatial_filter_fields = {
|
|
# } |