-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathreference_repository.rb
More file actions
408 lines (378 loc) · 10.6 KB
/
reference_repository.rb
File metadata and controls
408 lines (378 loc) · 10.6 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
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
# frozen_string_literal: true
class ReferenceRepository < ApplicationRecord
include Indexable
include Elasticsearch::Model
include Hashid::Rails
hashid_config alphabet: "abcdefghijklmnopqrstuvwxyz" \
"1234567890"
before_save :downcase_fields
before_save :force_index
validates_uniqueness_of :re3doi, allow_nil: true, case_sensitive: false
#
# use different index for testing
if Rails.env.test?
index_name "reference_repositories-test#{ENV['TEST_ENV_NUMBER']}"
elsif ENV["ES_PREFIX"].present?
index_name "reference_repositories-#{ENV['ES_PREFIX']}"
else
index_name "reference_repositories"
end
def downcase_fields
self.re3doi.try(:downcase!)
self.client_id.try(:downcase!)
end
def self.create_from_client(client)
if client.re3data_id
ReferenceRepository.find_or_create_by(
re3doi: client.re3data_id
) do |rr|
rr.client_id = client.uid
end
else
ReferenceRepository.find_or_create_by(client_id: client.uid)
end
end
def self.create_from_re3repo(repo)
doi = repo.id&.gsub("https://doi.org/", "")
if not doi.blank?
ReferenceRepository.find_or_create_by(
re3doi: doi
)
end
end
def self.update_from_client(client)
rr = ReferenceRepository.find_or_create_by(client_id: client.uid)
if client.re3data_id && (rr.re3doi.blank?)
rr.re3doi = client.re3data_id
if !rr.valid?
ReferenceRepository.find_by(re3doi: client.re3data_id.downcase, client_id: nil).try(:destroy)
end
rr.save
else
rr.touch
end
end
def self.destroy_from_client(client)
ReferenceRepository.where(client_id: client.uid).destroy_all
end
def self.find_client(client_id)
::Client.where(symbol: client_id).where(deleted_at: nil).first
end
def self.find_re3(doi)
Rails.cache.fetch("re3repo/#{doi}", expires_in: 5.minutes) do
DataCatalog.find_by_id(doi).fetch(:data, []).first
end
end
def uid
hashid
end
def client_repo
if @dsclient&.uid == self[:client_id]
@dsclient
else
@dsclient = ReferenceRepository.find_client(self[:client_id])
end
end
def re3_repo
@re3repo ||= ReferenceRepository.find_re3(self[:re3doi])
end
def as_indexed_json(_options = {})
ReferenceRepositoryDenormalizer.new(self).to_hash
end
settings index: {
number_of_shards: 1,
analysis: {
analyzer: {
string_lowercase: {
tokenizer: "keyword", filter: %w[lowercase ascii_folding]
},
},
normalizer: {
keyword_lowercase: { type: "custom", filter: %w[lowercase] },
},
filter: {
ascii_folding: {
type: "asciifolding", preserve_original: true
},
},
},
} do
mapping dynamic: "false" do
indexes :id
indexes :uid, type: :text,
fields: {
raw: { type: :keyword }
}
indexes :client_id
indexes :re3doi
indexes :re3data_url
indexes :created_at, type: :date, format: :date_optional_time,
fields: {
created_sort: { type: :date }
}
indexes :updated_at, type: :date, format: :date_optional_time,
fields: {
updated_sort: { type: :date }
}
indexes :name
indexes :alternate_name
indexes :description
indexes :pid_system, type: :keyword
indexes :url
indexes :keyword, type: :keyword
indexes :contact
indexes :language, type: :keyword
indexes :certificate, type: :keyword
indexes :data_access, type: :object,
properties: {
type: { type: :keyword },
restrictions: { type: :text }
}
indexes :data_upload, type: :object,
properties: {
type: { type: :keyword },
restrictions: { type: :text }
}
indexes :provider_type, type: :keyword
indexes :repository_type, type: :keyword
indexes :data_upload_licenses, type: :keyword
indexes :software,
type: :text,
fields: {
keyword: { type: "keyword" },
raw: {
type: "text", analyzer: "string_lowercase", "fielddata": true
},
}
indexes :subject, type: :object,
properties: {
text: { type: :keyword },
id: { type: :keyword },
scheme: { type: :keyword }
}
indexes :re3_created_at, type: :date, format: :date_optional_time
indexes :re3_updated_at, type: :date, format: :date_optional_time
indexes :client_created_at, type: :date, format: :date_optional_time
indexes :client_updated_at, type: :date, format: :date_optional_time
indexes :provider_id, type: :keyword
indexes :provider_id_and_name, type: :keyword
indexes :year, type: :integer
end
end
def force_index
__elasticsearch__.instance_variable_set(:@__changed_model_attributes, nil)
end
class << self
def query_aggregations(facet_count: 10)
if facet_count.positive?
{
years: {
date_histogram: {
field: "created_at",
interval: "year",
format: "year",
order: { _key: "desc" },
min_doc_count: 1,
},
aggs: { bucket_truncate: { bucket_sort: { size: facet_count } } },
},
software: {
terms: {
field: "software.keyword",
size: facet_count,
min_doc_count: 1
},
},
repository_types: {
terms: {
field: "repository_type",
size: facet_count,
min_doc_count: 1
},
},
certificates: {
terms: {
field: "certificate",
size: facet_count,
min_doc_count: 1
},
},
providers: {
terms: {
field: "provider_id_and_name",
size: facet_count,
min_doc_count: 1
},
},
}
end
end
def id_fields
%w[
uid^10
client_id
re3doi
]
end
def find_by_id(ids, options = {})
ids = ids.split(",") if ids.is_a?(String)
ids = ids.map { |id| id.gsub("10.17616\/", "") }
options[:page] ||= {}
options[:page][:number] ||= 1
options[:page][:size] ||= 2_000
options[:sort] ||= sort_fields
__elasticsearch__.search(
from: (options.dig(:page, :number) - 1) * options.dig(:page, :size),
size: options.dig(:page, :size),
sort: options[:sort],
track_total_hits: true,
query: {
query_string: {
fields: id_fields,
query: ids.join(" OR ")
}
},
aggregations: query_aggregations,
)
end
def query_fields
%w[
uid^10
name^5
description^5
software
subject.text
_all
]
end
def query(query, options = {})
options[:page] ||= {}
options[:page][:number] ||= 1
options[:page][:size] ||= 25
options[:sort] ||= sort_fields
if !options.dig(:page, :cursor).blank?
__elasticsearch__.search(
{
size: options.dig(:page, :size),
search_after: search_after(options),
sort: options[:sort],
query: es_query(query, options),
aggregations: query_aggregations,
track_total_hits: true,
}.compact,
)
else
from =
((options.dig(:page, :number) || 1) - 1) *
(options.dig(:page, :size) || 25)
__elasticsearch__.search(
{
size: options.dig(:page, :size),
from: from,
sort: options[:sort],
query: es_query(query, options),
aggregations: query_aggregations,
track_total_hits: true,
}.compact,
)
end
end
private
def sort_fields
[
{ _score: { order: "desc" } },
{ "created_at.created_sort": { order: "asc" } },
{ "uid.raw": { order: "asc" } },
]
end
def search_after(options)
if options.dig(:page, :cursor).is_a?(String)
options.dig(:page, :cursor).split(",")
end
end
def must(query)
if query.present?
[{
query_string: {
query: query,
fields: query_fields,
default_operator: "AND",
phrase_slop: 1,
},
}]
else
[{ match_all: {} }]
end
end
def filter(options)
retval = []
if options[:year].present?
retval << { terms: {
"year": options[:year].split(",")
} }
end
if options[:software].present?
retval << { terms: {
"software.raw": options[:software].split(",")
} }
end
if options[:certificate].present?
retval << { terms: {
"certificate": options[:certificate].split(",")
} }
end
if options[:repository_type].present?
retval << { terms: {
"repository_type": options[:repository_type].split(",")
} }
end
if options[:provider_id].present?
retval << { term: {
"provider_id": { value: options[:provider_id], case_insensitive: true }
} }
end
if options[:is_open] == "true"
retval << { term: {
"data_access.type": "open"
} }
end
if options[:is_disciplinary] == "true"
retval << { term: {
"repository_type": "disciplinary"
} }
end
if options[:is_certified] == "true"
retval << { regexp: {
"certificate": ".+"
} }
end
if options[:has_pid] == "true"
retval << { regexp: {
pid_system: "doi|hdl|urn|ark"
} }
end
if options[:subject].present?
retval << { term: {
"subject.text": options[:subject]
} }
end
if options[:subject_id].present?
retval << { regexp: {
"subject.id": options[:subject_id]
} }
end
retval
end
def es_query(query, options)
{
bool: {
must: must(query),
# must_not: must_not,
filter: filter(options),
# should: should,
# minimum_should_match: minimum_should_match
}
}
end
end
end