@@ -43,9 +43,36 @@ pub fn event_to_document(event: &StoredEvent) -> Result<Value, SearchError> {
4343 . map ( |id| id. to_string ( ) )
4444 . unwrap_or_else ( || "__global__" . to_string ( ) ) ;
4545
46+ // For kind:0 (user metadata) we append the parsed JSON values to `content`
47+ // so Typesense's word-tokenizer can index them cleanly. Without this, a
48+ // raw blob like `{"display_name":"alice","about":"loves cats"}` does not
49+ // produce a clean `alice` token — the leading `"` glues onto the next
50+ // word, so the doc is unreachable for the obvious `q=alice` search.
51+ //
52+ // We only flatten kind:0 (the structured-metadata kind defined by NIP-01)
53+ // and only the small set of fields the member-picker uses. Bio / about /
54+ // website are intentionally left out so they don't pollute name-prefix
55+ // searches with false positives. Stays consistent with `display_name >
56+ // nip05 > pubkey` ranking applied on the desktop side.
57+ //
58+ // The Typesense `content` field is write-only as far as the relay's read
59+ // paths go (the bridge fetches the canonical event from Postgres by id
60+ // after Typesense returns hits), so appending derived tokens here doesn't
61+ // affect any consumer's view of the event's actual content.
62+ //
63+ // NOTE: existing kind:0 docs indexed before this change won't have the
64+ // appended tokens. Running `just reindex-search` (or the
65+ // `sprout-relay reindex-search` admin path) repopulates them. New /
66+ // updated profiles get the tokens automatically.
67+ let content_indexed = if event_kind_i32 ( nostr_event) == 0 {
68+ flatten_kind0_for_indexing ( nostr_event. content . as_str ( ) )
69+ } else {
70+ nostr_event. content . as_str ( ) . to_string ( )
71+ } ;
72+
4673 let doc = json ! ( {
4774 "id" : nostr_event. id. to_string( ) ,
48- "content" : nostr_event . content . as_str ( ) ,
75+ "content" : content_indexed ,
4976 // Cast to i32 for Typesense schema (int32 field). nostr Kind is u16; all Sprout kinds fit in i32.
5077 "kind" : event_kind_i32( nostr_event) ,
5178 "pubkey" : nostr_event. pubkey. to_string( ) ,
@@ -57,6 +84,38 @@ pub fn event_to_document(event: &StoredEvent) -> Result<Value, SearchError> {
5784 Ok ( doc)
5885}
5986
87+ /// For kind:0 events, return the original content with the searchable fields
88+ /// (`display_name`, `name`, `nip05`) appended as space-separated plain words.
89+ ///
90+ /// Tolerant of malformed input: anything that fails JSON parsing returns the
91+ /// original content unchanged, never an error.
92+ fn flatten_kind0_for_indexing ( raw_content : & str ) -> String {
93+ let Ok ( parsed) = serde_json:: from_str :: < Value > ( raw_content) else {
94+ return raw_content. to_string ( ) ;
95+ } ;
96+ let Some ( obj) = parsed. as_object ( ) else {
97+ return raw_content. to_string ( ) ;
98+ } ;
99+
100+ let mut extracted: Vec < & str > = Vec :: with_capacity ( 3 ) ;
101+ for key in [ "display_name" , "name" , "nip05" ] {
102+ if let Some ( val) = obj. get ( key) . and_then ( Value :: as_str) {
103+ let trimmed = val. trim ( ) ;
104+ if !trimmed. is_empty ( ) {
105+ extracted. push ( trimmed) ;
106+ }
107+ }
108+ }
109+
110+ if extracted. is_empty ( ) {
111+ raw_content. to_string ( )
112+ } else {
113+ // Single leading space ensures we don't smash the closing `}` of the
114+ // original JSON into the first appended token.
115+ format ! ( "{} {}" , raw_content, extracted. join( " " ) )
116+ }
117+ }
118+
60119/// Indexes a single event via Typesense upsert.
61120pub async fn index_event (
62121 client : & reqwest:: Client ,
@@ -269,6 +328,134 @@ mod tests {
269328 assert_eq ! ( doc[ "channel_id" ] . as_str( ) . unwrap( ) , "__global__" ) ;
270329 }
271330
331+ // ── kind:0 flattening for searchability ─────────────────────────────────
332+
333+ #[ test]
334+ fn kind0_appends_display_name_for_tokenization ( ) {
335+ let stored = make_stored_event (
336+ r#"{"display_name":"alice","about":"loves cats"}"# ,
337+ Kind :: Metadata ,
338+ None ,
339+ ) ;
340+ let doc = event_to_document ( & stored) . unwrap ( ) ;
341+ let content = doc[ "content" ] . as_str ( ) . unwrap ( ) ;
342+ // Original JSON is preserved (read paths don't depend on this but it
343+ // costs nothing and makes debugging the index cheaper).
344+ assert ! ( content. contains( r#""display_name":"alice""# ) ) ;
345+ // The display name is also present as a free-standing token so the
346+ // default Typesense tokenizer can index it without the leading-quote
347+ // gluing onto the next character.
348+ assert ! ( content. ends_with( " alice" ) , "got: {content:?}" ) ;
349+ }
350+
351+ #[ test]
352+ fn kind0_appends_name_when_display_name_absent ( ) {
353+ // NIP-01 allows `name` as the canonical display field too.
354+ let stored = make_stored_event ( r#"{"name":"bob","about":"x"}"# , Kind :: Metadata , None ) ;
355+ let doc = event_to_document ( & stored) . unwrap ( ) ;
356+ let content = doc[ "content" ] . as_str ( ) . unwrap ( ) ;
357+ assert ! ( content. ends_with( " bob" ) , "got: {content:?}" ) ;
358+ }
359+
360+ #[ test]
361+ fn kind0_includes_both_display_name_and_name_when_present ( ) {
362+ let stored = make_stored_event (
363+ r#"{"display_name":"Alice","name":"alice"}"# ,
364+ Kind :: Metadata ,
365+ None ,
366+ ) ;
367+ let doc = event_to_document ( & stored) . unwrap ( ) ;
368+ let content = doc[ "content" ] . as_str ( ) . unwrap ( ) ;
369+ assert ! ( content. ends_with( " Alice alice" ) , "got: {content:?}" ) ;
370+ }
371+
372+ #[ test]
373+ fn kind0_includes_nip05_in_appended_tokens ( ) {
374+ let stored = make_stored_event (
375+ r#"{"display_name":"alice","nip05":"[email protected] "}"# , 376+ Kind :: Metadata ,
377+ None ,
378+ ) ;
379+ let doc = event_to_document ( & stored) . unwrap ( ) ;
380+ let content = doc[ "content" ] . as_str ( ) . unwrap ( ) ;
381+ assert ! (
382+ content
. ends_with
( " alice [email protected] " ) , 383+ "got: {content:?}"
384+ ) ;
385+ }
386+
387+ #[ test]
388+ fn kind0_excludes_about_and_website_from_appended_tokens ( ) {
389+ // `about` and `website` deliberately do not get appended — including
390+ // them would cause name-prefix searches to return false positives from
391+ // bios. The user's own display_name still appears.
392+ let stored = make_stored_event (
393+ r#"{"display_name":"alice","about":"I work with bob on x","website":"https://carol.example"}"# ,
394+ Kind :: Metadata ,
395+ None ,
396+ ) ;
397+ let doc = event_to_document ( & stored) . unwrap ( ) ;
398+ let content = doc[ "content" ] . as_str ( ) . unwrap ( ) ;
399+ assert ! ( content. ends_with( " alice" ) , "got: {content:?}" ) ;
400+ // Sanity: the about/website are still in the doc because we preserve
401+ // the original JSON — they just don't appear in the trailing tokens.
402+ assert ! ( content. contains( "bob" ) ) ;
403+ assert ! ( content. contains( "carol" ) ) ;
404+ }
405+
406+ #[ test]
407+ fn kind0_malformed_json_is_passed_through_unchanged ( ) {
408+ let stored = make_stored_event ( "not json at all" , Kind :: Metadata , None ) ;
409+ let doc = event_to_document ( & stored) . unwrap ( ) ;
410+ assert_eq ! ( doc[ "content" ] . as_str( ) . unwrap( ) , "not json at all" ) ;
411+ }
412+
413+ #[ test]
414+ fn kind0_non_object_json_is_passed_through_unchanged ( ) {
415+ // Defensive: NIP-01 says content is a JSON object, but a malformed
416+ // client could publish e.g. a JSON array. We don't crash, we just
417+ // skip the flattening for that doc.
418+ let stored = make_stored_event ( r#"["nope"]"# , Kind :: Metadata , None ) ;
419+ let doc = event_to_document ( & stored) . unwrap ( ) ;
420+ assert_eq ! ( doc[ "content" ] . as_str( ) . unwrap( ) , r#"["nope"]"# ) ;
421+ }
422+
423+ #[ test]
424+ fn kind0_empty_string_values_skipped ( ) {
425+ let stored = make_stored_event (
426+ r#"{"display_name":"","name":"alice","nip05":" "}"# ,
427+ Kind :: Metadata ,
428+ None ,
429+ ) ;
430+ let doc = event_to_document ( & stored) . unwrap ( ) ;
431+ let content = doc[ "content" ] . as_str ( ) . unwrap ( ) ;
432+ // Only `name` is non-empty; whitespace-only `nip05` is also skipped.
433+ assert ! ( content. ends_with( " alice" ) , "got: {content:?}" ) ;
434+ }
435+
436+ #[ test]
437+ fn kind0_no_searchable_fields_is_passed_through ( ) {
438+ // Profile with only fields we don't extract.
439+ let stored = make_stored_event (
440+ r#"{"about":"just a bio","picture":"https://x"}"# ,
441+ Kind :: Metadata ,
442+ None ,
443+ ) ;
444+ let doc = event_to_document ( & stored) . unwrap ( ) ;
445+ let content = doc[ "content" ] . as_str ( ) . unwrap ( ) ;
446+ // No trailing space-separated tokens added; original content unchanged.
447+ assert_eq ! ( content, r#"{"about":"just a bio","picture":"https://x"}"# ) ;
448+ }
449+
450+ #[ test]
451+ fn non_kind0_events_not_flattened ( ) {
452+ // kind:1 (note) with a JSON-looking body must be left strictly alone.
453+ let json_looking = r#"{"display_name":"alice"}"# ;
454+ let stored = make_stored_event ( json_looking, Kind :: TextNote , None ) ;
455+ let doc = event_to_document ( & stored) . unwrap ( ) ;
456+ assert_eq ! ( doc[ "content" ] . as_str( ) . unwrap( ) , json_looking) ;
457+ }
458+
272459 #[ test]
273460 fn tag_flattening_uses_unit_separator ( ) {
274461 let keys = Keys :: generate ( ) ;
0 commit comments