-
Notifications
You must be signed in to change notification settings - Fork 32
/
documents.py
45 lines (35 loc) · 1.31 KB
/
documents.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import json
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from core.common.utils import jsonify_safe, flatten_dict
from core.orgs.models import Organization
@registry.register_document
class OrganizationDocument(Document):
class Index:
name = 'organizations'
settings = {'number_of_shards': 1, 'number_of_replicas': 0}
last_update = fields.DateField(attr='updated_at')
public_can_view = fields.BooleanField(attr='public_can_view')
name = fields.KeywordField(attr='name', normalizer="lowercase")
mnemonic = fields.KeywordField(attr='mnemonic', normalizer="lowercase")
extras = fields.ObjectField(dynamic=True)
class Django:
model = Organization
fields = [
'is_active',
'company',
'location',
]
@staticmethod
def get_boostable_search_attrs():
return dict(mnemonic=dict(boost=5), name=dict(boost=4))
@staticmethod
def prepare_extras(instance):
value = {}
if instance.extras:
value = jsonify_safe(instance.extras)
if isinstance(value, dict):
value = flatten_dict(value)
if value:
value = json.loads(json.dumps(value).replace('-', '_'))
return value or {}