···11-with
22- releases as (
33- select *, rel_score: mbz.fts_release ($1, $2)
44- from mbz.release
55- where
66- (
77- rel_score is not null
88- and rel_score > 5
99- and $2 IS NULL
1010- )
1111- or (
1212- $2 IS NOT NULL
1313- and release.gid = $2
1414- )
1515- ),
1616- recordings as (
1717- select *, rec_score: mbz.fts_recording ($3, $4)
1818- from mbz.recording
1919- where
2020- (
2121- rec_score is not null
2222- and rec_score > 5
2323- and $4 IS NULL
2424- )
2525- or (
2626- $4 IS NOT NULL
2727- and recording.gid = $4
2828- )
2929- ),
3030- artists as (
3131- select *, artist_score: mbz.fts_artists ($5, coalesce($2, $4)),
3232- from mbz.artist_credit
3333- where
3434- artist_score is not null
3535- and artist_score > 2.5
3636- )
3737-select
3838- track_name: track.name,
3939- track_gid: track.gid,
4040- release_gid: releases.gid,
4141- release_name: releases.name,
4242- recording_gid: recordings.gid,
4343- release_grp_gid: release_group.gid,
4444- release_grp_name: release_group.name,
4545- artists: artists.name,
4646- rel_score,
4747- rec_score,
4848- artist_score: coalesce(artist_score, 0)
4949-from mbz.track
5050- inner join recordings on recordings.id = track.recording
5151- inner join mbz.medium on medium.id = track.medium
5252- inner join releases on releases.id = medium.release
5353- inner join mbz.release_group on release_group.id = releases.release_group
5454- left join artists on artists.id = recordings.artist_credit
5555-where is_data_track = false
5656-order by
5757- rel_score desc,
5858- rec_score desc,
5959- artist_score desc
11+with releases as (select "release".*, rank
22+ from fts_release
33+ left join "release" on id = fts_release.ROWID
44+ where fts_release.name match ?1
55+ and ?2 IS NULL
66+ union
77+ select *, 0 as rank
88+ from "release"
99+ where gid = ?2
1010+ and ?2 IS NOT NULL
1111+ order by rank),
1212+ recordings as (select recording.*, rank
1313+ from fts_recording
1414+ left join recording on id = fts_recording.ROWID
1515+ where fts_recording.name match ?3
1616+ and ?4 IS NULL
1717+ union
1818+ select *, 0 as rank
1919+ from recording
2020+ where gid = ?4
2121+ and ?4 IS NOT NULL
2222+ order by rank),
2323+ artists as (select artist_credit.*, rank
2424+ from fts_artist_credit
2525+ inner join artist_credit on artist_credit.id = fts_artist_credit.ROWID
2626+ where fts_artist_credit.name match ?5
2727+ and ?2 IS NULL
2828+ and ?4 IS NULL
2929+ order by rank)
3030+select track.name,
3131+ track.gid,
3232+ releases.gid,
3333+ releases.name,
3434+ recordings.gid,
3535+ release_group.gid,
3636+ release_group.name,
3737+ artists.name,
3838+ coalesce(releases.rank,0),
3939+ coalesce(recordings.rank,0),
4040+ coalesce(artists.rank,0)
4141+from track
4242+ inner join recordings on recordings.id = track.recording
4343+ inner join medium on medium.id = track.medium
4444+ inner join releases on releases.id = medium.release
4545+ inner join release_group on release_group.id = releases.release_group
4646+ left join artists on artists.id = recordings.artist_credit
4747+order by releases.rank, recordings.rank, artists.rank
6048limit 10;
+31-41
src/mbz/find_by_isrc.sql
···11-with
22- releases as (
33- select *, rel_score: mbz.fts_release ($1, $2)
44- from mbz.release
55- where
66- (
77- rel_score is not null
88- and rel_score > 2
99- and $2 IS NULL
1010- )
1111- or (
1212- $2 IS NOT NULL
1313- and release.gid = $2
1414- )
1515- )
1616-select
1717- track_name: track.name,
1818- track_gid: track.gid,
1919- release_gid: releases.gid,
2020- release_name: releases.name,
2121- recording_gid: recording.gid,
2222- release_grp_gid: release_group.gid,
2323- release_grp_name: release_group.name,
2424- artists: artist_credit.name,
2525- rel_score,
2626- rec_score: 10,
2727- artist_score: 10
2828-from mbz.track
2929- inner join mbz.isrc on isrc.recording = track.recording
3030- inner join mbz.recording on recording.id = isrc.recording
3131- inner join mbz.medium on medium.id = track.medium
3232- inner join releases on releases.id = medium.release
3333- inner join mbz.release_group on release_group.id = releases.release_group
3434- left join mbz.artist_credit on artist_credit.id = recording.artist_credit
3535-where
3636- is_data_track = false
3737- and isrc.isrc = $3
3838-order by
3939- rel_score desc,
4040- rec_score desc,
4141- artist_score desc
11+with releases as (select "release".*, rank
22+ from fts_release
33+ left join "release" on id = fts_release.ROWID
44+ where fts_release.name match ?1
55+ and ?2 IS NULL
66+ union
77+ select *, 0 as rank
88+ from "release"
99+ where gid = ?2
1010+ and ?2 IS NOT NULL
1111+ order by rank)
1212+select track.name,
1313+ track.gid,
1414+ releases.gid,
1515+ releases.name,
1616+ recording.gid,
1717+ release_group.gid,
1818+ release_group.name,
1919+ artist_credit.name,
2020+ coalesce(releases.rank, 0),
2121+ 0,
2222+ 0
2323+from track
2424+ inner join isrc on isrc.recording = track.recording
2525+ inner join recording on recording.id = isrc.recording
2626+ inner join medium on medium.id = track.medium
2727+ inner join releases on releases.id = medium.release
2828+ inner join release_group on release_group.id = releases.release_group
2929+ left join artist_credit on artist_credit.id = recording.artist_credit
3030+where isrc.isrc = ?3
3131+order by releases.rank desc
4232limit 10;
···11-install fts;
22-load fts;
33-41begin;
55-create schema mbz;
6273-- TODO MAYBE: l_*_* link tables?
84-- TODO MAYBE: label, label_alias, label_gid_redirect, release_label ??
951010-create table mbz.artist
66+create table if not exists artist
117(
128 id bigint primary key,
1313- gid uuid not null,
1414- name text not null,
1515- sort_name text not null,
99+ gid text not null,
1010+ name text not null,
1111+ sort_name text not null,
1612 begin_date_year int,
1713 begin_date_month int,
1814 begin_date_day int,
···2420 gender int,
2521 comment text,
2622 edits_pending int,
2727- last_update timestamptz,
2828- ended bool not null,
2323+ last_update datetime,
2424+ ended text not null,
2925 begin_area int,
3030- end_area int,
2626+ end_area int
3127);
32283333-create table mbz.artist_alias
2929+create table if not exists artist_alias
3430(
3531 id bigint primary key,
3636- artist bigint not null,
3737- name text not null,
3232+ artist bigint not null references artist (id),
3333+ name text not null,
3834 locale text,
3935 edits_pending int,
4040- last_update timestamptz,
3636+ last_update datetime,
4137 type int,
4242- sort_name text not null,
3838+ sort_name text not null,
4339 begin_date_year int,
4440 begin_date_month int,
4541 begin_date_day int,
4642 end_date_year int,
4743 end_date_month int,
4844 end_date_day int,
4949- primary_for_locale bool not null,
5050- ended bool not null,
4545+ primary_for_locale text not null,
4646+ ended text not null
5147);
52485353-create table mbz.artist_credit
4949+create table if not exists artist_credit
5450(
5551 id bigint primary key,
5656- name text not null,
5757- artist_count int not null,
5858- ref_count int not null,
5959- created timestamptz not null,
5252+ name text not null,
5353+ artist_count int not null,
5454+ ref_count int not null,
5555+ created datetime not null,
6056 edits_pending int,
6161- gid uuid not null,
5757+ gid text not null
6258);
63596464-create table mbz.artist_credit_name
6060+create table if not exists artist_credit_name
6561(
6666- artist_credit bigint not null,
6262+ artist_credit bigint not null references artist_credit (id),
6763 position int not null,
6868- artist bigint not null,
6464+ artist bigint not null references artist (id),
6965 name text not null,
7066 join_phrase text,
71677268 primary key (artist_credit, position)
7369);
74707575-create table mbz.artist_gid_redirect
7171+create table if not exists artist_gid_redirect
7672(
7773 gid text primary key,
7878- new_id bigint not null,
7979- created timestamptz not null
7474+ new_id bigint not null references artist (id),
7575+ created datetime not null
8076);
81778282-create table mbz.artist_tag
7878+create table if not exists artist_tag
8379(
8484- artist bigint not null,
8585- tag bigint not null,
8080+ artist bigint not null references artist (id),
8181+ tag bigint not null references tag (id),
8682 count int,
8787- last_update timestamptz,
8383+ last_update datetime,
88848985 primary key (artist, tag)
9086);
91879292-create table mbz.genre
8888+8989+create table if not exists genre
9390(
9491 id bigint primary key,
9595- gid uuid not null,
9292+ gid text not null,
9693 name text not null,
9794 comment text,
9895 edits_pending int,
9999- last_update timestamptz
9696+ last_update datetime
10097);
10198102102-create table mbz.genre_alias
9999+create table if not exists genre_alias
103100(
104101 id bigint primary key,
105105- genre bigint not null,
106106- name text not null,
102102+ genre bigint not null references genre (id),
103103+ name text not null,
107104 locale text,
108105 edits_pending int,
109109- last_update timestamptz,
106106+ last_update datetime,
110107 type int,
111111- sort_name text not null,
108108+ sort_name text not null,
112109 begin_date_year int,
113110 begin_date_month int,
114111 begin_date_day int,
115112 end_date_year int,
116113 end_date_month int,
117114 end_date_day int,
118118- primary_for_locale bool not null,
119119- ended bool not null,
115115+ primary_for_locale text not null,
116116+ ended text not null
120117);
121118122122-create table mbz.isrc
119119+create table if not exists isrc
123120(
124121 id bigint primary key,
125125- recording bigint not null,
122122+ recording bigint not null references recording (id),
126123 isrc text not null,
127124 source int,
128125 edits_pending int,
129129- created timestamptz
126126+ created datetime
130127);
131128132132-create table mbz.medium
129129+create table if not exists medium
133130(
134131 id bigint primary key,
135135- release bigint not null,
132132+ "release" bigint not null references "release" (id),
136133 position int not null,
137134 format int,
138135 name text,
139136 edits_pending int,
140140- last_update timestamptz,
137137+ last_update datetime,
141138 track_count int,
142142- gid uuid not null,
139139+ gid text not null
143140);
144141145145-create table mbz.recording
142142+create table if not exists recording
146143(
147144 id bigint primary key,
148148- gid uuid not null,
149149- name text not null,
150150- artist_credit bigint not null,
145145+ gid text not null,
146146+ name text not null,
147147+ artist_credit bigint not null references artist_credit (id),
151148 length int,
152149 comment text,
153150 edits_pending int,
154154- last_update timestamptz,
155155- video bool not null,
151151+ last_update datetime,
152152+ video text not null
156153);
157154158158-create table mbz.recording_alias
155155+create table if not exists recording_alias
159156(
160157 id bigint primary key,
161161- recording bigint not null,
162162- name text not null,
158158+ recording bigint not null references recording (id),
159159+ name text not null,
163160 locale text,
164161 edits_pending int,
165165- last_update timestamptz,
162162+ last_update datetime,
166163 type int,
167167- sort_name text not null,
164164+ sort_name text not null,
168165 begin_date_year int,
169166 begin_date_month int,
170167 begin_date_day int,
171168 end_date_year int,
172169 end_date_month int,
173170 end_date_day int,
174174- primary_for_locale bool not null,
175175- ended bool not null,
171171+ primary_for_locale text not null,
172172+ ended text not null
176173);
177174178178-create table mbz.recording_gid_redirect
175175+create table if not exists recording_gid_redirect
179176(
180177 gid text primary key,
181181- new_id bigint not null,
182182- created timestamptz not null
178178+ new_id bigint not null references recording (id),
179179+ created datetime not null
183180);
184181185185-create table mbz.recording_tag
182182+create table if not exists recording_tag
186183(
187187- recording bigint not null,
188188- tag bigint not null,
184184+ recording bigint not null references recording (id),
185185+ tag bigint not null references tag (id),
189186 count int,
190190- last_updated timestamptz,
187187+ last_updated datetime,
191188192189 primary key (recording, tag)
193190);
194191195195-create table mbz.release
192192+create table if not exists "release"
196193(
197194 id bigint primary key,
198198- gid uuid not null,
195195+ gid text not null,
199196 name text not null,
200200- artist_credit bigint not null,
201201- release_group bigint not null,
197197+ artist_credit bigint not null references artist_credit (id),
198198+ release_group bigint not null references release_group (id),
202199 status int,
203200 packaging int,
204201 language int,
···207204 comment text,
208205 edits_pending int,
209206 quality int,
210210- last_update timestamptz
207207+ last_update datetime
211208);
212209213213-create table mbz.release_alias
210210+create table if not exists release_alias
214211(
215212 id bigint primary key,
216216- release bigint not null,
217217- name text not null,
213213+ "release" bigint not null references "release" (id),
214214+ name text not null,
218215 locale text,
219216 edits_pending int,
220220- last_update timestamptz,
217217+ last_update datetime,
221218 type int,
222222- sort_name text not null,
219219+ sort_name text not null,
223220 begin_date_year int,
224221 begin_date_month int,
225222 begin_date_day int,
226223 end_date_year int,
227224 end_date_month int,
228225 end_date_day int,
229229- primary_for_locale bool not null,
230230- ended bool not null,
226226+ primary_for_locale text not null,
227227+ ended text not null
231228);
232229233233-create table mbz.release_gid_redirect
230230+create table if not exists release_gid_redirect
234231(
235232 gid text primary key,
236236- new_id bigint not null,
237237- created timestamptz not null
233233+ new_id bigint not null,
234234+ created datetime not null
238235);
239236240240-create table mbz.release_group
237237+create table if not exists release_group
241238(
242239 id bigint primary key,
243243- gid uuid not null,
240240+ gid text not null,
244241 name text not null,
245245- artist_credit bigint not null,
242242+ artist_credit bigint not null references artist_credit (id),
246243 type int,
247244 comment text,
248245 edits_pending int,
249249- last_update timestamptz
246246+ last_update datetime
250247);
251248252252-create table mbz.release_group_alias
249249+create table if not exists release_group_alias
253250(
254251 id bigint primary key,
255255- release_group bigint not null,
256256- name text not null,
252252+ release_group bigint not null references release_group (id),
253253+ name text not null,
257254 locale text,
258255 edits_pending int,
259259- last_update timestamptz,
256256+ last_update datetime,
260257 type int,
261261- sort_name text not null,
258258+ sort_name text not null,
262259 begin_date_year int,
263260 begin_date_month int,
264261 begin_date_day int,
265262 end_date_year int,
266263 end_date_month int,
267264 end_date_day int,
268268- primary_for_locale bool not null,
269269- ended bool not null,
265265+ primary_for_locale text not null,
266266+ ended text not null
270267);
271268272272-create table mbz.release_group_gid_redirect
269269+create table if not exists release_group_gid_redirect
273270(
274271 gid text primary key,
275275- new_id bigint not null,
276276- created timestamptz not null
272272+ new_id bigint not null,
273273+ created datetime not null
277274);
278275279279-create table mbz.release_group_tag
276276+create table if not exists release_group_tag
280277(
281281- release_group bigint not null,
282282- tag bigint not null,
278278+ release_group bigint not null references release_group (id),
279279+ tag bigint not null references tag (id),
283280 count int,
284284- last_update timestamptz,
281281+ last_update datetime,
285282286283 primary key (release_group, tag)
287284);
288285289289-create table mbz.release_tag
286286+create table if not exists release_tag
290287(
291291- release bigint not null,
292292- tag bigint not null,
288288+ "release" bigint not null references "release" (id),
289289+ tag bigint not null references tag (id),
293290 count int,
294294- last_update timestamptz,
291291+ last_update datetime,
295292296296- primary key (release, tag)
293293+ primary key ("release", tag)
297294);
298295299299-create table mbz.tag
296296+create table if not exists tag
300297(
301298 id bigint primary key,
302299 name text not null,
303303- ref_count bigint,
300300+ ref_count bigint
304301);
305302306306-create table mbz.track
303303+create table if not exists track
307304(
308305 id bigint primary key,
309309- gid uuid not null,
310310- recording bigint not null,
311311- medium bigint not null,
306306+ gid text not null,
307307+ recording bigint not null references recording (id),
308308+ medium bigint not null references medium (id),
312309 position int,
313310 number text,
314314- name text not null,
315315- artist_credit bigint not null,
311311+ name text not null,
312312+ artist_credit bigint not null references artist_credit (id),
316313 length int,
317314 edits_pending int,
318318- last_updated timestamptz,
319319- is_data_track bool not null
320320-315315+ last_updated datetime,
316316+ is_data_track text not null
321317);
322318323323-create table mbz.track_gid_redirect
319319+create table if not exists track_gid_redirect
324320(
325321 gid text primary key,
326326- new_id bigint not null,
327327- created timestamptz not null
322322+ new_id bigint not null,
323323+ created datetime not null
328324);
329325commit;
+6-18
src/mbz/init_fts.sql
···11-PRAGMA create_fts_index('mbz.artist_credit', 'id', 'name');
22-PRAGMA create_fts_index('mbz.recording', 'id', 'name');
33-PRAGMA create_fts_index('mbz.release', 'id', 'name', 'comment');
44-55-create macro if not exists mbz.fts_release (term, gid) AS CASE
66- WHEN gid IS NOT NULL THEN 10
77- ELSE fts_mbz_release.match_bm25 (release.id, term) * if (lower(release.name) = lower(term), 2.5, 1)
88-END;
99-1010-create macro if not exists mbz.fts_recording (term, gid) AS CASE
1111- WHEN gid IS NOT NULL THEN 10
1212- ELSE fts_mbz_recording.match_bm25 (recording.id, term) * if (lower(recording.name) = lower(term), 2.5, 1)
1313-END;
11+create virtual table fts_artist_credit using fts5(name, content='artist_credit', content_rowid='id');
22+create virtual table fts_recording using fts5(name, content='recording', content_rowid='id');
33+create virtual table fts_release using fts5(name, comment, content='release', content_rowid='id');
1441515--- like the above but `trig` takes the release/recording mbid/isrc to disable the query
1616-create macro if not exists mbz.fts_artists(term, trig) AS CASE
1717- WHEN trig IS NOT NULL THEN 10
1818- ELSE fts_mbz_artist_credit.match_bm25(artist_credit.id, term) * if (lower(artist_credit.name) = lower(term), 2.5, 1)
1919-END;
55+insert into fts_artist_credit(fts_artist_credit) values ('rebuild');
66+insert into fts_recording(fts_recording) values ('rebuild');
77+insert into fts_release(fts_release) values ('rebuild');
+48
src/mbz/init_index.sql
···11+create index if not exists artist__gid_idx on artist (gid);
22+33+create index if not exists artist_alias__artist_idx on artist_alias (artist);
44+55+create index if not exists artist_credit_name__artist_credit_idx on artist_credit_name (artist_credit);
66+create index if not exists artist_credit_name__artist_idx on artist_credit_name (artist_credit);
77+88+create index if not exists artist_tag__tag_idx on artist_tag (tag);
99+create index if not exists artist_tag__artist_idx on artist_tag (artist);
1010+1111+create index if not exists genre__gid_idx on genre (gid);
1212+1313+create index if not exists genre_alias__genre_idx on genre_alias (genre);
1414+1515+create index if not exists isrc__recording_idx on isrc (recording);
1616+1717+create index if not exists medium__gid_idx on medium (gid);
1818+create index if not exists medium__release_idx on medium ("release");
1919+2020+create index if not exists recording__artist_credit_idx on recording (artist_credit);
2121+create index if not exists recording__gid_idx on recording (gid);
2222+2323+create index if not exists recording_alias__recording_idx on recording_alias (recording);
2424+2525+create index if not exists recording_tag__tag_idx on recording_tag (tag);
2626+create index if not exists recording_tag__recording_idx on recording_tag (recording);
2727+2828+create index if not exists release__artist_credit_idx on "release" (artist_credit);
2929+create index if not exists release__gid_idx on "release" (gid);
3030+create index if not exists release__release_group_idx on "release" (release_group);
3131+3232+create index if not exists release_alias__recording_idx on release_alias ("release");
3333+3434+create index if not exists release_group__artist_credit_idx on release_group (artist_credit);
3535+create index if not exists release_group__gid_idx on release_group (gid);
3636+3737+create index if not exists release_group_alias__release_group_idx on release_group_alias (release_group);
3838+3939+create index if not exists release_group_tag__tag_idx on release_group_tag (tag);
4040+create index if not exists release_group_tag__release_group_idx on release_group_tag (release_group);
4141+4242+create index if not exists release_tag__tag_idx on release_tag (tag);
4343+create index if not exists release_tag__release_idx on release_tag ("release");
4444+4545+create index if not exists track__artist_credit_idx on track ("artist_credit");
4646+create index if not exists track__gid_idx on track (gid);
4747+create index if not exists track__medium_idx on track (medium);
4848+create index if not exists track__recording_idx on track (recording);