when searching for device always prefer matching on device to alias
This commit is contained in:
@@ -126,7 +126,7 @@ sub search_aliases {
|
|||||||
# this helps us avoid triggering any DNS.
|
# this helps us avoid triggering any DNS.
|
||||||
my $by_ip = ($q =~ m{^(?:$RE{net}{IPv4}|$RE{net}{IPv6})(?:/\d+)?$}i) ? 1 : 0;
|
my $by_ip = ($q =~ m{^(?:$RE{net}{IPv4}|$RE{net}{IPv6})(?:/\d+)?$}i) ? 1 : 0;
|
||||||
|
|
||||||
my $clause;
|
my ($clause, $sorter);
|
||||||
if ($by_ip) {
|
if ($by_ip) {
|
||||||
my $ip = NetAddr::IP::Lite->new($q)
|
my $ip = NetAddr::IP::Lite->new($q)
|
||||||
or return undef; # could be a MAC address!
|
or return undef; # could be a MAC address!
|
||||||
@@ -134,6 +134,7 @@ sub search_aliases {
|
|||||||
'me.ip' => { '<<=' => $ip->cidr },
|
'me.ip' => { '<<=' => $ip->cidr },
|
||||||
'device_ips.alias' => { '<<=' => $ip->cidr },
|
'device_ips.alias' => { '<<=' => $ip->cidr },
|
||||||
];
|
];
|
||||||
|
$sorter = \[q{CASE WHEN (me.ip <<= ?) THEN 1 ELSE 0 END}, $ip->cidr];
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
$q = "\%$q\%" if ($options->{partial} and $q !~ m/\%/);
|
$q = "\%$q\%" if ($options->{partial} and $q !~ m/\%/);
|
||||||
@@ -142,6 +143,7 @@ sub search_aliases {
|
|||||||
'me.dns' => { '-ilike' => $q },
|
'me.dns' => { '-ilike' => $q },
|
||||||
'device_ips.dns' => { '-ilike' => $q },
|
'device_ips.dns' => { '-ilike' => $q },
|
||||||
];
|
];
|
||||||
|
$sorter = \[q{CASE WHEN (me.name ILIKE ? OR me.dns ILIKE ?) THEN 1 ELSE 0 END}, $q, $q];
|
||||||
}
|
}
|
||||||
|
|
||||||
return $rs->search(
|
return $rs->search(
|
||||||
@@ -149,7 +151,9 @@ sub search_aliases {
|
|||||||
-or => $clause,
|
-or => $clause,
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
order_by => [qw/ me.dns me.ip /],
|
'+select' => [ { coalesce => $sorter, -as => 'in_device' } ],
|
||||||
|
order_by => [{ -desc => 'in_device' }, { -asc => [qw/ me.dns me.ip /] } ],
|
||||||
|
group_by => ['me.ip'],
|
||||||
join => 'device_ips',
|
join => 'device_ips',
|
||||||
distinct => 1,
|
distinct => 1,
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user