Last touches to the search.

This commit is contained in:
Sergiotarxz 2023-09-04 17:23:29 +02:00
parent 37b26db84b
commit 8d2eb989e5
2 changed files with 48 additions and 23 deletions

View File

@ -17,6 +17,9 @@ use Mojo::UserAgent;
use BurguillosInfo::Posts;
use BurguillosInfo::Categories;
use BurguillosInfo::IndexUtils;
my $index_utils = BurguillosInfo::IndexUtils->new;
sub run ( $self, @args ) {
require BurguillosInfo;
@ -42,15 +45,20 @@ sub _index_categories ( $self, $index, $categories ) {
my $slug = $category->{slug};
my $url = "/$slug";
my $content =
Mojo::DOM->new( '<html>' . $category->{description} =~ s/\s+/ /gr . '</html>' )->all_text;
Mojo::DOM->new(
'<html>' . $category->{description} =~ s/\s+/ /gr . '</html>' )
->all_text;
my $title = $category->{title};
my $attributes = $category->{attributes};
$self->_index_attributes( $index, $slug, $attributes );
push @$index,
{
title => $title,
titleNormalized => $index_utils->n($title),
content => $content,
contentNormalized => $index_utils->n( $content =~ s/\s+/ /gr ),
url => $url,
urlNormalized => $index_utils->n($url),
};
}
}
@ -62,10 +70,16 @@ sub _index_attributes($self, $index, $category_slug, $attributes) {
my $slug = $attribute->{identifier};
my $url = "/$category_slug/atributo/$slug";
my $title = $attribute->{title};
my $content = Mojo::DOM->new( '<html>' . $attribute->{description} . '</html>' )->all_text;
push @$index, {
my $content =
Mojo::DOM->new( '<html>' . $attribute->{description} . '</html>' )
->all_text;
push @$index,
{
titleNormalized => $index_utils->n($title),
title => $title,
contentNormalized => $index_utils->n( $content =~ s/\s+/ /gr ),
content => $content =~ s/\s+/ /gr,
urlNormalized => $index_utils->n($url),
url => $url,
};
}
@ -84,10 +98,15 @@ sub _index_posts ( $self, $index, $posts ) {
my $author = $post->{author};
push @$index,
{
titleNormalized => $index_utils->n($title),
title => $title,
authorNormalized => $index_utils->n($author),
author => $author,
contentNormalized => $index_utils->n( $content =~ s/\s+/ /gr ),
content => $content =~ s/\s+/ /gr,
urlNormalized => $index_utils->n($url),
url => $url,
urlImageNormalized => $index_utils->n($urlImage),
urlImage => $urlImage,
};
}

View File

@ -10,14 +10,20 @@ use Data::Dumper;
use Mojo::Base 'Mojolicious::Controller', '-signatures';
use Mojo::UserAgent;
use BurguillosInfo::IndexUtils;
my $index_utils = BurguillosInfo::IndexUtils->new;
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};
$query =~ s/\btitle:/titleNormalized:/g;
$query =~ s/\bcontent:/contentNormalized:/g;
my $tx = $ua->get( $search_backend . '/search/' . $search_index,
{}, form => { q => $query } );
{}, form => { q => $index_utils->n($query) } );
my $result = $tx->result;
my $output = $result->json;