Skip to content
This repository has been archived by the owner on Jul 11, 2020. It is now read-only.

Commit

Permalink
search-backend: import current version
Browse files Browse the repository at this point in the history
  • Loading branch information
florolf committed Jan 6, 2015
1 parent 1f0811c commit 90e7abc
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 30 deletions.
13 changes: 12 additions & 1 deletion server/media-import.pl
Expand Up @@ -4,7 +4,7 @@
use strict;
use warnings;

use local::lib ("$ENV{HOME}/.perl5");
use local::lib;

use LWP::Simple;
use Search::Elasticsearch;
Expand All @@ -15,6 +15,14 @@
WEBGEN_PREFIX => "http://media.ccc.de/browse/",
};

$| = 1;

my $verbose = 0;
if(defined $ARGV[0] and $ARGV[0] eq '-v') {
shift @ARGV;
$verbose = 1;
}

my $raw;

if($ARGV[0]) {
Expand Down Expand Up @@ -66,8 +74,11 @@ sub get_cached {
acronym => $conference->{acronym},

frontend_link => WEBGEN_PREFIX . $conference->{webgen_location},
logo => $conference->{logo_url} // "",
url => $event->{conference_url}
}
}
) or die "insert failed: $@";

print "." if $verbose;
}
129 changes: 100 additions & 29 deletions server/search.pl
Expand Up @@ -4,56 +4,127 @@
use strict;
use warnings;

use local::lib ("$ENV{HOME}/.perl5");
use local::lib;

use Mojolicious::Lite;
use Search::Elasticsearch;
use Data::Dumper;

plugin JSONP => callback => 'callback';

my $e = Search::Elasticsearch->new();

sub dosearch {
my $query = shift;

my $ret = $e->search(
index => 'media',
type => 'event',
body => {
query => $query,
@_
}
);
}

get '/:string' => sub {
sub term_search {
my $c = shift;
my $str = $c->param('string');

my $ret = dosearch({
bool => {
should => [
{match => { 'event.title' => { query => $str, fuzziness => "AUTO"}}},
{match => { 'event.description' => {query => $str, fuzziness => "AUTO"}}},
{match => { 'event.persons' => {query => $str, fuzziness => "AUTO"}}},
{match => { 'conference.title' => {query => $str, fuzziness => "AUTO"}}},
{match => { 'conference.acronym' => {query => $str, fuzziness => "AUTO"}}},
]
}
});


$c->render(json => $ret);
my $str = lc($c->param('term') // "");
my $page = $c->param('displayPage') // 0;
my $per_page = $c->param('perPage') // 10;

my $ret = dosearch(
query => {
function_score => {
query => {
bool => {
disable_coord => 1,
should => [
{
multi_match => {
query => $str,
fields => [
'event.title^4',
'event.subtitle^3',
'event.persons^3',
'conference.acronym^2',
'conference.title^2',
'event.description^1'
],
type => 'best_fields',
operator => 'or',
fuzziness => 1
},
},
{
prefix => {
'event.title' => {
value => $str,
boost => 12
}
}
},
{
prefix => {
'event.subtitle' => {
value => $str,
boost => 3
}
}
},
{
prefix => {
'conference.acronym' => {
value => $str,
boost => 2
}
}
},
{
prefix => {
'conference.persons' => {
value => $str,
boost => 1
}
}
}
]
}
},
boost => 1.2,
functions => [
{
"gauss" => {
"event.date" => {
"scale" => "96w",
"decay" => 0.5
}
}
}
]
}
},
from => $page * $per_page,
size => $per_page
);

$c->render_jsonp($ret);
};

get '/persons/:string' => sub {
my $c = shift;
my $str = $c->param('string');
post '/term' => \&term_search;
get '/term' => \&term_search;

my $ret = dosearch({
match => { 'event.persons' => {query => $str, fuzziness => 'AUTO'}}
});
app->hook(before_dispatch => sub {
my $c = shift;
$c->req->url->base(Mojo::URL->new(q{http://koeln.media.ccc.de/search/api/}));

# remove the "/search/api"-prefix
shift @{$c->req->url->path};
shift @{$c->req->url->path};

$c->render(json => $ret);
};
$c->res->headers->header( 'Access-Control-Allow-Origin' => '*' );
$c->res->headers->header( 'Access-Control-Allow-Methods' => 'GET, POST, OPTIONS' );
$c->res->headers->header( 'Access-Control-Max-Age' => '3600' );
});

app->types->type(json => "application/json; charset=utf-8");
app->types->type(html => "text/html; charset=utf-8");

app->start;

0 comments on commit 90e7abc

Please sign in to comment.