blob: a4ba64b44708b7db4fd71eea2502166ecb019258 [file] [log] [blame]
Nils Diewald8f4b5da2014-12-03 22:13:39 +00001package Korap::Controller::Search;
2use Mojo::Base 'Mojolicious::Controller';
3
4# Add X-Forwarded-For to user agent call everywhere
5
6
7# Query the KorAP backends and render a template
8sub query {
9 my $c = shift;
10 $c->layout('default');
11 $c->title('KorAP');
12
13 my $query = $c->param('q');
14
15 # Base parameters for remote access
16 my %param = (
17 query_language => scalar $c->param('ql'),
18 query => $query,
19 );
20
21 my $inspect = (scalar $c->param('action') // '') eq 'inspect' ? 1 : 0;
22
23 # Just check the serialization non-blocking
24 if ($inspect) {
25 $c->search->trace(
26 %param => sub {
27 return $c->render(template => 'query_info');
28 }
29 );
30 return;
31 };
32
33 my $template = scalar $c->param('snippet') ? 'snippet' : 'search';
34
35 # Search non-blocking
36 $c->delay(
37 sub {
38 my $delay = shift;
39 $c->search(
40 cutoff => scalar $c->param('cutoff'),
41 count => scalar $c->param('count'),
Nils Diewald034ea702015-01-16 19:41:52 +000042 start_page => scalar $c->param('p'),
Nils Diewald8f4b5da2014-12-03 22:13:39 +000043 cb => $delay->begin,
44 %param
45 ) if $query;
46
47 $c->search->resource(
48 type => 'collection',
49 $delay->begin
50 );
51 },
52 sub {
53 return $c->render(template => $template);
54 }
55 );
56};
57
58
59# Get informations about a match
60sub match_info {
61 my $c = shift;
62
63 my $foundry = '*';
64 my %query = (foundry => '*');
65 if ($c->param('foundry')) {
66 $query{foundry} = $c->param('foundry');
67 if ($c->param('layer')) {
68 $query{layer} = $c->param('layer');
69 };
70 if ($c->param('spans')) {
71 $query{spans} = 'true';
72 };
73 };
74
75 $c->render_later;
76
77 # Use the API for fetching matching information non-blocking
78 $c->search->match(
79 corpus_id => $c->stash('corpus_id'),
80 doc_id => $c->stash('doc_id'),
81 match_id => $c->stash('match_id'),
82 %query,
83
84 # Callback for async search
85 sub {
86 my $index = shift;
Nils Diewald8f4b5da2014-12-03 22:13:39 +000087 return $c->respond_to(
88
89 # Render json if requested
90 json => sub {
91 # Add notifications to the matching json
92 # TODO: There should be a special notification engine doing that!
93 my $notes = $c->notifications(json => $index->results->[0]);
94 $c->render(json => $notes);
95 },
96
97 # Render html if requested
98 html => sub {
99 return $c->render(
100 layout => 'default',
101 template => 'match_info'
102 )
103 }
104 );
105 }
106 );
107};
108
1091;
110
111
112__END__