Skip to content

Commit

Permalink
Moved all Document::Set packages to their own files
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeyn committed Jul 11, 2017
1 parent 3f6ffa5 commit 800165f
Show file tree
Hide file tree
Showing 18 changed files with 1,552 additions and 1,551 deletions.
112 changes: 0 additions & 112 deletions lib/MetaCPAN/Document/Author.pm
Expand Up @@ -145,118 +145,6 @@ sub validate {
return @result;
}

__PACKAGE__->meta->make_immutable;

package MetaCPAN::Document::Author::Set;

use strict;
use warnings;

use Moose;
extends 'ElasticSearchX::Model::Document::Set';

use Ref::Util qw( is_arrayref );

use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

sub by_ids {
my ( $self, $ids ) = @_;

map {uc} @{$ids};

my $body = {
query => {
constant_score => {
filter => { ids => { values => $ids } }
}
},
size => scalar @{$ids},
};

my $authors = $self->es->search(
index => $self->index->name,
type => 'author',
body => $body,
);
return {} unless $authors->{hits}{total};

my @authors = map {
single_valued_arrayref_to_scalar( $_->{_source} );
$_->{_source}
} @{ $authors->{hits}{hits} };

return { authors => \@authors };
}

sub by_user {
my ( $self, $users ) = @_;
$users = [$users] unless is_arrayref($users);

my $authors = $self->es->search(
index => $self->index->name,
type => 'author',
body => {
query => { terms => { user => $users } },
size => 100,
}
);
return {} unless $authors->{hits}{total};

my @authors = map {
single_valued_arrayref_to_scalar( $_->{_source} );
$_->{_source}
} @{ $authors->{hits}{hits} };

return { authors => \@authors };
}

sub search {
my ( $self, $query, $from ) = @_;

my $body = {
query => {
bool => {
should => [
{
match => {
'name.analyzed' =>
{ query => $query, operator => 'and' }
}
},
{
match => {
'asciiname.analyzed' =>
{ query => $query, operator => 'and' }
}
},
{ match => { 'pauseid' => uc($query) } },
{ match => { 'profile.id' => lc($query) } },
]
}
},
size => 10,
from => $from || 0,
};

my $ret = $self->es->search(
index => $self->index->name,
type => 'author',
body => $body,
);
return {} unless $ret->{hits}{total};

my @authors = map {
single_valued_arrayref_to_scalar( $_->{_source} );
+{ %{ $_->{_source} }, id => $_->{_id} }
} @{ $ret->{hits}{hits} };

return +{
authors => \@authors,
took => $ret->{took},
total => $ret->{hits}{total},
};
}

__PACKAGE__->meta->make_immutable;
1;

Expand Down
112 changes: 112 additions & 0 deletions lib/MetaCPAN/Document/Author/Set.pm
@@ -0,0 +1,112 @@
package MetaCPAN::Document::Author::Set;

use strict;
use warnings;

use Moose;
extends 'ElasticSearchX::Model::Document::Set';

use Ref::Util qw( is_arrayref );

use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

sub by_ids {
my ( $self, $ids ) = @_;

map {uc} @{$ids};

my $body = {
query => {
constant_score => {
filter => { ids => { values => $ids } }
}
},
size => scalar @{$ids},
};

my $authors = $self->es->search(
index => $self->index->name,
type => 'author',
body => $body,
);
return {} unless $authors->{hits}{total};

my @authors = map {
single_valued_arrayref_to_scalar( $_->{_source} );
$_->{_source}
} @{ $authors->{hits}{hits} };

return { authors => \@authors };
}

sub by_user {
my ( $self, $users ) = @_;
$users = [$users] unless is_arrayref($users);

my $authors = $self->es->search(
index => $self->index->name,
type => 'author',
body => {
query => { terms => { user => $users } },
size => 100,
}
);
return {} unless $authors->{hits}{total};

my @authors = map {
single_valued_arrayref_to_scalar( $_->{_source} );
$_->{_source}
} @{ $authors->{hits}{hits} };

return { authors => \@authors };
}

sub search {
my ( $self, $query, $from ) = @_;

my $body = {
query => {
bool => {
should => [
{
match => {
'name.analyzed' =>
{ query => $query, operator => 'and' }
}
},
{
match => {
'asciiname.analyzed' =>
{ query => $query, operator => 'and' }
}
},
{ match => { 'pauseid' => uc($query) } },
{ match => { 'profile.id' => lc($query) } },
]
}
},
size => 10,
from => $from || 0,
};

my $ret = $self->es->search(
index => $self->index->name,
type => 'author',
body => $body,
);
return {} unless $ret->{hits}{total};

my @authors = map {
single_valued_arrayref_to_scalar( $_->{_source} );
+{ %{ $_->{_source} }, id => $_->{_id} }
} @{ $ret->{hits}{hits} };

return +{
authors => \@authors,
took => $ret->{took},
total => $ret->{hits}{total},
};
}

__PACKAGE__->meta->make_immutable;
1;
57 changes: 0 additions & 57 deletions lib/MetaCPAN/Document/Contributor.pm
Expand Up @@ -30,61 +30,4 @@ has pauseid => (
);

__PACKAGE__->meta->make_immutable;

package MetaCPAN::Document::Contributor::Set;

use strict;
use warnings;

use Moose;

extends 'ElasticSearchX::Model::Document::Set';

sub find_release_contributors {
my ( $self, $author, $name ) = @_;

my $query = +{
bool => {
must => [
{ term => { release_author => $author } },
{ term => { release_name => $name } },
]
}
};

my $res = $self->es->search(
index => 'contributor',
type => 'contributor',
body => {
query => $query,
size => 999,
}
);
$res->{hits}{total} or return {};

return +{
contributors => [ map { $_->{_source} } @{ $res->{hits}{hits} } ]
};
}

sub find_author_contributions {
my ( $self, $pauseid ) = @_;

my $query = +{ term => { pauseid => $pauseid } };

my $res = $self->es->search(
index => 'contributor',
type => 'contributor',
body => {
query => $query,
size => 999,
}
);
$res->{hits}{total} or return {};

return +{
contributors => [ map { $_->{_source} } @{ $res->{hits}{hits} } ]
};
}

1;
58 changes: 58 additions & 0 deletions lib/MetaCPAN/Document/Contributor/Set.pm
@@ -0,0 +1,58 @@
package MetaCPAN::Document::Contributor::Set;

use strict;
use warnings;

use Moose;

extends 'ElasticSearchX::Model::Document::Set';

sub find_release_contributors {
my ( $self, $author, $name ) = @_;

my $query = +{
bool => {
must => [
{ term => { release_author => $author } },
{ term => { release_name => $name } },
]
}
};

my $res = $self->es->search(
index => 'contributor',
type => 'contributor',
body => {
query => $query,
size => 999,
}
);
$res->{hits}{total} or return {};

return +{
contributors => [ map { $_->{_source} } @{ $res->{hits}{hits} } ]
};
}

sub find_author_contributions {
my ( $self, $pauseid ) = @_;

my $query = +{ term => { pauseid => $pauseid } };

my $res = $self->es->search(
index => 'contributor',
type => 'contributor',
body => {
query => $query,
size => 999,
}
);
$res->{hits}{total} or return {};

return +{
contributors => [ map { $_->{_source} } @{ $res->{hits}{hits} } ]
};
}

__PACKAGE__->meta->make_immutable;
1;

0 comments on commit 800165f

Please sign in to comment.