update elasticsearch index
This commit is contained in:
parent
750f141017
commit
729a77866d
|
|
@ -11,6 +11,7 @@ from django.db import models
|
||||||
from django.db.models import When, Case, F, ExpressionWrapper, Subquery, Q
|
from django.db.models import When, Case, F, ExpressionWrapper, Subquery, Q
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
from elasticsearch_dsl import Q
|
||||||
from phonenumber_field.modelfields import PhoneNumberField
|
from phonenumber_field.modelfields import PhoneNumberField
|
||||||
|
|
||||||
from collection.models import Collection
|
from collection.models import Collection
|
||||||
|
|
@ -98,6 +99,16 @@ class EstablishmentQuerySet(models.QuerySet):
|
||||||
else:
|
else:
|
||||||
return self.none()
|
return self.none()
|
||||||
|
|
||||||
|
def es_search(self, value, locale=None):
|
||||||
|
"""Search text via ElasticSearch."""
|
||||||
|
from search_indexes.documents import EstablishmentDocument
|
||||||
|
search = EstablishmentDocument.search().filter(
|
||||||
|
Q('match', name=value) |
|
||||||
|
Q('match', **{f'description.{locale}': value})
|
||||||
|
).execute()
|
||||||
|
ids = [result.meta.id for result in search]
|
||||||
|
return self.filter(id__in=ids)
|
||||||
|
|
||||||
def by_country_code(self, code):
|
def by_country_code(self, code):
|
||||||
"""Return establishments by country code"""
|
"""Return establishments by country code"""
|
||||||
return self.filter(address__city__country__code=code)
|
return self.filter(address__city__country__code=code)
|
||||||
|
|
@ -330,7 +341,6 @@ class Establishment(ProjectBaseMixin, URLImageMixin, TranslatedFieldsMixin):
|
||||||
raise ValidationError('Establishment type of subtype does not match')
|
raise ValidationError('Establishment type of subtype does not match')
|
||||||
self.establishment_subtypes.add(establishment_subtype)
|
self.establishment_subtypes.add(establishment_subtype)
|
||||||
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def vintage_year(self):
|
def vintage_year(self):
|
||||||
last_review = self.reviews.by_status(Review.READY).last()
|
last_review = self.reviews.by_status(Review.READY).last()
|
||||||
|
|
|
||||||
|
|
@ -14,6 +14,7 @@ EstablishmentIndex.settings(number_of_shards=1, number_of_replicas=1)
|
||||||
class EstablishmentDocument(Document):
|
class EstablishmentDocument(Document):
|
||||||
"""Establishment document."""
|
"""Establishment document."""
|
||||||
|
|
||||||
|
preview_image = fields.KeywordField(attr='preview_image_url')
|
||||||
description = fields.ObjectField(attr='description_indexing',
|
description = fields.ObjectField(attr='description_indexing',
|
||||||
properties=OBJECT_FIELD_PROPERTIES)
|
properties=OBJECT_FIELD_PROPERTIES)
|
||||||
establishment_type = fields.ObjectField(
|
establishment_type = fields.ObjectField(
|
||||||
|
|
@ -50,7 +51,8 @@ class EstablishmentDocument(Document):
|
||||||
fields={'raw': fields.KeywordField()}
|
fields={'raw': fields.KeywordField()}
|
||||||
),
|
),
|
||||||
'number': fields.IntegerField(),
|
'number': fields.IntegerField(),
|
||||||
'location': fields.GeoPointField(attr='location_field_indexing'),
|
'coordinates': fields.GeoPointField(attr='location_field_indexing'),
|
||||||
|
# todo: remove if not used
|
||||||
'city': fields.ObjectField(
|
'city': fields.ObjectField(
|
||||||
properties={
|
properties={
|
||||||
'id': fields.IntegerField(),
|
'id': fields.IntegerField(),
|
||||||
|
|
@ -82,9 +84,10 @@ class EstablishmentDocument(Document):
|
||||||
fields = (
|
fields = (
|
||||||
'id',
|
'id',
|
||||||
'name',
|
'name',
|
||||||
'toque_number',
|
'name_translated',
|
||||||
'price_level',
|
'price_level',
|
||||||
'preview_image_url',
|
'toque_number',
|
||||||
|
'public_mark',
|
||||||
'slug',
|
'slug',
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -40,13 +40,36 @@ class NewsDocumentSerializer(DocumentSerializer):
|
||||||
return get_translated_value(obj.description)
|
return get_translated_value(obj.description)
|
||||||
|
|
||||||
|
|
||||||
# todo: country_name_translated
|
class TagsDocumentSerializer(serializers.Serializer):
|
||||||
|
"""Tags serializer for ES Document."""
|
||||||
|
|
||||||
|
id = serializers.IntegerField()
|
||||||
|
label_translated = serializers.SerializerMethodField()
|
||||||
|
|
||||||
|
def get_label_translated(self, obj):
|
||||||
|
return get_translated_value(obj.label)
|
||||||
|
|
||||||
|
|
||||||
|
class AddressDocumentSerializer(serializers.Serializer):
|
||||||
|
"""Address serializer for ES Document."""
|
||||||
|
|
||||||
|
id = serializers.IntegerField()
|
||||||
|
street_name_1 = serializers.CharField()
|
||||||
|
street_name_2 = serializers.CharField()
|
||||||
|
number = serializers.IntegerField()
|
||||||
|
postal_code = serializers.CharField()
|
||||||
|
latitude = serializers.FloatField(allow_null=True, source='coordinates.lat')
|
||||||
|
longitude = serializers.FloatField(allow_null=True, source='coordinates.lon')
|
||||||
|
geo_lon = serializers.FloatField(allow_null=True, source='coordinates.lon')
|
||||||
|
geo_lat = serializers.FloatField(allow_null=True, source='coordinates.lat')
|
||||||
|
|
||||||
|
|
||||||
class EstablishmentDocumentSerializer(DocumentSerializer):
|
class EstablishmentDocumentSerializer(DocumentSerializer):
|
||||||
"""Establishment document serializer."""
|
"""Establishment document serializer."""
|
||||||
|
|
||||||
description_translated = serializers.SerializerMethodField(allow_null=True)
|
address = AddressDocumentSerializer()
|
||||||
|
tags = TagsDocumentSerializer(many=True)
|
||||||
|
|
||||||
preview_image = serializers.URLField(source='preview_image_url')
|
|
||||||
class Meta:
|
class Meta:
|
||||||
"""Meta class."""
|
"""Meta class."""
|
||||||
|
|
||||||
|
|
@ -54,43 +77,40 @@ class EstablishmentDocumentSerializer(DocumentSerializer):
|
||||||
fields = (
|
fields = (
|
||||||
'id',
|
'id',
|
||||||
'name',
|
'name',
|
||||||
'public_mark',
|
'name_translated',
|
||||||
'toque_number',
|
|
||||||
'price_level',
|
'price_level',
|
||||||
'description_translated',
|
'toque_number',
|
||||||
'tags',
|
'public_mark',
|
||||||
'address',
|
|
||||||
'collections',
|
|
||||||
'establishment_type',
|
|
||||||
'establishment_subtypes',
|
|
||||||
'preview_image',
|
|
||||||
'slug',
|
'slug',
|
||||||
|
'preview_image',
|
||||||
|
'address',
|
||||||
|
'tags',
|
||||||
|
# 'collections',
|
||||||
|
# 'establishment_type',
|
||||||
|
# 'establishment_subtypes',
|
||||||
)
|
)
|
||||||
|
|
||||||
@staticmethod
|
|
||||||
def get_description_translated(obj):
|
|
||||||
return get_translated_value(obj.description)
|
|
||||||
|
|
||||||
def to_representation(self, instance):
|
# def to_representation(self, instance):
|
||||||
ret = super().to_representation(instance)
|
# ret = super().to_representation(instance)
|
||||||
dict_merge = lambda a, b: a.update(b) or a
|
# dict_merge = lambda a, b: a.update(b) or a
|
||||||
|
#
|
||||||
ret['tags'] = map(lambda tag: dict_merge(tag, {'label_translated': get_translated_value(tag.pop('label'))}),
|
# ret['tags'] = map(lambda tag: dict_merge(tag, {'label_translated': get_translated_value(tag.pop('label'))}),
|
||||||
ret['tags'])
|
# ret['tags'])
|
||||||
ret['establishment_subtypes'] = map(
|
# ret['establishment_subtypes'] = map(
|
||||||
lambda subtype: dict_merge(subtype, {'name_translated': get_translated_value(subtype.pop('name'))}),
|
# lambda subtype: dict_merge(subtype, {'name_translated': get_translated_value(subtype.pop('name'))}),
|
||||||
ret['establishment_subtypes'])
|
# ret['establishment_subtypes'])
|
||||||
if ret.get('establishment_type'):
|
# if ret.get('establishment_type'):
|
||||||
ret['establishment_type']['name_translated'] = get_translated_value(ret['establishment_type'].pop('name'))
|
# ret['establishment_type']['name_translated'] = get_translated_value(ret['establishment_type'].pop('name'))
|
||||||
if ret.get('address'):
|
# if ret.get('address'):
|
||||||
ret['address']['city']['country']['name_translated'] = get_translated_value(
|
# ret['address']['city']['country']['name_translated'] = get_translated_value(
|
||||||
ret['address']['city']['country'].pop('name'))
|
# ret['address']['city']['country'].pop('name'))
|
||||||
location = ret['address'].pop('location')
|
# location = ret['address'].pop('location')
|
||||||
if location:
|
# if location:
|
||||||
ret['address']['geo_lon'] = location['lon']
|
# ret['address']['geo_lon'] = location['lon']
|
||||||
ret['address']['geo_lat'] = location['lat']
|
# ret['address']['geo_lat'] = location['lat']
|
||||||
|
#
|
||||||
ret['type'] = ret.pop('establishment_type')
|
# ret['type'] = ret.pop('establishment_type')
|
||||||
ret['subtypes'] = ret.pop('establishment_subtypes')
|
# ret['subtypes'] = ret.pop('establishment_subtypes')
|
||||||
|
#
|
||||||
return ret
|
# return ret
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
"""Search indexes app views."""
|
"""Search indexes app views."""
|
||||||
from rest_framework import permissions
|
from rest_framework import permissions
|
||||||
from django_elasticsearch_dsl_drf import constants
|
from django_elasticsearch_dsl_drf import constants
|
||||||
from django_elasticsearch_dsl_drf.filter_backends import (FilteringFilterBackend,
|
from django_elasticsearch_dsl_drf.filter_backends import (
|
||||||
GeoSpatialFilteringFilterBackend)
|
FilteringFilterBackend,
|
||||||
|
GeoSpatialFilteringFilterBackend
|
||||||
|
)
|
||||||
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
|
from django_elasticsearch_dsl_drf.viewsets import BaseDocumentViewSet
|
||||||
|
|
||||||
from utils.pagination import ProjectPageNumberPagination
|
from utils.pagination import ProjectPageNumberPagination
|
||||||
|
|
@ -108,7 +110,7 @@ class EstablishmentDocumentViewSet(BaseDocumentViewSet):
|
||||||
|
|
||||||
geo_spatial_filter_fields = {
|
geo_spatial_filter_fields = {
|
||||||
'location': {
|
'location': {
|
||||||
'field': 'address.location',
|
'field': 'address.coordinates',
|
||||||
'lookups': [
|
'lookups': [
|
||||||
constants.LOOKUP_FILTER_GEO_BOUNDING_BOX,
|
constants.LOOKUP_FILTER_GEO_BOUNDING_BOX,
|
||||||
]
|
]
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user