Elasticsearch: establishment address. Filter by country_id, country_code

This commit is contained in:
evgeniy-st 2019-09-17 17:33:07 +03:00
parent 2abf2c7ffe
commit e67d7767a1
6 changed files with 62 additions and 0 deletions

View File

@ -104,6 +104,11 @@ class Address(models.Model):
def longitude(self):
return self.coordinates.x
@property
def location_field_indexing(self):
return {'lat': self.latitude,
'lon': self.longitude}
# todo: Make recalculate price levels
@receiver(post_save, sender=Country)

View File

@ -21,6 +21,34 @@ class EstablishmentDocument(Document):
'label': fields.ObjectField(attr='label')
},
multi=True)
address = fields.ObjectField(
properties={
'id': fields.IntegerField(),
'street_name_1': fields.TextField(
fields={'raw': fields.KeywordField()}
),
'street_name_2': fields.TextField(
fields={'raw': fields.KeywordField()}
),
'number': fields.IntegerField(),
'location': fields.GeoPointField(attr='location_field_indexing'),
'city': fields.ObjectField(
properties={
'id': fields.IntegerField(),
'name': fields.KeywordField(),
'is_island': fields.BooleanField(),
'country': fields.ObjectField(
properties={
'id': fields.IntegerField(),
'name': fields.ObjectField(attr='name_indexing',
properties=OBJECT_FIELD_PROPERTIES),
'code': fields.KeywordField(),
}
),
}
),
}
)
class Django:

View File

@ -40,6 +40,7 @@ class NewsDocumentSerializer(DocumentSerializer):
return get_translated_value(obj.description)
# todo: country_name_translated
class EstablishmentDocumentSerializer(DocumentSerializer):
"""Establishment document serializer."""
@ -58,6 +59,7 @@ class EstablishmentDocumentSerializer(DocumentSerializer):
'price_level',
'description_translated',
'tags',
'address',
)
@staticmethod

View File

@ -7,6 +7,7 @@ from utils.models import get_current_language
OBJECT_FIELD_PROPERTIES = {
'en-GB': fields.TextField(analyzer='english'),
'ru-RU': fields.TextField(analyzer='russian'),
'fr-FR': fields.TextField(analyzer='french'),
}

View File

@ -78,4 +78,10 @@ class EstablishmentDocumentViewSet(BaseDocumentViewSet):
constants.LOOKUP_QUERY_LTE,
]
},
'country_id': {
'field': 'address.city.country.id'
},
'country': {
'field': 'address.city.country.code'
}
}

View File

@ -55,6 +55,24 @@ def translate_field(self, field_name):
return translate
# todo: refactor this
class IndexJSON:
def __getattr__(self, item):
return None
def index_field(self, field_name):
def index(self):
field = getattr(self, field_name)
obj = IndexJSON()
if isinstance(field, dict):
for key, value in field.items():
setattr(obj, key, value)
return obj
return index
class TranslatedFieldsMixin:
"""Translated Fields mixin"""
@ -69,6 +87,8 @@ class TranslatedFieldsMixin:
if isinstance(field, TJSONField):
setattr(cls, f'{field.name}_translated',
property(translate_field(self, field_name)))
setattr(cls, f'{field_name}_indexing',
property(index_field(self, field_name)))
def __str__(self):
"""Overrided __str__ method."""