-
Notifications
You must be signed in to change notification settings - Fork 16
/
query_filters.py
374 lines (311 loc) · 9.38 KB
/
query_filters.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
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
from flask_login import current_user
from portality.core import app
from portality import models, constants
from copy import deepcopy
# General utilities
###################
def remove_fields(query: dict, fields_to_remove: list):
q = deepcopy(query)
for del_attr in fields_to_remove:
if del_attr in q:
del q[del_attr]
return q
# query sanitisers
##################
def public_query_validator(q):
# no deep paging
if q.from_result() > 10000:
return False
if q.size() > 200:
return False
return True
# query filters
###############
def remove_search_limits(query: dict):
return remove_fields(query, ['size', 'from'])
def only_in_doaj(q):
q.clear_match_all()
q.add_must_filter({"term": {"admin.in_doaj": True}})
return q
def owner(q):
q.clear_match_all()
q.add_must_filter({"term" : {"admin.owner.exact" : current_user.id}})
return q
def update_request(q):
q.clear_match_all()
q.add_must_filter({"range" : {"created_date" : {"gte" : app.config.get("UPDATE_REQUESTS_SHOW_OLDEST")}}})
q.add_must_filter({"term" : {"admin.application_type.exact" : constants.APPLICATION_TYPE_UPDATE_REQUEST}})
return q
def not_update_request(q):
q.clear_match_all()
q.add_must_filter({"term" : {"admin.application_type.exact" : constants.APPLICATION_TYPE_NEW_APPLICATION}})
return q
def associate(q):
q.clear_match_all()
q.add_must_filter({"term" : {"admin.editor.exact" : current_user.id}})
return q
def editor(q):
gnames = []
groups = models.EditorGroup.groups_by_editor(current_user.id)
for g in groups:
gnames.append(g.name)
q.clear_match_all()
q.add_must({"terms" : {"admin.editor_group.exact" : gnames}})
return q
def private_source(q):
q.add_include(["admin.application_status", "admin.ticked",
"admin.seal", "last_updated", "created_date", "id", "bibjson"])
return q
def public_source(q):
q.add_include(["admin.ticked", "admin.seal", "last_updated",
"created_date", "id", "bibjson"])
return q
def strip_facets(q):
q.clear_facets()
return q
def es_type_fix(q):
# FIXME: document this and will need attention for ES 7 upgrade
ctx = q.as_dict()
if "query" not in ctx:
return q
ctx = ctx["query"]
if "filtered" not in ctx:
return q
ctx = ctx["filtered"]
if "filter" not in ctx:
return q
ctx = ctx["filter"]
if "bool" not in ctx:
return q
ctx = ctx["bool"]
if "must" not in ctx:
return q
ctx = ctx["must"]
for m in ctx:
if "term" in m and "_type" in m["term"]:
m["term"] = {"es_type" : m["term"]["_type"]}
return q
def last_update_fallback(q):
s = q.sort()
if s is None or len(s) == 0:
return q
add_created_sort = False
sort_order = None
for sortby in s:
if "last_manual_update" in sortby:
sort_order = sortby["last_manual_update"].get("order")
add_created_sort = True
break
if add_created_sort:
params = {}
if sort_order is not None:
params["order"] = sort_order
s.append({"created_date" : params})
q.set_sort(s)
return q
# ~~WhoCurrentUser:Query~~
def who_current_user(q):
q.clear_match_all()
q.add_must_filter({"term": {"who.exact": current_user.id}})
return q
# results filters
#################
def public_result_filter(results, unpacked=False):
# Dealing with single unpacked result
if unpacked:
if "admin" in results:
for k in list(results["admin"]):
if k not in ["ticked", "seal"]:
del results["admin"][k]
return results
# Dealing with a list of es results
if "hits" not in results:
return results
if "hits" not in results["hits"]:
return results
for hit in results["hits"]["hits"]:
if "_source" in hit:
if "admin" in hit["_source"]:
for k in list(hit["_source"]["admin"]):
if k not in ["ticked", "seal"]:
del hit["_source"]["admin"][k]
return results
def prune_author_emails(results, unpacked=False):
# Dealing with single unpacked ES result
if unpacked:
if "bibjson" in results:
if "author" in results["bibjson"]:
for a in results["bibjson"]["author"]:
if "email" in a:
del a["email"]
return results
# Dealing with a list of ES results
if "hits" not in results:
return results
if "hits" not in results["hits"]:
return results
for hit in results["hits"]["hits"]:
if "_source" in hit:
if "bibjson" in hit["_source"]:
if "author" in hit["_source"]["bibjson"]:
for a in hit["_source"]["bibjson"]["author"]:
if "email" in a:
del a["email"]
return results
def publisher_result_filter(results, unpacked=False):
allowed_admin = ["ticked", "seal", "in_doaj", "related_applications", "current_application", "current_journal", "application_status"]
# Dealing with single unpacked ES result
if unpacked:
if "admin" in results:
for k in list(results["admin"]):
if k not in allowed_admin:
del results["admin"][k]
return results
# Dealing with a list of ES results
if "hits" not in results:
return results
if "hits" not in results["hits"]:
return results
for hit in results["hits"]["hits"]:
if "_source" in hit:
if "admin" in hit["_source"]:
for k in list(hit["_source"]["admin"]):
if k not in allowed_admin:
del hit["_source"]["admin"][k]
return results
def add_fqw_facets(results, unpacked=False):
if unpacked:
return results
facets = {
"index.license.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"bibjson.journal.title.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"bibjson.archiving_policy.policy.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"created_date": {
"_type": "date_histogram",
"entries": []
},
"index.country.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"index.date": {
"_type": "date_histogram",
"entries": []
},
"bibjson.journal.volume.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"index.publisher.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"index.has_seal.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"index.has_apc.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"bibjson.editorial_review.process.exact": {
"_type": "match",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"index.language.exact": {
"_type": "match",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"bibjson.journal.number.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"index.date_toc_fv_month": {
"_type": "date_histogram",
"entries": []
},
"index.classification.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"_type": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
},
"index.issn.exact": {
"_type": "terms",
"missing": 0,
"total": 0,
"other": 0,
"terms": []
}
}
results["aggregations"] = facets
return results
def fqw_back_compat(results, unpacked=False):
if unpacked:
return results
# Dealing with a list of ES results
if "hits" not in results:
return results
if "hits" not in results["hits"]:
return results
for hit in results["hits"]["hits"]:
if hit.get("_source").get("es_type") != "journal":
continue
identifiers = []
bj = hit.get("_source", {}).get("bibjson", {})
if bj.get("pissn"):
identifiers.append({"type" : "pissn", "id" : bj.get("pissn")})
if bj.get("eissn"):
identifiers.append({"type" : "eissn", "id" : bj.get("eissn")})
bj["identifier"] = identifiers
return results