-
Notifications
You must be signed in to change notification settings - Fork 32
/
documents.py
97 lines (83 loc) · 2.7 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import json
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from pydash import get
from core.common.utils import jsonify_safe, flatten_dict
from core.url_registry.models import URLRegistry
@registry.register_document
class URLRegistryDocument(Document):
class Index:
name = 'url_registries'
settings = {'number_of_shards': 1, 'number_of_replicas': 0}
namespace = fields.TextField(attr='namespace')
url = fields.TextField(attr='url')
_url = fields.KeywordField(attr='url')
name = fields.TextField(attr='name')
_name = fields.KeywordField(attr='name', normalizer='lowercase')
extras = fields.ObjectField(dynamic=True)
last_update = fields.DateField(attr='updated_at')
updated_by = fields.KeywordField(attr='updated_by.username')
owner = fields.KeywordField(attr='owner.mnemonic', normalizer='lowercase')
owner_type = fields.KeywordField(attr='owner_type')
owner_url = fields.KeywordField(attr='owner_url')
repo = fields.TextField()
repo_owner_type = fields.KeywordField(attr='repo.parent_resource_type')
repo_owner = fields.KeywordField(attr='repo.parent_resource', normalizer='lowercase')
class Django:
model = URLRegistry
fields = ['is_active']
@staticmethod
def get_match_phrase_attrs():
return ['_url', '_name', 'namespace', 'repo', 'repo_owner']
@staticmethod
def get_exact_match_attrs():
return {
'url': {
'boost': 4,
},
'namespace': {
'boost': 3.5,
},
'name': {
'boost': 3,
}
}
@staticmethod
def get_wildcard_search_attrs():
return {
'url': {
'boost': 1,
'wildcard': True,
'lower': True
},
'namespace': {
'boost': 0.8,
'wildcard': True,
'lower': True
},
'name': {
'boost': 0.6,
'wildcard': True,
'lower': True
}
}
@staticmethod
def get_fuzzy_search_attrs():
return {
'namespace': {
'boost': 0.8,
}
}
@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))
return value or {}
@staticmethod
def prepare_repo(instance):
return get(instance, 'repo.mnemonic')