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_backend = $config->{search_backend};
my $search_index = $config->{search_index}; my $search_index = $config->{search_index};
my $ua = Mojo::UserAgent->new; 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 $categories = BurguillosInfo::Categories->new->Retrieve;
my $index = []; my $index = [];
$self->_index_posts( $index, $posts ); $self->_index_posts( $index, $posts );

View File

@ -11,6 +11,7 @@ use Mojo::Base 'Mojolicious::Controller', '-signatures';
use Mojo::UserAgent; use Mojo::UserAgent;
use BurguillosInfo::IndexUtils; use BurguillosInfo::IndexUtils;
use BurguillosInfo::Posts;
my $index_utils = BurguillosInfo::IndexUtils->new; my $index_utils = BurguillosInfo::IndexUtils->new;
@ -36,9 +37,23 @@ sub search ($self) {
return $self->render( status => 400, json => { ok => 0 } ); return $self->render( status => 400, json => { ok => 0 } );
} }
my $searchObjects = $output->{searchObjects}; my $searchObjects = $output->{searchObjects};
$searchObjects = [grep { $self->filterSearch($_) } @$searchObjects];
return $self->render( return $self->render(
status => 200, status => 200,
json => { ok => 1, searchObjects => $searchObjects } 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; 1;

View File

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