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