Skip to content

Commit

Permalink
Merge pull request #1296 from metacpan/haarg/esconfig
Browse files Browse the repository at this point in the history
ESConfig: configurable Elasticsearch document types to allow splitting index
  • Loading branch information
haarg authored Oct 28, 2024
2 parents 6974280 + d6571ec commit 2a7a80b
Show file tree
Hide file tree
Showing 108 changed files with 979 additions and 957 deletions.
3 changes: 3 additions & 0 deletions .perlcriticrc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ theme = core
[-ValuesAndExpressions::ProhibitNoisyQuotes]
[-Variables::ProhibitPunctuationVars]

# doesn't understand signatures
[-Subroutines::ProhibitSubroutinePrototypes]

[CodeLayout::RequireTrailingCommas]
severity = 4

Expand Down
25 changes: 4 additions & 21 deletions lib/Catalyst/Plugin/Session/Store/ElasticSearch.pm
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use Moose;
extends 'Catalyst::Plugin::Session::Store';
use MooseX::Types::ElasticSearch qw( ES );

use MetaCPAN::ESConfig qw( es_doc_path );
use MetaCPAN::Server::Config ();
use MetaCPAN::Util qw( true false );

Expand All @@ -17,26 +18,12 @@ has _session_es => (
default =>
sub { MetaCPAN::Server::Config::config()->{elasticsearch_servers} },
);
has _session_es_index => (
required => 1,
is => 'ro',
default => sub { shift->_session_plugin_config->{index} || 'user' }
);
has _session_es_type => (
required => 1,
is => 'ro',
default => sub { shift->_session_plugin_config->{type} || 'session' }
);

sub get_session_data {
my ( $self, $key ) = @_;
if ( my ($sid) = $key =~ /^\w+:(.*)/ ) {
my $data = eval {
$self->_session_es->get(
index => $self->_session_es_index,
type => $self->_session_es_type,
id => $sid,
);
$self->_session_es->get( es_doc_path('session'), id => $sid, );
} || return undef;
if ( $key =~ /^expires:/ ) {
return $data->{_source}->{_expires};
Expand All @@ -52,8 +39,7 @@ sub store_session_data {
if ( my ($sid) = $key =~ /^session:(.*)/ ) {
$session->{_expires} = $self->session_expires;
$self->_session_es->index(
index => $self->_session_es_index,
type => $self->_session_es_type,
es_doc_path('session'),
id => $sid,
body => $session,
refresh => true,
Expand All @@ -66,8 +52,7 @@ sub delete_session_data {
if ( my ($sid) = $key =~ /^session:(.*)/ ) {
eval {
$self->_session_es->delete(
index => $self->_session_es_index,
type => $self->_session_es_type,
es_doc_path('session'),
id => $sid,
refresh => true,
);
Expand All @@ -93,8 +78,6 @@ sub delete_expired_sessions { }
MyApp->config(
'Plugin::Session' => {
servers => ':9200',
index => 'user',
type => 'session',
} );
=head1 DESCRIPTION
Expand Down
6 changes: 3 additions & 3 deletions lib/MetaCPAN/API/Model/Cover.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package MetaCPAN::API::Model::Cover;

use MetaCPAN::ESConfig qw( es_doc_path );
use MetaCPAN::Moose;

use MetaCPAN::Util qw(hit_total);
Expand All @@ -12,9 +13,8 @@ sub find_release_coverage {
my $query = +{ term => { release => $release } };

my $res = $self->_run_query(
index => 'cover',
type => 'cover',
body => {
es_doc_path('cover'),
body => {
query => $query,
size => 999,
}
Expand Down
4 changes: 2 additions & 2 deletions lib/MetaCPAN/API/Model/User.pm
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package MetaCPAN::API::Model::User;

use MetaCPAN::ESConfig qw( es_doc_path );
use MetaCPAN::Moose;

with 'MetaCPAN::API::Model::Role::ES';
Expand All @@ -17,8 +18,7 @@ sub lookup {
};

my $res = $self->_run_query(
index => 'user',
type => 'account',
es_doc_path('account'),
body => { query => $query },
search_type => 'dfs_query_then_fetch',
);
Expand Down
5 changes: 1 addition & 4 deletions lib/MetaCPAN/API/Plugin/Model.pm
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,7 @@ has download => sub {

has search => sub {
my $self = shift;
return MetaCPAN::Query::Search->new(
es => $self->app->es,
index_name => 'cpan',
);
return MetaCPAN::Query::Search->new( es => $self->app->es, );
};

has user => sub {
Expand Down
7 changes: 4 additions & 3 deletions lib/MetaCPAN/Document/Author.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ use ElasticSearchX::Model::Document::Types qw( Location );
use ElasticSearchX::Model::Document;

# load order not important
use Gravatar::URL ();
use MetaCPAN::Types qw( ESBool Profile );
use MetaCPAN::Types::TypeTiny qw(
use Gravatar::URL ();
use MetaCPAN::Document::Author::Profile ();
use MetaCPAN::Types qw( ESBool Profile );
use MetaCPAN::Types::TypeTiny qw(
ArrayRef
ArrayRefPromote
Blog
Expand Down
5 changes: 1 addition & 4 deletions lib/MetaCPAN/Document/Author/Set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ has query_author => (

sub _build_query_author {
my $self = shift;
return MetaCPAN::Query::Author->new(
es => $self->es,
index_name => $self->index->name,
);
return MetaCPAN::Query::Author->new( es => $self->es );
}

__PACKAGE__->meta->make_immutable;
Expand Down
5 changes: 1 addition & 4 deletions lib/MetaCPAN/Document/CVE/Set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ has query_cve => (

sub _build_query_cve {
my $self = shift;
return MetaCPAN::Query::CVE->new(
es => $self->es,
index_name => 'cve',
);
return MetaCPAN::Query::CVE->new( es => $self->es );
}

__PACKAGE__->meta->make_immutable;
Expand Down
5 changes: 1 addition & 4 deletions lib/MetaCPAN/Document/Contributor/Set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ has query_contributor => (

sub _build_query_contributor {
my $self = shift;
return MetaCPAN::Query::Contributor->new(
es => $self->es,
index_name => 'contributor',
);
return MetaCPAN::Query::Contributor->new( es => $self->es );
}

__PACKAGE__->meta->make_immutable;
Expand Down
5 changes: 1 addition & 4 deletions lib/MetaCPAN/Document/Cover/Set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ has query_cover => (

sub _build_query_cover {
my $self = shift;
return MetaCPAN::Query::Cover->new(
es => $self->es,
index_name => 'cover',
);
return MetaCPAN::Query::Cover->new( es => $self->es );
}

__PACKAGE__->meta->make_immutable;
Expand Down
2 changes: 1 addition & 1 deletion lib/MetaCPAN/Document/Distribution.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ has river => (

sub releases {
my $self = shift;
return $self->index->type("release")
return $self->index->model->doc("release")
->query( { term => { "distribution" => $self->name } } );
}

Expand Down
5 changes: 1 addition & 4 deletions lib/MetaCPAN/Document/Distribution/Set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ has query_distribution => (

sub _build_query_distribution {
my $self = shift;
return MetaCPAN::Query::Distribution->new(
es => $self->es,
index_name => 'cpan',
);
return MetaCPAN::Query::Distribution->new( es => $self->es );
}

__PACKAGE__->meta->make_immutable;
Expand Down
5 changes: 1 addition & 4 deletions lib/MetaCPAN/Document/Favorite/Set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ has query_favorite => (

sub _build_query_favorite {
my $self = shift;
return MetaCPAN::Query::Favorite->new(
es => $self->es,
index_name => $self->index->name,
);
return MetaCPAN::Query::Favorite->new( es => $self->es );
}

__PACKAGE__->meta->make_immutable;
Expand Down
Loading

0 comments on commit 2a7a80b

Please sign in to comment.