Index: openacs-4/packages/tsearch2-driver/tcl/tsearch2-driver-procs.tcl =================================================================== RCS file: /usr/local/cvsroot/openacs-4/packages/tsearch2-driver/tcl/tsearch2-driver-procs.tcl,v diff -u -N -r1.10 -r1.11 --- openacs-4/packages/tsearch2-driver/tcl/tsearch2-driver-procs.tcl 30 Dec 2004 15:31:00 -0000 1.10 +++ openacs-4/packages/tsearch2-driver/tcl/tsearch2-driver-procs.tcl 18 Feb 2005 18:36:11 -0000 1.11 @@ -1,9 +1,6 @@ -# - ad_library { - Procedures for tsearch full text enginge driver - + @author Dave Bauer (dave@thedesignexperience.org) @creation-date 2004-06-05 @arch-tag: 49a5102d-7c06-4245-8b8d-15a3b12a8cc5 @@ -18,44 +15,42 @@ title keywords } { - add object to full text index - + @author Dave Bauer (dave@thedesignexperience.org) @creation-date 2004-06-05 - + @param object_id @param txt @param title @param keywords - - @return - - @error + + @return nothing } { set index_exists_p [db_0or1row object_exists "select 1 from txt where object_id=:object_id"] if {!$index_exists_p} { - db_dml index "insert into txt (object_id,fti) values ( :object_id, to_tsvector('default',:txt))" - + db_dml index " + insert into txt (object_id,fti) + values ( :object_id, + setweight(to_tsvector('default',coalesce(:title,'')),'A') + ||setweight(to_tsvector('default',coalesce(:keywords,'')),'B') + ||to_tsvector('default',coalesce(:txt,'')))" } else { tsearch2::update_index $object_id $txt $title $keywords } - } ad_proc -public tsearch2::unindex { object_id } { Remove item from FTS index - + @author Dave Bauer (dave@thedesignexperience.org) @creation-date 2004-06-05 - + @param object_id - @return - - @error + @return nothing } { db_dml unindex "delete from txt where object_id=:object_id" } @@ -66,28 +61,30 @@ title keywords } { - update full text index - + @author Dave Bauer (dave@thedesignexperience.org) @creation-date 2004-06-05 - + @param object_id @param txt @param title @param keywords - - @return - - @error + + @return nothing } { set index_exists_p [db_0or1row object_exists "select 1 from txt where object_id=:object_id"] if {!$index_exists_p} { - db_dml index "insert into txt (object_id,fti) values ( :object_id, to_tsvector('default',:txt))" + tsearch2::index $object_id $txt $title $keywords } else { - db_dml update_index "update txt set fti = to_tsvector('default',:txt) where object_id=:object_id" + db_dml update_index " + update txt set fti = + setweight(to_tsvector('default',coalesce(:title,'')),'A') + ||setweight(to_tsvector('default',coalesce(:keywords,'')),'B') + ||to_tsvector('default',coalesce(:txt,'')) + where object_id=:object_id + " } - } ad_proc -public tsearch2::search { @@ -103,7 +100,7 @@ @author Dave Bauer (dave@thedesignexperience.org) @creation-date 2004-06-05 - + @param query @param offset @@ -116,9 +113,9 @@ @param dt - @return - - @error + @return + + @error } { # clean up query # turn and into & @@ -128,6 +125,7 @@ set limit_clause "" set offset_clause "" + if {[string is integer $limit]} { set limit_clause " limit :limit " } @@ -138,7 +136,7 @@ from acs_object_party_privilege_map m where m.object_id = txt.object_id and m.party_id = :user_id - and m.privilege = 'read') order by rank(fti,to_tsquery('default',:query)) ${limit_clause} ${offset_clause}" + and m.privilege = 'read') order by rank(fti,to_tsquery('default',:query)) desc ${limit_clause} ${offset_clause}" set results_ids [db_list search $query_text] set count [db_string count "select count(*) from txt where fti @@ to_tsquery('default',:query) and exists (select 1 from acs_object_party_privilege_map m @@ -147,26 +145,26 @@ and m.privilege = 'read')"] set stop_words [list] # lovely the search package requires count to be returned but the - # service contract definition doesn't specify it! + # service contract definition doesn't specify it! return [list ids $results_ids stopwords $stop_words count $count] } ad_proc -public tsearch2::summary { query txt } { - - + Highlights matching terms. + @author Dave Bauer (dave@thedesignexperience.org) @creation-date 2004-06-05 - + @param query @param txt @return summary containing search query terms - - @error + + @error } { set query [tsearch2::build_query -query $query] return [db_string summary "select headline('default',:txt,to_tsquery('default',:query))"] @@ -196,14 +194,14 @@ @return returns formatted query string for tsearch2 tsquery } { # get rid of everything that isn't a letter or number - regsub -all {[^?\d\w\s]} $query {} query + regsub -all {[^-/@.\d\w\s]+} $query { } query # replace boolean words with boolean operators - set query [string map {" and " & " or " | " not " !} $query] + set query [string map {" and " & " or " | " not " " ! "} " $query "] # remove leading and trailing spaces so they aren't turned into | set query [string trim $query] # remove any spaces between words and operators - regsub -all {\s+([!&|])\s+} $query {\1} query + regsub -all {\s*([!&|])\s+} $query {\1} query # all remaining spaces between words turn into | regsub -all {\s+} $query {\&} query # if a ! is by itself then prepend &