| 1 | package SWISH::Prog::Native::Searcher; | ||
| 2 | use strict; | ||
| 3 | use warnings; | ||
| 4 | use Carp; | ||
| 5 | use base qw( SWISH::Prog::Searcher ); | ||
| 6 | use SWISH::API::Object; | ||
| 7 | use SWISH::Prog::Native::InvIndex; | ||
| 8 | use SWISH::Prog::Native::Result; | ||
| 9 | use Search::Query; | ||
| 10 | |||
| 11 | __PACKAGE__->mk_accessors(qw( swish sao_opts result_class )); | ||
| 12 | |||
| 13 | our $VERSION = '0.75'; | ||
| 14 | |||
| 15 | =head1 NAME | ||
| 16 | |||
| 17 | SWISH::Prog::Native::Searcher - wrapper for SWISH::API::Object | ||
| 18 | |||
| 19 | =head1 SYNOPSIS | ||
| 20 | |||
| 21 | # see SWISH::Prog::Searcher | ||
| 22 | |||
| 23 | =head1 DESCRIPTION | ||
| 24 | |||
| 25 | The Native Searcher is a thin wrapper around SWISH::API::Object. | ||
| 26 | |||
| 27 | =head1 METHODS | ||
| 28 | |||
| 29 | =cut | ||
| 30 | |||
| 31 | =head2 init | ||
| 32 | |||
| 33 | Instantiates the SWISH::API::Object instance and stores it | ||
| 34 | in the swish() accessor. | ||
| 35 | |||
| 36 | =cut | ||
| 37 | |||
| 38 | sub init { | ||
| 39 | my $self = shift; | ||
| 40 | $self->SUPER::init(@_); | ||
| 41 | |||
| 42 | $self->{swish} = SWISH::API::Object->new( | ||
| 43 | indexes => [ map { $_->file } @{ $self->{invindex} } ], | ||
| 44 | class => $self->{result_class} || 'SWISH::Prog::Native::Result', | ||
| 45 | @{ $self->{sao_opts} || [] } | ||
| 46 | ); | ||
| 47 | |||
| 48 | # add accessor methods to the Result class | ||
| 49 | # to mimic what SWISH::API::Object does. | ||
| 50 | my $resclass = $self->{swish}->{class}; | ||
| 51 | if ( $resclass->can('mk_accessors') ) { | ||
| 52 | my @propnames = $self->{swish}->props; | ||
| 53 | for my $name (@propnames) { | ||
| 54 | if ( !$resclass->can($name) ) { | ||
| 55 | $resclass->mk_accessors($name); | ||
| 56 | } | ||
| 57 | } | ||
| 58 | } | ||
| 59 | |||
| 60 | # load meta from the first invindex | ||
| 61 | my $invindex = $self->invindex->[0]; | ||
| 62 | my $config = $invindex->meta; | ||
| 63 | |||
| 64 | # have to wrap MetaNames check in eval because | ||
| 65 | # there may not be any explicitly defined in config. | ||
| 66 | my $metanames; | ||
| 67 | eval { $metanames = $config->MetaNames; }; | ||
| 68 | if ( $@ and $@ =~ m/^no such Meta key: MetaNames/ ) { | ||
| 69 | $metanames = { swishdefault => {} }; | ||
| 70 | } | ||
| 71 | my $field_names = [ keys %$metanames ]; | ||
| 72 | my %fieldtypes; | ||
| 73 | for my $name (@$field_names) { | ||
| 74 | |||
| 75 | # TODO check PropertyNames for string|int|date | ||
| 76 | $fieldtypes{$name} = {}; | ||
| 77 | |||
| 78 | if ( exists $metanames->{$name}->{alias_for} ) { | ||
| 79 | $fieldtypes{$name}->{alias_for} | ||
| 80 | = $metanames->{$name}->{alias_for}; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | # TODO could expose 'qp' as param to new(). | ||
| 85 | $self->{qp} ||= Search::Query::Parser->new( | ||
| 86 | dialect => 'SWISH', | ||
| 87 | fields => \%fieldtypes, | ||
| 88 | query_class_opts => { | ||
| 89 | default_field => $field_names, | ||
| 90 | debug => $self->debug, | ||
| 91 | } | ||
| 92 | ); | ||
| 93 | |||
| 94 | return $self; | ||
| 95 | } | ||
| 96 | |||
| 97 | =head2 sao_opts( I<array_ref> ) | ||
| 98 | |||
| 99 | Options to pass to SWISH::API::Object in new(). | ||
| 100 | |||
| 101 | =head2 result_class( I<class_name> ) | ||
| 102 | |||
| 103 | Passed to SWISH::API::Object in new(). | ||
| 104 | |||
| 105 | =head2 swish | ||
| 106 | |||
| 107 | The SWISH::API::Object instance. | ||
| 108 | |||
| 109 | =head2 search( I<query>, I<opts> ) | ||
| 110 | |||
| 111 | Calls the query() method on the internal SWISH::API::Object. | ||
| 112 | Returns a SWISH::API::Object::Results object. | ||
| 113 | |||
| 114 | I<opts> is an optional hashref with the following supported | ||
| 115 | key/values: | ||
| 116 | |||
| 117 | =over | ||
| 118 | |||
| 119 | =item start | ||
| 120 | |||
| 121 | The starting position. Default is 0. | ||
| 122 | |||
| 123 | =item max | ||
| 124 | |||
| 125 | The ending position. Default is max_hits() as documented | ||
| 126 | in SWISH::Prog::Searcher. | ||
| 127 | |||
| 128 | =item order | ||
| 129 | |||
| 130 | Takes a SQL-like sort string in pattern I<field> I<direction>. | ||
| 131 | See the Swish-e docs for sort string details. | ||
| 132 | |||
| 133 | =item limit | ||
| 134 | |||
| 135 | Takes an arrayref of arrayrefs. Each child arrayref should | ||
| 136 | have three values: a field (PropertyName) value, a lower limit | ||
| 137 | and an upper limit. | ||
| 138 | |||
| 139 | =item rank_scheme | ||
| 140 | |||
| 141 | Takes an int, C<0> or C<1>. Default is C<1>. | ||
| 142 | |||
| 143 | =item default_boolop | ||
| 144 | |||
| 145 | The default boolean connector for parsing I<query>. Valid values | ||
| 146 | are B<AND> and B<OR>. The default is B<AND>. | ||
| 147 | |||
| 148 | =back | ||
| 149 | |||
| 150 | =cut | ||
| 151 | |||
| 152 | my %boolops = ( | ||
| 153 | 'AND' => '+', | ||
| 154 | 'OR' => '', | ||
| 155 | ); | ||
| 156 | |||
| 157 | sub search { | ||
| 158 | my $self = shift; | ||
| 159 | my $query = shift or croak "query required"; | ||
| 160 | my $opts = shift || {}; | ||
| 161 | my $start = $opts->{start} || 0; | ||
| 162 | my $max = $opts->{max} || $self->max_hits; | ||
| 163 | my $order = $opts->{order}; | ||
| 164 | my $limits = $opts->{limit} || []; | ||
| 165 | my $rank_scheme = $opts->{rank_scheme}; | ||
| 166 | $rank_scheme = 1 unless defined $rank_scheme; | ||
| 167 | my $boolop = $opts->{default_boolop} || 'AND'; | ||
| 168 | |||
| 169 | if ( !exists $boolops{ uc($boolop) } ) { | ||
| 170 | croak "Unsupported default_boolop: $boolop (should be AND or OR)"; | ||
| 171 | } | ||
| 172 | $self->{qp}->default_boolop( $boolops{$boolop} ); | ||
| 173 | my $parsed_query = $self->{qp}->parse($query) | ||
| 174 | or croak "Query syntax error: " . $self->{qp}->error; | ||
| 175 | |||
| 176 | my $swishdb = $self->{swish}; | ||
| 177 | |||
| 178 | # use idf ranking | ||
| 179 | $swishdb->rank_scheme($rank_scheme); | ||
| 180 | $swishdb->die_on_error('critical_error'); | ||
| 181 | |||
| 182 | my $searcher = $swishdb->new_search_object; | ||
| 183 | |||
| 184 | for my $limit (@$limits) { | ||
| 185 | if ( !ref $limit or ref($limit) ne 'ARRAY' or @$limit != 3 ) { | ||
| 186 | croak | ||
| 187 | "poorly-formed limit ($limit). should be an array ref of 3 values."; | ||
| 188 | } | ||
| 189 | $searcher->set_search_limit(@$limit); | ||
| 190 | } | ||
| 191 | if ($order) { | ||
| 192 | $searcher->set_sort($order); | ||
| 193 | $swishdb->die_on_error; | ||
| 194 | } | ||
| 195 | |||
| 196 | my $results = $searcher->execute("$parsed_query"); | ||
| 197 | $results->{swish_query} | ||
| 198 | = join( ' ', $results->parsed_words( $swishdb->indexes->[0] ) ); | ||
| 199 | $results->{query} = $parsed_query; | ||
| 200 | $swishdb->die_on_error; | ||
| 201 | $results->seek_result($start); | ||
| 202 | return $results; | ||
| 203 | } | ||
| 204 | |||
| 205 | 1; | ||
| 206 | |||
| 207 | __END__ |