burguillos.info/lib/BurguillosInfo/Controller/Search.pm

45 lines
1.2 KiB
Perl
Raw Normal View History

2023-09-04 12:54:38 +02:00
package BurguillosInfo::Controller::Search;
use v5.34.1;
use strict;
use warnings;
use Data::Dumper;
use Mojo::Base 'Mojolicious::Controller', '-signatures';
use Mojo::UserAgent;
2023-09-04 17:23:29 +02:00
use BurguillosInfo::IndexUtils;
my $index_utils = BurguillosInfo::IndexUtils->new;
2023-09-04 12:54:38 +02:00
sub search ($self) {
my $ua = Mojo::UserAgent->new;
my $query = $self->param('q');
my $config = $self->config;
my $search_backend = $config->{search_backend};
my $search_index = $config->{search_index};
2023-09-04 17:23:29 +02:00
$query =~ s/\btitle:/titleNormalized:/g;
$query =~ s/\bcontent:/contentNormalized:/g;
2023-09-04 12:54:38 +02:00
my $tx = $ua->get( $search_backend . '/search/' . $search_index,
2023-09-04 17:23:29 +02:00
{}, form => { q => $index_utils->n($query) } );
2023-09-04 12:54:38 +02:00
my $result = $tx->result;
my $output = $result->json;
if ( !defined $output ) {
return $self->render( status => 500, json => { ok => 0 } );
}
my $ok = $output->{ok};
my $reason = $output->{reason};
if ( !$ok ) {
return $self->render( status => 400, json => { ok => 0 } );
}
my $searchObjects = $output->{searchObjects};
return $self->render(
status => 200,
json => { ok => 1, searchObjects => $searchObjects }
);
}
1;