-
Notifications
You must be signed in to change notification settings - Fork 14
/
migration_038.sql
293 lines (283 loc) · 12.2 KB
/
migration_038.sql
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
CREATE FUNCTION media_to_text(media full_link) RETURNS text
LANGUAGE sql STABLE
AS $_$
SELECT media.title || ' ' || media.attribution;
$_$;
CREATE FUNCTION media_to_text(media full_file) RETURNS text
LANGUAGE sql STABLE
AS $_$
SELECT media.title || ' ' || media.attribution;
$_$;
CREATE FUNCTION medialist_to_text(list full_file[]) RETURNS text
LANGUAGE sql STABLE
AS $_$
SELECT COALESCE(array_to_string(
array_agg(media_to_text(medium)),
' '), '')
from unnest(list) as medium
;
$_$;
CREATE FUNCTION medialist_to_text(list full_link[]) RETURNS text
LANGUAGE sql STABLE
AS $_$
SELECT COALESCE(array_to_string(
array_agg(media_to_text(medium)),
' '), '')
from unnest(list) as medium
;
$_$;
CREATE FUNCTION value_key_to_text(lang text, field text, key text) RETURNS text
LANGUAGE sql STABLE
AS $_$
SELECT COALESCE(keyvalues ->> quote_ident(format('name:%s-key:%s', field, key)), '')
FROM localizations
WHERE language = lang;
$_$;
CREATE FUNCTION keylist_to_text(lang text, field text, keys text[]) RETURNS text
LANGUAGE sql STABLE
AS $_$
SELECT COALESCE(array_to_string(
array_agg(value_key_to_text(lang, field, key)),
' '), '')
from unnest(keys) as key
;
$_$;
-- FIXME: Split the materialized view into smaller functions like the below
-- CREATE FUNCTION get_all_authors() RETURNS TABLE(thingid int, authorstring text)
-- LANGUAGE sql STABLE
-- AS $_$
-- SELECT
-- things.id thingid,
-- string_agg(users.name, ' ') authorstring
-- FROM users, authors, things
-- WHERE
-- authors.user_id = users.id AND
-- authors.thingid = things.id
-- GROUP BY things.id
-- $_$;
--
-- CREATE FUNCTION get_most_recent_texts_en() RETURNS TABLE(thingid int, title text, body text, description text)
-- LANGUAGE sql STABLE
-- AS $_$
-- SELECT DISTINCT ON(thingid) thingid, title, body, description
-- FROM (
-- SELECT DISTINCT ON(thingid, timestamp) thingid, title, body, description
-- FROM localized_texts
-- WHERE language = 'en'
-- ORDER BY thingid, timestamp, DESC
-- ) AS titles
-- ORDER BY thingid;
-- $_$;
CREATE MATERIALIZED VIEW search_index_en AS
WITH allauthors AS (
SELECT
things.id thingid,
string_agg(users.name, ' ') authorstring
FROM users, authors, things
WHERE
authors.user_id = users.id AND
authors.thingid = things.id
GROUP BY things.id
), texts AS (
select distinct on(thingid) thingid, title, body, description
from (
select distinct on(thingid, timestamp) thingid, title, body, description
from localized_texts
where language = 'en'
order by thingid, timestamp DESC
) as titles
order by thingid
)
SELECT
cases.id,
cases.type,
texts.title,
texts.body,
texts.description,
setweight(to_tsvector('english'::regconfig, texts.title), 'A') ||
setweight(to_tsvector('english'::regconfig, COALESCE(texts.body, '')), 'D') ||
setweight(to_tsvector('english'::regconfig, COALESCE(texts.description, '')), 'C') ||
setweight(to_tsvector('english'::regconfig, keylist_to_text('en', 'tags', cases.tags)), 'A') ||
setweight(to_tsvector('english'::regconfig, allauthors.authorstring), 'A') ||
setweight(to_tsvector('english'::regconfig, COALESCE(cases.city, '')), 'A') ||
setweight(to_tsvector('english'::regconfig, COALESCE(cases.country, '')), 'A') ||
setweight(to_tsvector('english'::regconfig,
COALESCE(cases.funder, '')), 'D') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'general_issues', cases.general_issues)), 'B') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'specific_topics', cases.specific_topics)), 'B') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'purposes', cases.purposes)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'approaches', cases.approaches)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'targeted_participants', cases.targeted_participants)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'method_types', cases.method_types)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'participants_interactions', cases.participants_interactions)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'learning_resources', cases.learning_resources)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'decision_methods', cases.decision_methods)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'if_voting', cases.if_voting)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'insights_outcomes', cases.insights_outcomes)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'organizer_types', cases.organizer_types)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'funder_types', cases.funder_types)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'change_types', cases.change_types)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'implementers_of_change', cases.implementers_of_change)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'tools_techniques_types', cases.tools_techniques_types)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'scope_of_influence', cases.scope_of_influence)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'public_spectrum', cases.public_spectrum)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'legality', cases.legality)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'facilitators', cases.facilitators)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'facilitator_training', cases.facilitator_training)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'facetoface_online_or_both', cases.facetoface_online_or_both)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'open_limited', cases.open_limited)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'recruitment_method', cases.recruitment_method)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'time_limited', cases.time_limited)), 'C') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(cases.links)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(cases.videos)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(cases.audio)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(cases.evaluation_links)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(cases.photos)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(cases.files)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(cases.evaluation_reports)), 'D')
AS document
FROM
cases
JOIN texts ON texts.thingid = cases.id
JOIN allauthors ON allauthors.thingid = cases.id
WHERE
cases.hidden = false
UNION
SELECT
methods.id,
methods.type,
texts.title,
texts.body,
texts.description,
setweight(to_tsvector('english'::regconfig, texts.title), 'A') ||
setweight(to_tsvector('english'::regconfig, COALESCE(texts.body, '')), 'D') ||
setweight(to_tsvector('english'::regconfig, COALESCE(texts.description, '')), 'C') ||
setweight(to_tsvector('english'::regconfig, allauthors.authorstring), 'A') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'method_types', methods.method_types)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'scope_of_influence', methods.scope_of_influence)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'participants_interactions', methods.participants_interactions)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'number_of_participants', methods.number_of_participants)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'decision_methods', methods.decision_methods)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'if_voting', methods.if_voting)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'facetoface_online_or_both', methods.facetoface_online_or_both)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'public_spectrum', methods.public_spectrum)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'open_limited', methods.open_limited)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'recruitment_method', methods.recruitment_method)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'level_polarization', methods.level_polarization)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'level_complexity', methods.level_polarization)), 'C') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(methods.links)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(methods.videos)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(methods.audio)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(methods.photos)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(methods.files)), 'D')
AS document
FROM
methods
JOIN texts ON texts.thingid = methods.id
JOIN allauthors ON allauthors.thingid = methods.id
WHERE
methods.hidden = false
UNION
SELECT
organizations.id,
organizations.type,
texts.title,
texts.body,
texts.description,
setweight(to_tsvector('english'::regconfig, texts.title), 'A') ||
setweight(to_tsvector('english'::regconfig, texts.body), 'D') ||
setweight(to_tsvector('english'::regconfig, texts.description), 'C') ||
setweight(to_tsvector('english'::regconfig, allauthors.authorstring), 'A') ||
setweight(to_tsvector('english'::regconfig, COALESCE(organizations.city, '')), 'A') ||
setweight(to_tsvector('english'::regconfig, COALESCE(organizations.country, '')), 'A') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'scope_of_influence', organizations.scope_of_influence)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'type_method', organizations.type_method)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'type_tool', organizations.type_tool)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'specific_topics', organizations.specific_topics)), 'C') ||
setweight(to_tsvector('english'::regconfig,
keylist_to_text('en', 'general_issues', organizations.general_issues)), 'C') ||
setweight(to_tsvector('english'::regconfig,
value_key_to_text('en', 'sector', organizations.sector)), 'C') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(organizations.links)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(organizations.videos)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(organizations.audio)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(organizations.photos)), 'D') ||
setweight(to_tsvector('english'::regconfig,
medialist_to_text(organizations.files)), 'D')
AS document
FROM
organizations
JOIN texts ON texts.thingid = organizations.id
JOIN allauthors ON allauthors.thingid = organizations.id
WHERE
organizations.hidden = false
;
CREATE INDEX idx_fts_search_en ON search_index_en USING gin(document);
-- CREATE FUNCTION update_search() RETURNS trigger
-- LANGUAGE sql STABLE
-- AS $_$
-- REFRESH MATERIALIZED VIEW search_index_en;
-- $_$;
--
-- create trigger search_insert after insert on things
-- for each statement execute procedure update_search();
--
-- create trigger search_update after update on things
-- for each statement execute procedure update_search();