| 1 | package XTaTIK::Model::ProductSearch; | ||
| 2 | |||
| 3 | our $VERSION = '0.005003'; # VERSION | ||
| 4 | |||
| 5 | 1 | ▶ | use Mojo::Base -base; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use Search::Indexer; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use Mojo::DOM; | ||
| 8 | use List::UtilsBy qw/nsort_by/; | ||
| 9 | use experimental 'postderef'; | ||
| 10 | |||
| 11 | has 'dir'; | ||
| 12 | sub add { | ||
| 13 | my ( $self, $id, $keywords ) = @_; | ||
| 14 | |||
| 15 | $keywords = eval { Mojo::DOM->new($keywords)->all_text } // $keywords; | ||
| 16 | |||
| 17 | my @keywords; | ||
| 18 | for ( $keywords =~ /[-\w]+/g ) { | ||
| 19 | while ( length ) { | ||
| 20 | push @keywords, $_; | ||
| 21 | $_ = substr $_, 0, length($_)-1; | ||
| 22 | } | ||
| 23 | } | ||
| 24 | |||
| 25 | Search::Indexer->new( dir => $self->dir, writeMode => 1 ) | ||
| 26 | ->add( $id, join ' ', @keywords ); | ||
| 27 | |||
| 28 | return 1; | ||
| 29 | } | ||
| 30 | |||
| 31 | sub delete { | ||
| 32 | my ( $self, $id ) = @_; | ||
| 33 | |||
| 34 | Search::Indexer->new( dir => $self->dir, writeMode => 1 ) | ||
| 35 | ->remove( $id ); | ||
| 36 | |||
| 37 | return $self; | ||
| 38 | } | ||
| 39 | |||
| 40 | sub search { | ||
| 41 | my ( $self, $term ) = @_; | ||
| 42 | |||
| 43 | my $res = Search::Indexer->new( dir => $self->dir )->search( $term ); | ||
| 44 | return nsort_by { $res->{scores}->{$_} } keys $res->{scores}->%*; | ||
| 45 | } | ||
| 46 | |||
| 47 | 1; | ||
| 48 | |||
| 49 | __END__ | ||