mark: A photo of Mark kneeling on top of the Taal Volcano in the Philippines. It was a long hike. (Default)
Mark Smith ([staff profile] mark) wrote in [site community profile] changelog2009-10-20 02:36 am

[dw-free] Site-wide search

[commit: http://hg.dwscoalition.org/dw-free/rev/b1cb4d76b4b0]

http://bugs.dwscoalition.org/show_bug.cgi?id=1902

Add new options to site and journal search: you can sort results by date,
and if you quote the entire search query, we will do an exact phrase search.

Patch by [staff profile] mark.

Files modified:
  • bin/worker/sphinx-search-gm
  • htdocs/search.bml
--------------------------------------------------------------------------------
diff -r 7f743ff0a3ef -r b1cb4d76b4b0 bin/worker/sphinx-search-gm
--- a/bin/worker/sphinx-search-gm	Tue Oct 20 01:35:49 2009 +0000
+++ b/bin/worker/sphinx-search-gm	Tue Oct 20 02:36:10 2009 +0000
@@ -41,6 +41,18 @@ sub sphinx_search {
        ->SetSortMode( SPH_SORT_RELEVANCE )
        ->SetMaxQueryTime( 15_000 )
        ->SetLimits( $args->{offset} || 0, 20 );
+
+    # adjust the match mode if there are quotes around the entire query
+    if ( $args->{query} =~ s/^['"](.+)['"]$/$1/ ) {
+        $sx->SetMatchMode( SPH_MATCH_PHRASE );
+    }
+
+    # setup the sort they've requested
+    if ( $args->{sort_by} eq 'new' ) {
+        $sx->SetSortMode( SPH_SORT_ATTR_DESC, 'date_posted' );
+    } elsif ( $args->{sort_by} eq 'old' ) {
+        $sx->SetSortMode( SPH_SORT_ATTR_ASC, 'date_posted' );
+    }
     
     # filter to a journal if we have a userid set; else, filter on allow_global_search items
     $sx->SetFilter( 'journal_id', [ $args->{userid} ] )
diff -r 7f743ff0a3ef -r b1cb4d76b4b0 htdocs/search.bml
--- a/htdocs/search.bml	Tue Oct 20 01:35:49 2009 +0000
+++ b/htdocs/search.bml	Tue Oct 20 02:36:10 2009 +0000
@@ -36,6 +36,8 @@ body<=
     # see what search mode...
     my $su = LJ::load_user( $POST{mode} || $GET{user} );
     my $q = LJ::strip_html( LJ::trim( $POST{query} ) );
+    my $sby = $POST{sort_by} || 'new';
+    $sby = 'new' unless $sby =~ /^(?:new|old|rel)$/;
 
     # helper sub for returning the search form
     my $search_form = sub {
@@ -50,8 +52,16 @@ body<=
                                       value => $tu->user, label => "Journal Search: <strong>" . $tu->user . "</strong>", noescape => 1 } );
         }
 
-        $ret .= '<br /><input type="text" name="query" maxlength="255" size="60" value="' . LJ::ehtml( $q ) .
-                '"> <input type="submit" value="Search" /></form>';
+        $ret .= '<br /><input type="text" name="query" maxlength="255" size="60" value="' . LJ::ehtml( $q ) . '">';
+        $ret .= ' <input type="submit" value="Search" /><br />';
+        $ret .= "Sort results by: ";
+        $ret .= LJ::html_select(
+            { selected => $sby, name => 'sort_by' },
+            new => "Date, newest posts first",
+            old => "Date, oldest posts first",
+            rel => "Relevance to search terms",
+        );
+        $ret .= '</form>';
 
         return $ret;
     };
@@ -114,7 +124,7 @@ body<=
     }
 
     # the arguments to the search (userid=0 implies global search)
-    my $args = { userid => $su ? $su->id : 0, query => $q, offset => $offset,
+    my $args = { userid => $su ? $su->id : 0, query => $q, offset => $offset, sort_by => $sby,
                  ignore_security => $ignore_security, allowmask => $allowmask };
     my $arg = Storable::nfreeze( $args );
 
--------------------------------------------------------------------------------

Post a comment in response:

This account has disabled anonymous posting.
If you don't have an account you can create one now.
HTML doesn't work in the subject.
More info about formatting

If you are unable to use this captcha for any reason, please contact us by email at support@dreamwidth.org