From f711047a33227855e39326cebd4fdcfd8c24494e Mon Sep 17 00:00:00 2001 From: Oliver Gorwits Date: Wed, 22 Jan 2014 17:58:14 +0000 Subject: [PATCH] tighten up the logic of fuzzy search to eliminate redundant matches --- .../lib/App/Netdisco/DB/ResultSet/Device.pm | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm b/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm index f0c8bd69..c7474acf 100644 --- a/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm +++ b/Netdisco/lib/App/Netdisco/DB/ResultSet/Device.pm @@ -279,19 +279,23 @@ sub search_fuzzy { unless $q; $q = "\%$q\%" if $q !~ m/\%/; - # basic IP check is a string match - my $ip_clause = [ - 'me.ip::text' => { '-ilike' => $q }, + # basic check is a string match + my @ip_clause = ( + 'me.name' => { '-ilike' => $q }, + 'me.dns' => { '-ilike' => $q }, + 'me.ip::text' => { '-ilike' => $q }, + 'device_ips.dns' => { '-ilike' => $q }, 'device_ips.alias::text' => { '-ilike' => $q }, - ]; + ); # but also allow prefix search (my $qc = $q) =~ s/\%//g; if (my $ip = NetAddr::IP::Lite->new($qc)) { - $ip_clause = [ + @ip_clause = ( + # exclude check on name and dns fields 'me.ip' => { '<<=' => $ip->cidr }, 'device_ips.alias' => { '<<=' => $ip->cidr }, - ]; + ); } return $rs->search( @@ -300,17 +304,12 @@ sub search_fuzzy { 'me.contact' => { '-ilike' => $q }, 'me.serial' => { '-ilike' => $q }, 'me.location' => { '-ilike' => $q }, - 'me.name' => { '-ilike' => $q }, 'me.description' => { '-ilike' => $q }, - -or => [ - 'me.dns' => { '-ilike' => $q }, - 'device_ips.dns' => { '-ilike' => $q }, - ], - -or => $ip_clause, + @ip_clause, ], }, { - order_by => [qw/ me.dns me.ip /], + order_by => { -desc => 'me.creation' }, join => 'device_ips', distinct => 1, }