| 1 | package File::Tabular::Web::Attachments::Indexed; | ||
| 2 | |||
| 3 | use base qw/File::Tabular::Web::Attachments/; | ||
| 4 | use strict; | ||
| 5 | use warnings; | ||
| 6 | no warnings 'uninitialized'; | ||
| 7 | |||
| 8 | use Search::Indexer; | ||
| 9 | use Search::QueryParser 0.92; | ||
| 10 | use locale; | ||
| 11 | |||
| 12 | #---------------------------------------------------------------------- | ||
| 13 | sub app_initialize { | ||
| 14 | #---------------------------------------------------------------------- | ||
| 15 | my $self = shift; | ||
| 16 | |||
| 17 | $self->SUPER::app_initialize; | ||
| 18 | |||
| 19 | # indexed fields are specified as "[fields]upload field=indexed" in config | ||
| 20 | my $upld_ref = $self->{app}{upload_fields}; | ||
| 21 | my @indexed = grep {$upld_ref->{$_} =~ /indexed/} values %$upld_ref; | ||
| 22 | |||
| 23 | @indexed < 2 or die "currently no support for multiple indexed fields"; | ||
| 24 | |||
| 25 | $self->{app}{indexed_field} = $indexed[0]; | ||
| 26 | } | ||
| 27 | |||
| 28 | |||
| 29 | #====================================================================== | ||
| 30 | # REQUEST HANDLING : SEARCH METHODS # | ||
| 31 | #====================================================================== | ||
| 32 | |||
| 33 | #---------------------------------------------------------------------- | ||
| 34 | sub words_queried { | ||
| 35 | #---------------------------------------------------------------------- | ||
| 36 | my $self = shift; | ||
| 37 | my $all_search = "$self->{search_string_orig} $self->{search_fulltext}"; | ||
| 38 | return ($all_search =~ m([\w/]+)g); | ||
| 39 | } | ||
| 40 | |||
| 41 | |||
| 42 | #---------------------------------------------------------------------- | ||
| 43 | sub log_search { | ||
| 44 | #---------------------------------------------------------------------- | ||
| 45 | my $self = shift; | ||
| 46 | return if not $self->{logger}; | ||
| 47 | |||
| 48 | my $msg = sprintf "[%s][%s] $self->{user}", | ||
| 49 | $self->{search_string_orig}, | ||
| 50 | $self->{search_fulltext}; | ||
| 51 | $self->{logger}->info($msg); | ||
| 52 | } | ||
| 53 | |||
| 54 | |||
| 55 | #---------------------------------------------------------------------- | ||
| 56 | sub before_search { | ||
| 57 | #---------------------------------------------------------------------- | ||
| 58 | my ($self) = @_; | ||
| 59 | |||
| 60 | $self->SUPER::before_search; | ||
| 61 | |||
| 62 | |||
| 63 | # searches into the fulltext index are passed through param 'SFT' | ||
| 64 | unless($self->{search_fulltext} = $self->param('SFT')) { | ||
| 65 | delete $self->{fulltext_result}; | ||
| 66 | return; | ||
| 67 | } | ||
| 68 | |||
| 69 | $self->{app}{indexer} ||= Search::Indexer->new( | ||
| 70 | dir => $self->{app}{dir}, | ||
| 71 | preMatch => $self->{cfg}->get('preMatch'), | ||
| 72 | postMatch => $self->{cfg}->get('postMatch'), | ||
| 73 | ); | ||
| 74 | |||
| 75 | my $result = $self->{app}{indexer} | ||
| 76 | ->search($self->{search_fulltext}, "implicit_plus"); | ||
| 77 | |||
| 78 | # MAYBE add some logging here (time for fulltext search, #docs found) | ||
| 79 | |||
| 80 | if ($result) { # nonempty results | ||
| 81 | $self->{results}{killedWords} = join ", ", @{$result->{killedWords}}; | ||
| 82 | $self->{results}{regex} = $result->{regex}; | ||
| 83 | |||
| 84 | # HACK : build a regex with all document ids, and add that into | ||
| 85 | # the search string. Not efficient if the result set is large; | ||
| 86 | # will require more clever handling in File::Tabular::compile_query | ||
| 87 | # using some kind of representation for sets of integers (bit vectors | ||
| 88 | # or Set::IntSpan::Fast) | ||
| 89 | # ASSUMES the document number is the record key, stored in first field | ||
| 90 | my $doc_ids = join ",", keys %{$result->{scores}} | ||
| 91 | or return; # no scores, no results | ||
| 92 | $self->{search_string} = "#$doc_ids"; | ||
| 93 | $self->{search_string} .= " AND ($self->{search_string_orig})" | ||
| 94 | if $self->{search_string_orig}; | ||
| 95 | } | ||
| 96 | $self->{fulltext_result} = $result; | ||
| 97 | |||
| 98 | return $self; | ||
| 99 | } | ||
| 100 | |||
| 101 | |||
| 102 | |||
| 103 | #---------------------------------------------------------------------- | ||
| 104 | sub search { # call parent search(), add fulltext scores into results | ||
| 105 | #---------------------------------------------------------------------- | ||
| 106 | my ($self, $search_string) = @_; | ||
| 107 | |||
| 108 | $self->SUPER::search($search_string); | ||
| 109 | |||
| 110 | my $fulltext_result = $self->{fulltext_result} or return; | ||
| 111 | |||
| 112 | # merge scores into results | ||
| 113 | $self->{data}->ht->add('score'); # new field for storing 'score' | ||
| 114 | |||
| 115 | foreach my $record (@{$self->{results}{records}}) { | ||
| 116 | my $doc_id = $self->key($record); | ||
| 117 | $record->{score} = $fulltext_result->{scores}{$doc_id}; | ||
| 118 | } | ||
| 119 | $self->{orderBy} ||= "score : -num"; # default sorting by decreasing scores | ||
| 120 | } | ||
| 121 | |||
| 122 | |||
| 123 | #---------------------------------------------------------------------- | ||
| 124 | sub sort_and_slice { | ||
| 125 | #---------------------------------------------------------------------- | ||
| 126 | my ($self) = @_; | ||
| 127 | |||
| 128 | $self->SUPER::sort_and_slice; | ||
| 129 | $self->add_excerpts; | ||
| 130 | } | ||
| 131 | |||
| 132 | |||
| 133 | |||
| 134 | #---------------------------------------------------------------------- | ||
| 135 | sub add_excerpts { # add text excerpts from attached files | ||
| 136 | #---------------------------------------------------------------------- | ||
| 137 | my ($self) = @_; | ||
| 138 | |||
| 139 | $self->{fulltext_result} or return; | ||
| 140 | |||
| 141 | # need new field in the Hash::Type to store the excerpts | ||
| 142 | $self->{data}->ht->add('excerpts'); | ||
| 143 | |||
| 144 | # add excerpts into each displayed record | ||
| 145 | my $regex = $self->{results}->{regex}; | ||
| 146 | foreach my $record (@{$self->{results}{records}}) { | ||
| 147 | my $buf = $self->indexed_doc_content($record); | ||
| 148 | my $excerpts = $self->{app}{indexer}->excerpts($buf, $regex); | ||
| 149 | $record->{excerpts} = join(' / ', @$excerpts); | ||
| 150 | } | ||
| 151 | } | ||
| 152 | |||
| 153 | |||
| 154 | |||
| 155 | #---------------------------------------------------------------------- | ||
| 156 | sub params_for_next_slice { | ||
| 157 | #---------------------------------------------------------------------- | ||
| 158 | my ($self, $start) = @_; | ||
| 159 | |||
| 160 | return ("SFT=$self->{search_fulltext}", | ||
| 161 | $self->SUPER::params_for_next_slice($start)); | ||
| 162 | } | ||
| 163 | |||
| 164 | |||
| 165 | |||
| 166 | #====================================================================== | ||
| 167 | # HANDLING ATTACHMENTS # | ||
| 168 | #====================================================================== | ||
| 169 | |||
| 170 | |||
| 171 | #---------------------------------------------------------------------- | ||
| 172 | sub after_add_attachment { | ||
| 173 | #---------------------------------------------------------------------- | ||
| 174 | my ($self, $record, $field, $path) = @_; | ||
| 175 | |||
| 176 | if ($field eq $self->{app}{indexed_field}) { | ||
| 177 | my $buf = $self->indexed_doc_content($record); | ||
| 178 | delete $self->{app}{indexer}; | ||
| 179 | my $indexer = Search::Indexer->new(dir => $self->{app}{dir}, | ||
| 180 | writeMode => 1); | ||
| 181 | $indexer->add($self->key($record), $buf); | ||
| 182 | } | ||
| 183 | } | ||
| 184 | |||
| 185 | #---------------------------------------------------------------------- | ||
| 186 | sub before_delete_attachment { | ||
| 187 | #---------------------------------------------------------------------- | ||
| 188 | my ($self, $record, $field, $path) = @_; | ||
| 189 | |||
| 190 | if ($field eq $self->{app}{indexed_field}) { | ||
| 191 | delete $self->{app}{indexer}; | ||
| 192 | my $indexer = Search::Indexer->new(dir => $self->{app}{dir}, | ||
| 193 | writeMode => 1); | ||
| 194 | $indexer->remove($self->key($record)); | ||
| 195 | } | ||
| 196 | } | ||
| 197 | |||
| 198 | |||
| 199 | #---------------------------------------------------------------------- | ||
| 200 | sub indexed_doc_content { | ||
| 201 | #---------------------------------------------------------------------- | ||
| 202 | my ($self, $record) = @_; | ||
| 203 | |||
| 204 | # this is the default implementation, MOST PROBABLY INADEQUATE | ||
| 205 | # should be overridden in subclasses to perform appropriate | ||
| 206 | # conversions from Html, Pdf, Word, etc. | ||
| 207 | |||
| 208 | my $path = $self->upload_fullpath($record, $self->{indexed_field}); | ||
| 209 | open my $fh, $path or die "open $path: $!"; | ||
| 210 | local $/; | ||
| 211 | my $content = <$fh>; # just return the file content | ||
| 212 | return $content; | ||
| 213 | } | ||
| 214 | |||
| 215 | |||
| 216 | |||
| 217 | |||
| 218 | 1; | ||
| 219 | |||
| 220 | __END__ |