Index: openacs.org-dev/packages/forums/sql/postgresql/forums-sc-create.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs.org-dev/packages/forums/sql/postgresql/forums-sc-create.sql,v
diff -u -r1.2 -r1.3
--- openacs.org-dev/packages/forums/sql/postgresql/forums-sc-create.sql	28 Oct 2002 20:40:16 -0000	1.2
+++ openacs.org-dev/packages/forums/sql/postgresql/forums-sc-create.sql	17 Apr 2003 17:02:46 -0000	1.3
@@ -24,24 +24,43 @@
 	'TCL'				-- impl_pl
 );
 
-create function forums_message_search__itrg ()
+-- til: only indexing full threads. changes to child messages will be treated as 
+-- change to the thread.
+
+create or replace function forums_message_search__itrg ()
 returns opaque as '
 begin
-    perform search_observer__enqueue(new.message_id,''INSERT'');
+    if new.parent_id is null then
+        perform search_observer__enqueue(new.message_id,''INSERT'');
+    else
+        perform search_observer__enqueue(forums_message__root_message_id(new.parent_id),''UPDATE'');
+    end if;
     return new;
 end;' language 'plpgsql';
 
-create function forums_message_search__dtrg ()
+create or replace function forums_message_search__dtrg ()
 returns opaque as '
+declare
+     v_root_message_id          forums_messages.message_id%TYPE;
 begin
-    perform search_observer__enqueue(old.message_id,''DELETE'');
+    -- if the deleted msg has a parent then its an UPDATE to a thread, otherwise a DELETE.
+
+    if old.parent_id is null then
+        perform search_observer__enqueue(old.message_id,''DELETE'');
+    else
+        v_root_message_id := forums_message__root_message_id(old.parent_id);
+        if not v_root_message_id is null then
+            perform search_observer__enqueue(v_root_message_id,''UPDATE'');
+        end if;
+    end if;
+
     return old;
 end;' language 'plpgsql';
 
-create function forums_message_search__utrg ()
+create or replace function forums_message_search__utrg ()
 returns opaque as '
 begin
-    perform search_observer__enqueue(old.message_id,''UPDATE'');
+    perform search_observer__enqueue(forums_message__root_message_id (old.message_id),''UPDATE'');
     return old;
 end;' language 'plpgsql';
 
Index: openacs.org-dev/packages/forums/sql/postgresql/forums-sc-drop.sql
===================================================================
RCS file: /usr/local/cvsroot/openacs.org-dev/packages/forums/sql/postgresql/forums-sc-drop.sql,v
diff -u -r1.1 -r1.2
--- openacs.org-dev/packages/forums/sql/postgresql/forums-sc-drop.sql	8 Aug 2002 23:50:28 -0000	1.1
+++ openacs.org-dev/packages/forums/sql/postgresql/forums-sc-drop.sql	17 Apr 2003 17:02:46 -0000	1.2
@@ -11,3 +11,13 @@
 	   'FtsContentProvider',		-- impl_contract_name
            'forums_message'				-- impl_name
 );
+
+
+
+drop trigger forums_message_search__itrg on forums_messages;
+drop trigger forums_message_search__dtrg on forums_messages;
+drop trigger forums_message_search__utrg on forums_messages;
+
+drop function forums_message_search__itrg ();
+drop function forums_message_search__dtrg ();
+drop function forums_message_search__utrg ();
Index: openacs.org-dev/packages/forums/tcl/forums-sc-procs.tcl
===================================================================
RCS file: /usr/local/cvsroot/openacs.org-dev/packages/forums/tcl/forums-sc-procs.tcl,v
diff -u -r1.5 -r1.6
--- openacs.org-dev/packages/forums/tcl/forums-sc-procs.tcl	21 Aug 2002 11:42:42 -0000	1.5
+++ openacs.org-dev/packages/forums/tcl/forums-sc-procs.tcl	17 Apr 2003 17:04:08 -0000	1.6
@@ -17,16 +17,65 @@
 
     returns a datasource for the search package
     this is the content that will be indexed by the full text
-    search engine
+    search engine.
 
+    We expect message_id to be a root message of a thread only, 
+    and return the text of all the messages below
+
 } {
+
     forum::message::get -message_id $message_id -array message
-	set datasource(object_id) $message(message_id)
-	set datasource(title) $message(subject)
-	set datasource(content) $message(content)
-	set datasource(keywords) ""
-	set datasource(storage_type) text
-	set datasource(mime) "text/html"
+
+    if { ![empty_string_p $message(parent_id)] } {
+        ns_log notice "forum::message::datasource was called with a message_id that has a parent - skipping: $message_id"
+        set empty(object_id) $message_id
+        set empty(title) ""
+        set empty(content) ""
+        set empty(keywords) ""
+        set empty(storage_type) ""
+        set empty(mime) ""
+        return [array get empty]
+    }
+    
+    set tree_sortkey $message(tree_sortkey)
+    set forum_id $message(forum_id)
+    set combined_content ""
+    set subjects [list]
+    lappend subjects $message(subject)
+
+    db_foreach messages {
+        select subject, content, html_p
+        from forums_messages
+        where message_id=:message_id or (tree_sortkey between tree_left(:tree_sortkey) and tree_right(:tree_sortkey))
+	and forum_id=:forum_id
+        order by tree_sortkey
+    } {
+
+        # include the subject in the text if it is different from the thread's subject
+        set root_subject $message(subject)
+        regexp {^(?:Re: )+(.*)$} $subject match subject
+
+        if { [string compare $subject $root_subject] != 0 } {
+            # different subject
+            append combined_content "$subject\n\n"
+        }
+        
+        if  { $html_p } {
+            append combined_content [ad_html_to_text -showtags $content]
+        } else {
+            append combined_content $content
+        }
+
+        # In case this text is not only used for indexing but also for display, beautify it
+        append combined_content "\n\n"
+    }
+
+    set datasource(object_id) $message(message_id)
+    set datasource(title) $message(subject)
+    set datasource(content) $combined_content
+    set datasource(keywords) ""
+    set datasource(storage_type) text
+    set datasource(mime) "text/plain"
     return [array get datasource]
 }