Skip to content

Commit

Permalink
see #233, prepare user search: makes retrieval much faster
Browse files Browse the repository at this point in the history
  • Loading branch information
vpeil committed Mar 7, 2018
1 parent 89dd19e commit 1c1de39
Show file tree
Hide file tree
Showing 7 changed files with 61 additions and 15 deletions.
6 changes: 6 additions & 0 deletions config/schemas.yml
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,12 @@ user:
type: string
pattern: '\d{4}-[01]\d-[0-3]\dT[0-2]\d:[0-5]\d:[0-5]\d([+-][0-2]\d:[0-5]\d|Z)'
description: "The user last modification date"
show:
oneOf:
- type: string
pattern: "^[01]$"
- type: integer
description: "Flag if person should be displayed on /person list"

required: ["_id","account_status","first_name","last_name","full_name"]
additionalProperties: true
Expand Down
5 changes: 5 additions & 0 deletions config/store.yml
Original file line number Diff line number Diff line change
Expand Up @@ -756,6 +756,11 @@ search:
'all': true
'exact': true
field: alias
show:
op:
'=': true
'<>': true
field: show

department:
cql_mapping:
Expand Down
4 changes: 4 additions & 0 deletions devel/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ full_name: User, Test
last_name: User
login: test
password: secret
show: 1
style: apa
...
---
Expand Down Expand Up @@ -52,6 +53,7 @@ full_name: Dean, James
last_name: Dean
login: james
password: secret
show: 1
style: apa
...
---
Expand All @@ -70,6 +72,7 @@ old_name:
full_name: 'Mortenson, Norma Jeane'
last_name: Mortenson
password: secret
show: 1
style: apa
...
---
Expand All @@ -87,5 +90,6 @@ old_name:
last_name: Hershlag
login: natalie
password: secret
show: 1
style: apa
...
16 changes: 2 additions & 14 deletions lib/LibreCat/App/Search/Route/person.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ get qr{/person} => sub {
my $c = params->{browse} // 'a';

my %search_params = (
cql => ["lastname=" . lc $c . "*"],
cql => ["show=1 AND lastname=" . lc $c . "*"],
sort => h->config->{default_person_sort},
start => 0,
limit => 1000
Expand All @@ -31,18 +31,6 @@ get qr{/person} => sub {

my $hits = LibreCat->searcher->search('user', \%search_params);

@{$hits->{hits}} = map {
my $rec = $_;
my $pub = LibreCat->searcher->search('publication',
{cql => ["person=$rec->{_id}"], start => 0, limit => 1,});
($pub->{total} > 0) ? $rec : undef;
} @{$hits->{hits}};

@{$hits->{hits}} = grep defined, @{$hits->{hits}};

# override the total number since we deleted some entries
$hits->{total} = scalar @{$hits->{hits}};

template 'person/list', $hits;
};

Expand All @@ -56,7 +44,7 @@ research data and author IDs.
get qr{/person/(.*?)/?(data)*} => sub {
my ($id, $modus) = splat;

# Redirect to the alias if the other can't be found
# Redirect to the alias if the ID cannot be found
h->log->debug("trying to find user $id");
unless (my $user = h->main_user->get($id)) {
h->log->debug("trying to find user alias $id");
Expand Down
29 changes: 28 additions & 1 deletion lib/LibreCat/Cmd/user.pm
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package LibreCat::Cmd::user;

use Catmandu::Sane;
use LibreCat;
use LibreCat::App::Helper;
use LibreCat::Validator::User;
use App::bmkpasswd qw(passwdcmp mkpasswd);
Expand All @@ -19,6 +20,7 @@ librecat user [options] get <id> | <IDFILE>
librecat user [options] delete <id> | <IDFILE>
librecat user [options] valid <FILE>
librecat user [options] passwd <id>
librecat user [options] prepare_search
options:
--sort=STR (sorting results [only in combination with cql-query])
Expand Down Expand Up @@ -47,7 +49,7 @@ sub command {

$self->opts($opts);

my $commands = qr/list|export|get|add|delete|valid|passwd/;
my $commands = qr/list|export|get|add|delete|valid|passwd|prepare_search/;

unless (@$args) {
$self->usage_error("should be one of $commands");
Expand Down Expand Up @@ -90,6 +92,9 @@ sub command {
elsif ($cmd eq 'passwd') {
return $self->_passwd(@$args);
}
elsif ($cmd eq 'prepare_search') {
return $self->_prepare_search(@$args);
}
}

sub _on_all {
Expand Down Expand Up @@ -342,6 +347,27 @@ sub _passwd {
return 0;
}

sub _prepare_search {
my ($self) = @_;

my $helper = LibreCat::App::Helper::Helpers->new;
my $index = $helper->user;

my $records = $index->select(sub {
my $rec = $_[0];
my $pub = LibreCat->searcher->search('publication', {cql => ["person=$rec->{_id}"], start => 0, limit => 1});
if ($pub->{total} > 0) {
$rec->{show} = 1;
}
return 1;
});

$index->add_many($records);
$index->commit;

return 0;
}

1;

__END__
Expand All @@ -361,6 +387,7 @@ LibreCat::Cmd::user - manage librecat users
librecat user delete <id> | <IDFILE>
librecat user valid <FILE>
librecat user passwd <id>
librecat user prepare_search
options:
--sort=STR (sorting results [only in combination with cql-query])
Expand Down
9 changes: 9 additions & 0 deletions t/LibreCat/Cmd/user.t
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,15 @@ Catmandu->store('search')->bag('user')->delete_all;
ok length($output) == 0, 'got no result';
}

{
my $result = test_app(qq|LibreCat::CLI| => ['user', 'prepare_search']);

ok !$result->error, 'ok no exception';

my $output = $result->stdout;
ok length($output) == 0, 'got no output';
}

done_testing;

sub count_user {
Expand Down
7 changes: 7 additions & 0 deletions t/www/person.t
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ subtest 'person overview page' => sub {
subtest 'person list alphabetical index' => sub {
$mech->get_ok('/person?browse=a');
$mech->get_ok('/person?browse=A');


$mech->get_ok('/person?browse=E');
$mech->content_unlike(qr/Einstein,, Albert/);

$mech->get_ok('/person?browse=P');
$mech->content_like(qr/Portman, Natalie/);
$mech->content_unlike(qr/Presley, Elvis/);
};

subtest 'person profile with single digit id' => sub {
Expand Down

0 comments on commit 1c1de39

Please sign in to comment.