From 8d2eb989e567b6395abd1b3340a859b9bd808c6a Mon Sep 17 00:00:00 2001 From: Sergiotarxz Date: Mon, 4 Sep 2023 17:23:29 +0200 Subject: [PATCH] Last touches to the search. --- lib/BurguillosInfo/Command/index.pm | 63 ++++++++++++++++--------- lib/BurguillosInfo/Controller/Search.pm | 8 +++- 2 files changed, 48 insertions(+), 23 deletions(-) diff --git a/lib/BurguillosInfo/Command/index.pm b/lib/BurguillosInfo/Command/index.pm index f7ea8dc..3accfe8 100644 --- a/lib/BurguillosInfo/Command/index.pm +++ b/lib/BurguillosInfo/Command/index.pm @@ -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; @@ -38,36 +41,47 @@ sub run ( $self, @args ) { sub _index_categories ( $self, $index, $categories ) { my @categories_keys = keys %$categories; for my $category_key (@categories_keys) { - my $category = $categories->{$category_key}; + my $category = $categories->{$category_key}; my $slug = $category->{slug}; my $url = "/$slug"; my $content = - Mojo::DOM->new( '' . $category->{description} =~ s/\s+/ /gr . '' )->all_text; - my $title = $category->{title}; + Mojo::DOM->new( + '' . $category->{description} =~ s/\s+/ /gr . '' ) + ->all_text; + my $title = $category->{title}; my $attributes = $category->{attributes}; - $self->_index_attributes($index, $slug, $attributes); + $self->_index_attributes( $index, $slug, $attributes ); push @$index, { - title => $title, - content => $content, - url => $url, + title => $title, + titleNormalized => $index_utils->n($title), + 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; for my $attribute_key (@attributes_keys) { my $attribute = $attributes->{$attribute_key}; - my $slug = $attribute->{identifier}; - my $url = "/$category_slug/atributo/$slug"; - my $title = $attribute->{title}; - my $content = Mojo::DOM->new( '' . $attribute->{description} . '' )->all_text; - push @$index, { - title => $title, - content => $content =~ s/\s+/ /gr, - url => $url, - }; + my $slug = $attribute->{identifier}; + my $url = "/$category_slug/atributo/$slug"; + my $title = $attribute->{title}; + my $content = + Mojo::DOM->new( '' . $attribute->{description} . '' ) + ->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,11 +98,16 @@ sub _index_posts ( $self, $index, $posts ) { my $author = $post->{author}; push @$index, { - title => $title, - author => $author, - content => $content =~ s/\s+/ /gr, - url => $url, - urlImage => $urlImage, + 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, }; } } diff --git a/lib/BurguillosInfo/Controller/Search.pm b/lib/BurguillosInfo/Controller/Search.pm index 617e6c6..09a05ac 100644 --- a/lib/BurguillosInfo/Controller/Search.pm +++ b/lib/BurguillosInfo/Controller/Search.pm @@ -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;