2023-09-03 21:16:25 +02:00
|
|
|
package BurguillosInfo::Command::index;
|
|
|
|
|
|
|
|
use v5.36.0;
|
|
|
|
|
|
|
|
use strict;
|
|
|
|
use warnings;
|
|
|
|
use utf8;
|
|
|
|
|
|
|
|
use feature 'signatures';
|
|
|
|
|
|
|
|
use Data::Dumper;
|
|
|
|
|
|
|
|
use Mojo::Base 'Mojolicious::Command';
|
|
|
|
use Moo;
|
|
|
|
|
|
|
|
use Mojo::UserAgent;
|
|
|
|
|
|
|
|
use BurguillosInfo::Posts;
|
|
|
|
use BurguillosInfo::Categories;
|
2023-09-04 17:23:29 +02:00
|
|
|
use BurguillosInfo::IndexUtils;
|
|
|
|
|
|
|
|
my $index_utils = BurguillosInfo::IndexUtils->new;
|
2023-09-03 21:16:25 +02:00
|
|
|
|
|
|
|
sub run ( $self, @args ) {
|
|
|
|
require BurguillosInfo;
|
|
|
|
my $app = BurguillosInfo->new;
|
|
|
|
my $config = $app->config;
|
|
|
|
my $search_backend = $config->{search_backend};
|
|
|
|
my $search_index = $config->{search_index};
|
|
|
|
my $ua = Mojo::UserAgent->new;
|
2023-09-18 23:11:04 +02:00
|
|
|
my $posts = BurguillosInfo::Posts->new->Retrieve(0);
|
2023-09-03 21:16:25 +02:00
|
|
|
my $categories = BurguillosInfo::Categories->new->Retrieve;
|
|
|
|
my $index = [];
|
|
|
|
$self->_index_posts( $index, $posts );
|
|
|
|
$self->_index_categories( $index, $categories );
|
2023-09-04 12:54:38 +02:00
|
|
|
my $response = $ua->put( $search_backend . '/index/' . $search_index,
|
2023-09-03 21:16:25 +02:00
|
|
|
{} => json => $index );
|
2023-09-04 12:54:38 +02:00
|
|
|
say $response->result->body;
|
2023-09-03 21:16:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
sub _index_categories ( $self, $index, $categories ) {
|
|
|
|
my @categories_keys = keys %$categories;
|
|
|
|
for my $category_key (@categories_keys) {
|
2023-09-04 17:23:29 +02:00
|
|
|
my $category = $categories->{$category_key};
|
2023-09-03 21:16:25 +02:00
|
|
|
my $slug = $category->{slug};
|
|
|
|
my $url = "/$slug";
|
|
|
|
my $content =
|
2023-09-04 17:23:29 +02:00
|
|
|
Mojo::DOM->new(
|
|
|
|
'<html>' . $category->{description} =~ s/\s+/ /gr . '</html>' )
|
|
|
|
->all_text;
|
|
|
|
my $title = $category->{title};
|
2023-09-03 21:16:25 +02:00
|
|
|
my $attributes = $category->{attributes};
|
2023-09-17 04:22:28 +02:00
|
|
|
my $image = $category->{image};
|
2023-09-04 17:23:29 +02:00
|
|
|
$self->_index_attributes( $index, $slug, $attributes );
|
2023-09-17 04:22:28 +02:00
|
|
|
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),
|
|
|
|
(
|
|
|
|
( defined $image )
|
|
|
|
? ( urlImage => $image )
|
|
|
|
: ()
|
|
|
|
)
|
|
|
|
|
|
|
|
};
|
2023-09-03 21:16:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-04 17:23:29 +02:00
|
|
|
sub _index_attributes ( $self, $index, $category_slug, $attributes ) {
|
2023-09-03 21:16:25 +02:00
|
|
|
my @attributes_keys = keys %$attributes;
|
|
|
|
for my $attribute_key (@attributes_keys) {
|
|
|
|
my $attribute = $attributes->{$attribute_key};
|
2023-09-04 17:23:29 +02:00
|
|
|
my $slug = $attribute->{identifier};
|
|
|
|
my $url = "/$category_slug/atributo/$slug";
|
|
|
|
my $title = $attribute->{title};
|
2023-09-17 04:22:28 +02:00
|
|
|
my $image = $attribute->{image};
|
2023-09-04 17:23:29 +02:00
|
|
|
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,
|
2023-09-17 04:22:28 +02:00
|
|
|
(
|
|
|
|
( defined $image )
|
|
|
|
? ( urlImage => $image )
|
|
|
|
: ()
|
|
|
|
)
|
2023-09-04 17:23:29 +02:00
|
|
|
};
|
2023-09-03 21:16:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sub _index_posts ( $self, $index, $posts ) {
|
|
|
|
my @posts_keys = keys %$posts;
|
|
|
|
for my $post_key (@posts_keys) {
|
|
|
|
my $post = $posts->{$post_key};
|
|
|
|
my $slug = $post->{slug};
|
|
|
|
my $url = "/posts/$slug";
|
|
|
|
my $urlImage = $post->{image};
|
|
|
|
my $content =
|
|
|
|
Mojo::DOM->new( '<html>' . $post->{content} . '</html>' )->all_text;
|
|
|
|
my $title = $post->{title};
|
|
|
|
my $author = $post->{author};
|
|
|
|
push @$index,
|
|
|
|
{
|
2023-09-04 17:23:29 +02:00
|
|
|
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,
|
2023-09-03 21:16:25 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
1;
|