-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdocuments.py
More file actions
61 lines (49 loc) · 2.12 KB
/
Copy pathdocuments.py
File metadata and controls
61 lines (49 loc) · 2.12 KB
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
from django_elasticsearch_dsl import Document, fields
from django_elasticsearch_dsl.registries import registry
from chat.models import Message
from .models import APIProduct, User
@registry.register_document
class APIProductDocument(Document):
class Index:
# Name of the Elasticsearch index
name = 'api_product'
# See Elasticsearch Indices API reference for available settings
settings = {'number_of_shards': 1,
'number_of_replicas': 0}
class Django:
# The model associated with this Document
model = APIProduct
# The fields of the model you want to be indexed in Elasticsearch
fields = ['name', 'about', 'logo', 'active']
@registry.register_document
class MessageDocument(Document):
topic = fields.ObjectField(properties={
'name': fields.TextField(),
'about': fields.TextField(),
'logo': fields.FileField(),
'active': fields.TextField(),
})
class Index:
# Name of the Elasticsearch index
name = 'chat_message'
# See Elasticsearch Indices API reference for available settings
settings = {'number_of_shards': 1,
'number_of_replicas': 0}
class Django:
# The model associated with this Document
model = Message
# The fields of the model you want to be indexed in Elasticsearch
fields = ['description', ]
related_models = [APIProduct, User]
def get_queryset(self):
"""Not mandatory but to improve performance we can select related in one sql request"""
return super(MessageDocument, self).get_queryset().select_related(
'topic'
)
def get_instances_from_related(self, related_instance):
"""If related_models is set, define how to retrieve the Car instance(s) from the related model.
The related_models option should be used with caution because it can lead in the index
to the updating of a lot of items.
"""
if isinstance(related_instance, APIProduct) or isinstance(related_instance, User):
return related_instance.message_set.all()