Skip to content

Commit

Permalink
added suggester to fix autocomplete
Browse files Browse the repository at this point in the history
  • Loading branch information
mickeyn committed Dec 5, 2016
1 parent 399132e commit f4c7e13
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 34 deletions.
27 changes: 27 additions & 0 deletions lib/MetaCPAN/Document/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,33 @@ sub _build_documentation {
return undef;
}

=head2 suggest
Autocomplete suggester data.
=cut

has suggest => (
is => 'ro',
lazy => 1,
builder => '_build_suggest',
);

sub _build_suggest {
my $self = shift;
my $doc = $self->documentation;
return unless $doc;

my $weight = 1000 - length($doc);
$weight = 0 if $weight < 0;

return {
input => [$doc],
payload => { doc_name => $doc },
weight => $weight
};
}

=head2 indexed
B<Default 0>
Expand Down
65 changes: 42 additions & 23 deletions lib/MetaCPAN/Document/File/Set.pm
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package MetaCPAN::Document::File::Set;

use Moose;

use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

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

my @ROGUE_DISTRIBUTIONS = qw(
Expand Down Expand Up @@ -442,37 +444,54 @@ sub autocomplete {
my $query = join( q{ }, @terms );
return $self unless $query;

return $self->search_type('dfs_query_then_fetch')->query(
my $suggestions
= $self->search_type('dfs_query_then_fetch')->es->suggest(
{
filtered => {
query => {
multi_match => {
query => $query,
type => 'most_fields',
fields => [ 'documentation', 'documentation.*' ],
analyzer => 'camelcase',
minimum_should_match => '80%'
},
},
index => $self->index->name,
body => {
documentation => {
text => $query,
completion => {
field => "suggest",
size => 50,
}
}
},
}
);

my @docs
= map { $_->{text} } @{ $suggestions->{documentation}[0]{options} };

my $data = $self->es->search(
{
index => $self->index->name,
type => 'file',
body => {
query => { match_all => {} },
filter => {
bool => {
must => [
{ exists => { field => 'documentation' } },
{ term => { status => 'latest' } },
{ term => { indexed => 1 } },
{ term => { authorized => 1 } }
],
must_not => [
{
terms =>
{ distribution => \@ROGUE_DISTRIBUTIONS }
},
{ term => { indexed => 1 } },
{ term => { authorized => 1 } },
{ term => { status => 'latest' } },
{ terms => { 'documentation.raw' => \@docs } },
],
}
}
}
},
fields => ['documentation'],
size => 10
}
)->sort( [ '_score', 'documentation' ] );
);

return +{
suggestions => [
sort { length($a) <=> length($b) || $a cmp $b }
map { $_->{fields}{documentation}[0] }
@{ $data->{hits}{hits} }
]
};
}

__PACKAGE__->meta->make_immutable;
Expand Down
14 changes: 14 additions & 0 deletions lib/MetaCPAN/Script/Mapping/Cpan/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ sub mapping {
'{
"dynamic" : false,
"properties" : {
"suggest": {
"type" : "completion",
"analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : true
},
"pod" : {
"index" : "no",
"fields" : {
Expand Down Expand Up @@ -82,6 +88,10 @@ sub mapping {
"store" : true,
"type" : "string",
"analyzer" : "camelcase"
},
"raw" : {
"type" : "string",
"index" : "not_analyzed"
}
},
"type" : "string"
Expand Down Expand Up @@ -131,6 +141,10 @@ sub mapping {
"store" : true,
"type" : "string",
"analyzer" : "camelcase"
},
"raw" : {
"type" : "string",
"index" : "not_analyzed"
}
},
"type" : "string"
Expand Down
3 changes: 2 additions & 1 deletion lib/MetaCPAN/Script/Release.pm
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,8 @@ sub import_archive {
push( @provides, $_->name ) if $_->indexed && $_->authorized;
}
$file->clear_module if ( $file->is_pod_file );
$file->documentation;
$file->documentation; # force build
$file->suggest; # force build
log_trace {"reindexing file $file->{path}"};
$bulk->put($file);
if ( !$document->has_abstract && $file->abstract ) {
Expand Down
8 changes: 1 addition & 7 deletions lib/MetaCPAN/Server/Controller/Search/Autocomplete.pm
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ use strict;
use warnings;

use Moose;
use MetaCPAN::Util qw( single_valued_arrayref_to_scalar );

BEGIN { extends 'MetaCPAN::Server::Controller' }

Expand All @@ -17,12 +16,7 @@ sub get : Local : Path('') : Args(0) {
my $model = $self->model($c);
$model = $model->fields( [qw(documentation release author distribution)] )
unless $model->fields;
my $data
= $model->autocomplete( $c->req->param("q") )->source(0)->raw->all;

single_valued_arrayref_to_scalar( $_->{fields} )
for @{ $data->{hits}{hits} };

my $data = $model->autocomplete( $c->req->param("q") );
$c->stash($data);
}

Expand Down
4 changes: 1 addition & 3 deletions t/server/controller/search/autocomplete.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ test_psgi app, sub {
ok( my $res = $cb->( GET '/search/autocomplete?q=Multiple::Modu' ),
'GET' );
my $json = decode_json_ok($res);

my $got = [ map { $_->{fields}{documentation} }
@{ $json->{hits}{hits} } ];
my $got = $json->{suggestions};

is_deeply $got, [
qw(
Expand Down

0 comments on commit f4c7e13

Please sign in to comment.