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::Posts;
use BurguillosInfo::Categories; use BurguillosInfo::Categories;
use BurguillosInfo::IndexUtils;
my $index_utils = BurguillosInfo::IndexUtils->new;
sub run ( $self, @args ) { sub run ( $self, @args ) {
require BurguillosInfo; require BurguillosInfo;
@ -38,36 +41,47 @@ sub run ( $self, @args ) {
sub _index_categories ( $self, $index, $categories ) { sub _index_categories ( $self, $index, $categories ) {
my @categories_keys = keys %$categories; my @categories_keys = keys %$categories;
for my $category_key (@categories_keys) { for my $category_key (@categories_keys) {
my $category = $categories->{$category_key}; my $category = $categories->{$category_key};
my $slug = $category->{slug}; my $slug = $category->{slug};
my $url = "/$slug"; my $url = "/$slug";
my $content = my $content =
Mojo::DOM->new( '<html>' . $category->{description} =~ s/\s+/ /gr . '</html>' )->all_text; Mojo::DOM->new(
my $title = $category->{title}; '<html>' . $category->{description} =~ s/\s+/ /gr . '</html>' )
->all_text;
my $title = $category->{title};
my $attributes = $category->{attributes}; my $attributes = $category->{attributes};
$self->_index_attributes($index, $slug, $attributes); $self->_index_attributes( $index, $slug, $attributes );
push @$index, push @$index,
{ {
title => $title, title => $title,
content => $content, titleNormalized => $index_utils->n($title),
url => $url, content => $content,
contentNormalized => $index_utils->n( $content =~ s/\s+/ /gr ),
url => $url,
urlNormalized => $index_utils->n($url),
}; };
} }
} }
sub _index_attributes($self, $index, $category_slug, $attributes) { sub _index_attributes ( $self, $index, $category_slug, $attributes ) {
my @attributes_keys = keys %$attributes; my @attributes_keys = keys %$attributes;
for my $attribute_key (@attributes_keys) { for my $attribute_key (@attributes_keys) {
my $attribute = $attributes->{$attribute_key}; my $attribute = $attributes->{$attribute_key};
my $slug = $attribute->{identifier}; my $slug = $attribute->{identifier};
my $url = "/$category_slug/atributo/$slug"; my $url = "/$category_slug/atributo/$slug";
my $title = $attribute->{title}; my $title = $attribute->{title};
my $content = Mojo::DOM->new( '<html>' . $attribute->{description} . '</html>' )->all_text; my $content =
push @$index, { Mojo::DOM->new( '<html>' . $attribute->{description} . '</html>' )
title => $title, ->all_text;
content => $content =~ s/\s+/ /gr, push @$index,
url => $url, {
}; 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,11 +98,16 @@ sub _index_posts ( $self, $index, $posts ) {
my $author = $post->{author}; my $author = $post->{author};
push @$index, push @$index,
{ {
title => $title, titleNormalized => $index_utils->n($title),
author => $author, title => $title,
content => $content =~ s/\s+/ /gr, authorNormalized => $index_utils->n($author),
url => $url, author => $author,
urlImage => $urlImage, 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::Base 'Mojolicious::Controller', '-signatures';
use Mojo::UserAgent; use Mojo::UserAgent;
use BurguillosInfo::IndexUtils;
my $index_utils = BurguillosInfo::IndexUtils->new;
sub search ($self) { sub search ($self) {
my $ua = Mojo::UserAgent->new; my $ua = Mojo::UserAgent->new;
my $query = $self->param('q'); my $query = $self->param('q');
my $config = $self->config; my $config = $self->config;
my $search_backend = $config->{search_backend}; my $search_backend = $config->{search_backend};
my $search_index = $config->{search_index}; 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, my $tx = $ua->get( $search_backend . '/search/' . $search_index,
{}, form => { q => $query } ); {}, form => { q => $index_utils->n($query) } );
my $result = $tx->result; my $result = $tx->result;
my $output = $result->json; my $output = $result->json;