Bug 4340 - patron name search does not handle punctuated names
authorJ. David Bavousett <dbavousett@ptfs.com>
Tue, 5 Apr 2011 14:26:41 +0000 (16:26 +0200)
committerChris Cormack <chrisc@catalyst.net.nz>
Fri, 8 Apr 2011 02:11:11 +0000 (14:11 +1200)
This patch will enable C4::Members::SearchMember to handle searching for punctuated
names (e.g. the "Jones" in "Smith-Jones" or the "Angelo" in "D'Angelo")

It is possible to add a bunch of LIKE clauses, but that adds to the search time in
a rather dramatic way.  REGEXP, by itself, is also a performance killer, but I found
a suggestion of using a more-general LIKE ANDed with the specific REGEXP, and this
gives excellent performance over other approaches.

Signed-off-by: Guillaume Hatt <guillaume.hatt@enc.sorbonne.fr>
Signed-off-by: Chris Cormack <chrisc@catalyst.net.nz>
C4/Members.pm

index bf73e0e..f0ac280 100644 (file)
@@ -213,22 +213,28 @@ sub SearchMember {
             $query.=" borrowers.branchcode =".$dbh->quote(C4::Context->userenv->{'branch'})." AND " unless (C4::Context->userenv->{'branch'} eq "insecure");
           }      
         }     
-        $query.="((surname LIKE ? OR surname LIKE ?
-                OR firstname  LIKE ? OR firstname LIKE ?
-                OR othernames LIKE ? OR othernames LIKE ?)
+        $query.="((surname LIKE ? OR (surname LIKE ? AND surname REGEXP ?)
+                OR firstname  LIKE ? OR (firstname LIKE ? AND firstname REGEXP ?)
+                OR othernames LIKE ? OR (othernames LIKE ? AND othernames REGEXP ?))
         " .
         ($category_type?" AND category_type = ".$dbh->quote($category_type):"");
+        my $regex = '[[:punct:][:space:]]'.$data[0];
         @bind = (
-            "$data[0]%", "% $data[0]%", "$data[0]%", "% $data[0]%",
-            "$data[0]%", "% $data[0]%"
+            "$data[0]%", "%$data[0]%", $regex, 
+            "$data[0]%", "%$data[0]%", $regex, 
+            "$data[0]%", "%$data[0]%", $regex 
         );
         for ( my $i = 1 ; $i < $count ; $i++ ) {
-            $query = $query . " AND (" . " surname LIKE ? OR surname LIKE ?
-                OR firstname  LIKE ? OR firstname LIKE ?
-                OR othernames LIKE ? OR othernames LIKE ?)";
+            $query = $query . " AND (" . " surname LIKE ? OR (surname LIKE ? AND surname REGEXP ?)
+                OR firstname  LIKE ? OR (firstname LIKE ? AND firstname REGEXP ?)
+                OR othernames LIKE ? OR (othernames LIKE ? AND othernames REGEXP ?))";
+            $regex = '[[:punct:][:space:]]'.$data[$i];
             push( @bind,
-                "$data[$i]%",   "% $data[$i]%", "$data[$i]%",
-                "% $data[$i]%", "$data[$i]%",   "% $data[$i]%" );
+              "$data[$i]%", "%$data[$i]%", $regex,
+              "$data[$i]%", "%$data[$i]%", $regex,
+              "$data[$i]%", "%$data[$i]%", $regex
+            );
+
 
             # FIXME - .= <<EOT;
         }