« OpenInteract2/Manage/Website/CreateSuperuserPassword.pm
↑ Summary
OpenInteract2/Setup/InitializeSPOPS.pm »
| 1 | package OpenInteract2::FullTextIndexer; | ||
| 2 | |||
| 3 | # $Id: FullTextIndexer.pm,v 1.2 2005/03/18 04:09:48 lachoy Exp $ | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use base qw( Class::Accessor::Fast ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | 1 | ▶ | use Log::Log4perl qw( get_logger ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use OpenInteract2::Constants qw( :log ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use OpenInteract2::Exception qw( oi_error ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 0 | ▶ | use OpenInteract2::FullTextIterator; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | |||
| 12 | $OpenInteract2::FullTextIndexer::VERSION = sprintf("%d.%02d", q$Revision: 1.2 $ =~ /(\d+)\.(\d+)/); | ||
| 13 | |||
| 14 | my @FIELDS = qw( min_word_length max_word_length empty_message ); | ||
| 15 | __PACKAGE__->mk_accessors( @FIELDS ); | ||
| 16 | |||
| 17 | my ( $log ); | ||
| 18 | |||
| 19 | sub new { | ||
| 20 | my ( $class, $params ) = @_; | ||
| 21 | $log ||= get_logger( LOG_APP ); | ||
| 22 | my $self = bless( {}, $class ); | ||
| 23 | for my $field ( @FIELDS ) { | ||
| 24 | $self->$field( $params->{ $field } ) if ( $params->{ $field } ); | ||
| 25 | } | ||
| 26 | $log->is_info && | ||
| 27 | $log->info( "Creating new instance of indexer '$class'" ); | ||
| 28 | $self->init( $params ); | ||
| 29 | return $self; | ||
| 30 | } | ||
| 31 | |||
| 32 | |||
| 33 | ######################################## | ||
| 34 | # INTERFACE METHODS | ||
| 35 | |||
| 36 | # Optional for subclasses to implement | ||
| 37 | |||
| 38 | sub init {} | ||
| 39 | |||
| 40 | # Throws exception if failure indexing content, otherwise returns | ||
| 41 | # nothing | ||
| 42 | |||
| 43 | sub add_to_index { | ||
| 44 | my ( $self, $content_class, $content_id, $content_ref ) = @_; | ||
| 45 | oi_error ref( $self ), " must implement add_to_index()"; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub refresh_index { | ||
| 49 | my ( $self, $content_class, $content_id, $content_ref ) = @_; | ||
| 50 | $self->remove_from_index( $content_class, $content_id ); | ||
| 51 | $self->add_to_index( $content_class, $content_id, $content_ref ); | ||
| 52 | } | ||
| 53 | |||
| 54 | sub remove_from_index { | ||
| 55 | my ( $self, $content_class, $content_id ) = @_; | ||
| 56 | oi_error ref( $self ), " must implement remove_from_index()"; | ||
| 57 | } | ||
| 58 | |||
| 59 | # Return value depends on 'return_type': | ||
| 60 | # 'raw': arrayref of arrayrefs: | ||
| 61 | # [ class, id, full-score, { term1 => num-occurrences, | ||
| 62 | # term2 => num-occurrences... } ] | ||
| 63 | # | ||
| 64 | # 'object': arrayref of arrayrefs: | ||
| 65 | # [ object, sum-of-occurrences | ||
| 66 | # | ||
| 67 | # 'iterator': SPOPS::Iterator subclass (OpenInteract2::FullTextIterator) | ||
| 68 | |||
| 69 | # $search_type = 'all' or 'any' | ||
| 70 | # @terms = terms to search for | ||
| 71 | |||
| 72 | sub search_index { | ||
| 73 | my ( $self, $params ) = @_; | ||
| 74 | my $search_type = lc $params->{search_type} || 'all'; | ||
| 75 | my @terms = @{ $params->{terms} }; | ||
| 76 | my $results = $self->_run_search( $search_type, @terms ); | ||
| 77 | |||
| 78 | $results = $self->_screen_results( $search_type, $results, @terms ); | ||
| 79 | |||
| 80 | my $return_type = $params->{return_type} || 'object'; | ||
| 81 | if ( $return_type eq 'object' ) { | ||
| 82 | my @objects = (); | ||
| 83 | foreach my $result ( @{ $results } ) { | ||
| 84 | my $class = $result->[0]; | ||
| 85 | my $id = $result->[1]; | ||
| 86 | my $object = eval { $class->fetch( $id ) }; | ||
| 87 | if ( $@ ) { | ||
| 88 | $log->error( "Failed to fetch object [$class: $id]: $@", | ||
| 89 | "Continuing to serialize search results..." ); | ||
| 90 | } | ||
| 91 | else { | ||
| 92 | push @objects, [ $object, $result->[2] ]; | ||
| 93 | } | ||
| 94 | } | ||
| 95 | return \@objects; | ||
| 96 | } | ||
| 97 | elsif ( $return_type eq 'iterator' ) { | ||
| 98 | return OpenInteract2::FullTextIterator->new({ | ||
| 99 | results => $results, | ||
| 100 | min => $params->{min}, | ||
| 101 | max => $params->{max}, | ||
| 102 | }); | ||
| 103 | } | ||
| 104 | elsif ( $return_type eq 'raw' ) { | ||
| 105 | return $results; | ||
| 106 | } | ||
| 107 | else { | ||
| 108 | oi_error "Do not know how to process return type '$return_type'"; | ||
| 109 | } | ||
| 110 | } | ||
| 111 | |||
| 112 | sub _run_search { | ||
| 113 | my ( $self, $search_type, @terms ) = @_; | ||
| 114 | oi_error ref( $self ), " must implement _run_search()"; | ||
| 115 | } | ||
| 116 | |||
| 117 | # Remove results that do not belong in the resultset -- currently this | ||
| 118 | # only screens out results in an 'AND' search that don't have all the | ||
| 119 | # terms found | ||
| 120 | |||
| 121 | sub _screen_results { | ||
| 122 | my ( $self, $search_type, $results, @search_terms ) = @_; | ||
| 123 | |||
| 124 | # If this was an AND search, knock off all the results that didn't | ||
| 125 | # have matches for all the terms | ||
| 126 | |||
| 127 | if ( lc $search_type eq 'all' ) { | ||
| 128 | my $num_terms = scalar @search_terms; | ||
| 129 | |||
| 130 | $log->is_info && | ||
| 131 | $log->info( "Screening results to ensure all terms (", | ||
| 132 | join( ', ', @search_terms ), ") are matched" ); | ||
| 133 | |||
| 134 | my @kept = (); | ||
| 135 | |||
| 136 | foreach my $result ( @{ $results } ) { | ||
| 137 | my $num_matches = scalar( keys %{ $result->[3] } ); | ||
| 138 | $log->is_debug && | ||
| 139 | $log->debug( "Result [$result->[0]: $result->[1]] has ", | ||
| 140 | "$num_matches matches" ); | ||
| 141 | if ( $num_matches >= $num_terms ) { | ||
| 142 | push @kept, $result; | ||
| 143 | } | ||
| 144 | } | ||
| 145 | |||
| 146 | my $num_removed = scalar @{ $results } - scalar @kept; | ||
| 147 | $log->is_debug && | ||
| 148 | $log->debug( "Removed '$num_removed' items from the list ", | ||
| 149 | "since they didn't match all the terms" ); | ||
| 150 | $results = \@kept; | ||
| 151 | } | ||
| 152 | return $results; | ||
| 153 | } | ||
| 154 | |||
| 155 | 1; | ||
| 156 | |||
| 157 | __END__ | ||