Improving search for automated postings in the future.

This commit is contained in:
Sergiotarxz 2023-09-18 23:11:04 +02:00
parent 4c5ba3166a
commit 91c8155205
3 changed files with 29 additions and 14 deletions

View File

@ -28,7 +28,7 @@ sub run ( $self, @args ) {
my $search_backend = $config->{search_backend};
my $search_index = $config->{search_index};
my $ua = Mojo::UserAgent->new;
my $posts = BurguillosInfo::Posts->new->Retrieve;
my $posts = BurguillosInfo::Posts->new->Retrieve(0);
my $categories = BurguillosInfo::Categories->new->Retrieve;
my $index = [];
$self->_index_posts( $index, $posts );

View File

@ -11,6 +11,7 @@ use Mojo::Base 'Mojolicious::Controller', '-signatures';
use Mojo::UserAgent;
use BurguillosInfo::IndexUtils;
use BurguillosInfo::Posts;
my $index_utils = BurguillosInfo::IndexUtils->new;
@ -36,9 +37,23 @@ sub search ($self) {
return $self->render( status => 400, json => { ok => 0 } );
}
my $searchObjects = $output->{searchObjects};
$searchObjects = [grep { $self->filterSearch($_) } @$searchObjects];
return $self->render(
status => 200,
json => { ok => 1, searchObjects => $searchObjects }
);
}
sub filterSearch($self, $searchObject) {
my $url = $searchObject->{url};
my ($posts_by_categories, $posts) = BurguillosInfo::Posts->Retrieve;
my $slug;
if ($url =~ m{^/posts/([^/]+?)(?:\?.*)?$}) {
$slug = $1;
if (!defined $posts->{$slug}) {
return 0;
}
}
return 1
}
1;

View File

@ -32,21 +32,22 @@ sub new {
return bless {}, shift;
}
sub _ReturnCacheFilter {
my $self = shift;
sub _ReturnCacheFilter($self, $filters = 1) {
my %posts_by_category_filtered;
my %posts_by_slug_filtered;
my $iso8601 = DateTime::Format::ISO8601->new;
my $current_date = DateTime->now;
for my $category ( keys %$cached_posts_by_category ) {
for my $post ( @{ $cached_posts_by_category->{$category} } ) {
my $date_post;
eval { $date_post = $iso8601->parse_datetime( $post->{date} ); };
if ($@) {
print Data::Dumper::Dumper $post;
}
if ( $date_post > $current_date ) {
next;
if ($filters) {
my $date_post;
eval { $date_post = $iso8601->parse_datetime( $post->{date} ); };
if ($@) {
print $@ . ': ' . Data::Dumper::Dumper $post;
}
if ( $date_post > $current_date ) {
next;
}
}
$posts_by_slug_filtered{ $post->{slug} } = $post;
$posts_by_category_filtered{ $post->{category} } //= [];
@ -155,13 +156,12 @@ sub _GeneratePostCache ($self) {
}
}
sub Retrieve {
my $self = shift;
sub Retrieve($self, $filters = 1) {
if ( defined $cached_posts_by_category && defined $cached_posts_by_slug ) {
return $self->_ReturnCacheFilter;
return $self->_ReturnCacheFilter($filters);
}
$self->_GeneratePostCache();
return $self->_ReturnCacheFilter;
return $self->_ReturnCacheFilter($filters);
}
my $cache_all_post_categories = {};