-
Notifications
You must be signed in to change notification settings - Fork 136
Expand file tree
/
Copy pathbook.py
More file actions
177 lines (145 loc) · 4.79 KB
/
book.py
File metadata and controls
177 lines (145 loc) · 4.79 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from django.conf import settings
from django_elasticsearch_dsl import Document, Index, fields
from django_elasticsearch_dsl_drf.compat import KeywordField, StringField
from django_elasticsearch_dsl_drf.analyzers import edge_ngram_completion
from django_elasticsearch_dsl_drf.versions import ELASTICSEARCH_GTE_5_0
from books.models import Book
from .analyzers import html_strip
__all__ = ('BookDocument',)
INDEX = Index(settings.ELASTICSEARCH_INDEX_NAMES[__name__])
# See Elasticsearch Indices API reference for available settings
INDEX.settings(
number_of_shards=1,
number_of_replicas=1,
blocks={'read_only_allow_delete': None},
# read_only_allow_delete=False
)
@INDEX.doc_type
class BookDocument(Document):
"""Book Elasticsearch document."""
# In different parts of the code different fields are used. There are
# a couple of use cases: (1) more-like-this functionality, where `title`,
# `description` and `summary` fields are used, (2) search and filtering
# functionality where all of the fields are used.
# ID
id = fields.IntegerField(attr='id')
# ********************************************************************
# *********************** Main data fields for search ****************
# ********************************************************************
__title_fields = {
'raw': KeywordField(),
'suggest': fields.CompletionField(),
'edge_ngram_completion': StringField(
analyzer=edge_ngram_completion
),
'mlt': StringField(analyzer='english'),
}
if ELASTICSEARCH_GTE_5_0:
__title_fields.update(
{
'suggest_context': fields.CompletionField(
contexts=[
{
"name": "tag",
"type": "category",
"path": "tags.raw",
},
{
"name": "state",
"type": "category",
"path": "state.raw",
},
{
"name": "publisher",
"type": "category",
"path": "publisher.raw",
},
]
),
}
)
title = StringField(
analyzer=html_strip,
fields=__title_fields
)
description = StringField(
analyzer=html_strip,
fields={
'raw': KeywordField(),
'mlt': StringField(analyzer='english'),
}
)
summary = StringField(
analyzer=html_strip,
fields={
'raw': KeywordField(),
'mlt': StringField(analyzer='english'),
}
)
# ********************************************************************
# ********** Additional fields for search and filtering **************
# ********************************************************************
authors = fields.ListField(
StringField(
analyzer=html_strip,
fields={
'raw': KeywordField(),
}
)
)
# Publisher
publisher = StringField(
attr='publisher_indexing',
analyzer=html_strip,
fields={
'raw': KeywordField(),
'suggest': fields.CompletionField(),
}
)
# Publication date
publication_date = fields.DateField()
# State
state = StringField(
analyzer=html_strip,
fields={
'raw': KeywordField(),
}
)
# ISBN
isbn = StringField(
analyzer=html_strip,
fields={
'raw': KeywordField(),
}
)
# Price
price = fields.FloatField()
# Pages
pages = fields.IntegerField()
# Stock count
stock_count = fields.IntegerField()
# Tags
tags = StringField(
attr='tags_indexing',
analyzer=html_strip,
fields={
'raw': KeywordField(multi=True),
'suggest': fields.CompletionField(multi=True),
},
multi=True
)
# Date created
created = fields.DateField(attr='created_indexing')
null_field = StringField(attr='null_field_indexing')
class Django(object):
model = Book # The model associate with this Document
class Meta:
parallel_indexing = True
# queryset_pagination = 50 # This will split the queryset
# # into parts while indexing
def prepare_summary(self, instance):
"""Prepare summary."""
return instance.summary[:32766] if instance.summary else None
def prepare_authors(self, instance):
"""Prepare authors."""
return [author.name for author in instance.authors.all()]