Modern Apps git

← Modern-Apps.git

musicbrainz: fold artist-credit terms into search indexes

Commit
af841558b6685b5c2768ec74d5e9a2c1864ba36c
Author
vayun-mathur <[email protected]>
When
2026-08-21 00:03 UTC (3 months ago)
Parents
c6f8791

Diff

scripts/musicbrainz/mb_ingest/src/build.rs
diff --git a/scripts/musicbrainz/mb_ingest/src/build.rs b/scripts/musicbrainz/mb_ingest/src/build.rsindex d0977ec345..6fdf163935 100644--- a/scripts/musicbrainz/mb_ingest/src/build.rs+++ b/scripts/musicbrainz/mb_ingest/src/build.rs@@ -468,6 +468,14 @@ impl Drop for PostingSpill {     } } +/// The display-string symbol for a credit id, or `SYM_EMPTY` when there is none.+fn credit_sym_of(credit_syms: &[Sym], credit_id: u32) -> Sym {+    if credit_id == pack::NONE {+        return SYM_EMPTY;+    }+    credit_syms.get(credit_id as usize).copied().unwrap_or(SYM_EMPTY)+}+ /// Tokenise `text` and spill one posting per term. The term must already be in /// `terms` from the dictionary pass. fn emit_postings(@@ -1123,7 +1131,12 @@ pub fn build<W: Write + Seek>(     let credit_offsets: Vec<u32> = credit_syms.iter().map(|&s| pool.offset(s)).collect();     w.section_u32(pack::S_CREDITS, &credit_offsets)?;     drop(credit_offsets);-    drop(credit_syms);+    // `credit_syms` survives to the search pass: artist-credit terms are folded into+    // the release-group and recording indexes so that typing an artist name into the+    // albums tab finds their albums. WS/2's `release-group?query=` matches artist+    // names, so a title-only index would be a REGRESSION against the live API the+    // pack replaces -- "radiohead" would return nothing rather than fewer results.+    // 20 MB to keep, and dropped as soon as the index is built.      let mut buf: Vec<u8> = Vec::new();     for a in &artists {@@ -1163,6 +1176,10 @@ pub fn build<W: Write + Seek>(     drop(recs);     drop(hi);     let rg_title_syms: Vec<Sym> = rgs.iter().map(|r| r.title).collect();+    let rg_credit_syms: Vec<Sym> = rgs+        .iter()+        .map(|r| credit_sym_of(&credit_syms, r.credit_id))+        .collect();     drop(rgs);      w.section_u32(pack::S_ARTIST_RGS, &artist_rgs)?;@@ -1415,6 +1432,20 @@ pub fn build<W: Write + Seek>(             terms.intern(t);         }     }+    // Every artist-credit display string, interned once. That covers the credits of+    // both release groups and recordings without walking 39.9 M recordings here --+    // 5.08 M strings instead. The postings pass below attaches them per entity.+    for &sym in credit_syms.iter() {+        if sym == SYM_EMPTY {+            continue;+        }+        let text = String::from_utf8_lossy(pool.get(sym)).into_owned();+        terms_buf.clear();+        search_terms(&text, &mut terms_buf);+        for t in terms_buf.iter() {+            terms.intern(t);+        }+    }     if opts.include_recording_search {         for id in rec_order.iter() {             let text = String::from_utf8_lossy(pool.get(rec_title[*id as usize])).into_owned();@@ -1452,35 +1483,44 @@ pub fn build<W: Write + Seek>(         }         for (i, &sym) in rg_title_syms.iter().enumerate() {             check_searchable(i as u32)?;+            let refv = (pack::KIND_RELEASE_GROUP << pack::KIND_SHIFT) | i as u32;             let text = String::from_utf8_lossy(pool.get(sym)).into_owned();-            emit_postings(-                &text,-                (pack::KIND_RELEASE_GROUP << pack::KIND_SHIFT) | i as u32,-                &terms,-                &term_ranks,-                &mut terms_buf,-                &mut post_spill,-            )?;+            emit_postings(&text, refv, &terms, &term_ranks, &mut terms_buf, &mut post_spill)?;+            // Its artist credit, so "radiohead" finds Radiohead's albums. Duplicate+            // (term, entity) pairs between title and credit dedup at emit time.+            let csym = rg_credit_syms[i];+            if csym != SYM_EMPTY {+                let ctext = String::from_utf8_lossy(pool.get(csym)).into_owned();+                emit_postings(&ctext, refv, &terms, &term_ranks, &mut terms_buf, &mut post_spill)?;+            }         }         if opts.include_recording_search {             for (i, id) in rec_order.iter().enumerate() {                 check_searchable(i as u32)?;+                let refv = (pack::KIND_RECORDING << pack::KIND_SHIFT) | i as u32;                 let text =                     String::from_utf8_lossy(pool.get(rec_title[*id as usize])).into_owned();-                emit_postings(-                    &text,-                    (pack::KIND_RECORDING << pack::KIND_SHIFT) | i as u32,-                    &terms,-                    &term_ranks,-                    &mut terms_buf,-                    &mut post_spill,-                )?;+                emit_postings(&text, refv, &terms, &term_ranks, &mut terms_buf, &mut post_spill)?;+                let csym = credit_sym_of(&credit_syms, rec_credit[*id as usize]);+                if csym != SYM_EMPTY {+                    let ctext = String::from_utf8_lossy(pool.get(csym)).into_owned();+                    emit_postings(+                        &ctext,+                        refv,+                        &terms,+                        &term_ranks,+                        &mut terms_buf,+                        &mut post_spill,+                    )?;+                }             }         }     }     drop(term_ranks);     drop(artist_name_syms);     drop(rg_title_syms);+    drop(rg_credit_syms);+    drop(credit_syms);     st.search_postings = post_spill.written / 8;     let post_buckets = post_spill.finish()?; 
scripts/musicbrainz/mb_ingest/tests/round_trip.rs
diff --git a/scripts/musicbrainz/mb_ingest/tests/round_trip.rs b/scripts/musicbrainz/mb_ingest/tests/round_trip.rsindex 24583da064..eddaaaaf4b 100644--- a/scripts/musicbrainz/mb_ingest/tests/round_trip.rs+++ b/scripts/musicbrainz/mb_ingest/tests/round_trip.rs@@ -379,9 +379,48 @@ fn search_finds_things_by_word_prefix() {     assert_eq!(hits.len(), 1);     assert_eq!(&*pack.recording(hits[0].idx).unwrap().title, "Breathe"); -    // A search term shared by several release groups ranks, dedups and truncates.+    // Artist-credit terms are folded into the release-group and recording indexes,+    // because WS/2's `release-group?query=` matches artist names: a title-only index+    // would be a REGRESSION against the live API, returning nothing for "radiohead"+    // in the albums tab rather than fewer results.+    let hits = pack.search_release_groups("pink", 25);+    let titles: Vec<String> = hits+        .iter()+        .map(|h| pack.release_group(h.idx).unwrap().title.into_owned())+        .collect();+    assert!(+        !titles.contains(&"Homogenic".to_string()),+        "Homogenic is not credited to Pink Floyd"+    );+    assert!(+        titles.iter().any(|t| t == "The Dark Side of the Moon"),+        "an artist-credit term must reach the artist's albums, got {titles:?}"+    );+    // "björk" appears in no release-group TITLE in the fixture, only in credits.+    let hits = pack.search_release_groups("björk", 25);+    let titles: Vec<String> = hits+        .iter()+        .map(|h| pack.release_group(h.idx).unwrap().title.into_owned())+        .collect();+    assert!(+        titles.iter().any(|t| t == "Homogenic"),+        "credit-only match must work, got {titles:?}"+    );+    // Same for recordings, whose credits are attached per recording.+    let hits = pack.search_recordings("björk", 25);+    assert!(!hits.is_empty(), "recording credits must be searchable too");+    let found: Vec<String> =+        hits.iter().map(|h| pack.recording(h.idx).unwrap().title.into_owned()).collect();+    assert!(+        found.iter().any(|t| t == "Jóga" || t == "Bachelorette"),+        "expected a Björk recording, got {found:?}"+    );++    // "pink floyd" now matches every release group CREDITED to Pink Floyd, not just+    // the one with both words in its title. That is the whole point of folding+    // credits in, and it is parity with what WS/2 does today.     let hits = pack.search_release_groups("pink floyd", 25);-    assert_eq!(hits.len(), 1, "only the compilation has both words in its title");+    assert_eq!(hits.len(), 3, "Dark Side, Echoes and the feat. release group");     let hits = pack.search_artists("pink", 0);     assert!(hits.is_empty(), "limit 0 returns nothing rather than everything");     assert!(pack.search_artists("", 25).is_empty());