Short circuit empty searches user/dbs/avoid_empty_searches
authorDan Scott <dscott@laurentian.ca>
Mon, 24 Jun 2013 15:41:32 +0000 (11:41 -0400)
committerDan Scott <dscott@laurentian.ca>
Mon, 24 Jun 2013 15:54:36 +0000 (11:54 -0400)
If we have no search terms, we shouldn't kick off a search. Accordingly,
track whether we have, in fact, any type + value combos where value is
not an empty string, or a non-empty original query, and return
immediately if we have nothing to search.

Compare the results of the following opensrf call before and after:

request open-ils.search open-ils.search.biblio.multiclass.query {}
"title: "

Signed-off-by: Dan Scott <dscott@laurentian.ca>
Open-ILS/src/perlmods/lib/OpenILS/Application/Search/Biblio.pm

index 2068128..eef7694 100644 (file)
@@ -854,6 +854,7 @@ sub multiclass_query {
     my $modifier_list_re = qr/(?:site|dir|sort|lang|available|preflib)/;
 
     my $tmp_value = '';
+    my $nonempty_search = 0; # prevent entirely empty queries
     while ($query =~ s/$simple_class_re//so) {
 
         my $qpart = $1;
@@ -871,10 +872,13 @@ sub multiclass_query {
             $tmp_value = '';
         }
 
-        next unless $type and $value;
+        next unless $type;
 
         $value =~ s/^\s*//og;
         $value =~ s/\s*$//og;
+
+        next unless $value;
+
         $type = 'sort_dir' if $type eq 'dir';
 
         if($type eq 'site') {
@@ -915,6 +919,7 @@ sub multiclass_query {
             $search->{$type}->{term} =  
                 ($search->{$type}->{term}) ? $search->{$type}->{term} . " $value" : $value;
         }
+        $nonempty_search = 1;
     }
 
     $query .= " $tmp_value";
@@ -933,7 +938,11 @@ sub multiclass_query {
         $search->{$type} =  {} unless $search->{$type};
         $search->{$type}->{term} =
             ($search->{$type}->{term}) ? $search->{$type}->{term} . " $query" : $query;
+        $nonempty_search = 1;
     }
+
+    return unless $nonempty_search;
+
     my $real_search = $arghash->{searches} = { $type => { term => $orig_query } };
 
     # capture the original limit because the search method alters the limit internally