From 419f3ae9fd57fc4e03146a830f7ed780ace83937 Mon Sep 17 00:00:00 2001 From: Dylan William Hardison Date: Sat, 19 Nov 2016 18:12:39 -0500 Subject: Bug 1307478 - Elasticsearch Indexer / Bulk Indexer --- Bugzilla/User.pm | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) (limited to 'Bugzilla/User.pm') diff --git a/Bugzilla/User.pm b/Bugzilla/User.pm index 0eb9587eb..69885f57c 100644 --- a/Bugzilla/User.pm +++ b/Bugzilla/User.pm @@ -30,6 +30,7 @@ use Scalar::Util qw(blessed); use Storable qw(dclone); use URI; use URI::QueryParam; +use Role::Tiny::With; use base qw(Bugzilla::Object Exporter); @Bugzilla::User::EXPORT = qw(is_available_username @@ -123,6 +124,81 @@ use constant VALIDATOR_DEPENDENCIES => { use constant EXTRA_REQUIRED_FIELDS => qw(is_enabled); +with 'Bugzilla::Elastic::Role::Object'; + +sub ES_TYPE { 'user' } + +sub ES_OBJECTS_AT_ONCE { 2000 } + +sub ES_SELECT_UPDATED_SQL { + my ($class, $mtime) = @_; + + my $sql = q{ + SELECT DISTINCT + object_id + FROM + audit_log + WHERE + class = 'Bugzilla::User' AND at_time > FROM_UNIXTIME(?) + }; + return ($sql, [$mtime]); +} + +sub ES_SELECT_ALL_SQL { + my ($class, $last_id) = @_; + + my $id = $class->ID_FIELD; + my $table = $class->DB_TABLE; + + return ("SELECT $id FROM $table WHERE $id > ? AND is_enabled ORDER BY $id", [$last_id // 0]); +} + +sub ES_PROPERTIES { + return { + suggest_user => { + type => 'completion', + analyzer => 'folding', + search_analyzer => 'folding', + payloads => \1, + }, + suggest_nick => { + type => 'completion', + analyzer => 'simple', + search_analyzer => 'simple', + payloads => \1, + }, + login => { type => 'string' }, + name => { type => 'string' }, + is_enabled => { type => 'boolean' }, + }; +} + +sub es_document { + my ( $self, $timestamp ) = @_; + my $weight = eval { $self->last_activity_ts ? datetime_from($self->last_activity_ts)->epoch : 0 } // 0; + my $doc = { + login => $self->login, + name => $self->name, + is_enabled => $self->is_enabled, + suggest_user => { + input => [ $self->login, $self->name ], + output => $self->identity, + payload => { name => $self->login, real_name => $self->name }, + weight => $weight, + }, + }; + if ($self->name && $self->name =~ /:(\w+)/) { + my $ircnick = $1; + $doc->{suggest_nick} = { + input => [ $ircnick ], + output => $self->login, + payload => { name => $self->login, real_name => $self->name, ircnick => $ircnick }, + weight => $weight, + }; + } + + return $doc; +} ################################################################################ # Functions ################################################################################ -- cgit v1.2.3-24-g4f1b