/usr/lib/perl5/KinoSearch1/Search/MultiSearcher.pm is in libkinosearch1-perl 1.00-1build3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | package KinoSearch1::Search::MultiSearcher;
use strict;
use warnings;
use KinoSearch1::Util::ToolSet;
use base qw( KinoSearch1::Searcher );
BEGIN {
__PACKAGE__->init_instance_vars(
# members / constructor args
searchables => undef,
# members
starts => undef,
max_doc => undef,
);
}
use KinoSearch1::Search::Similarity;
sub init_instance {
my $self = shift;
$self->{field_sims} = {};
# derive max_doc, relative start offsets
my $max_doc = 0;
my @starts;
for my $searchable ( @{ $self->{searchables} } ) {
push @starts, $max_doc;
$max_doc += $searchable->max_doc;
}
$self->{max_doc} = $max_doc;
$self->{starts} = \@starts;
# default similarity
$self->{similarity} = KinoSearch1::Search::Similarity->new
unless defined $self->{similarity};
}
sub get_field_names {
my $self = shift;
my %field_names;
for my $searchable ( @{ $self->{searchables} } ) {
my $sub_field_names = $searchable->get_field_names;
@field_names{@$sub_field_names} = (1) x scalar @$sub_field_names;
}
return [ keys %field_names ];
}
sub max_doc { shift->{max_doc} }
sub close { }
sub subsearcher {
my ( $self, $doc_num ) = @_;
my $i = -1;
for ( @{ $self->{starts} } ) {
last if $_ > $doc_num;
$i++;
}
return $i;
}
sub doc_freq {
my ( $self, $term ) = @_;
my $doc_freq = 0;
$doc_freq += $_->doc_freq($term) for @{ $self->{searchables} };
return $doc_freq;
}
sub fetch_doc {
my ( $self, $doc_num ) = @_;
my $i = $self->subsearcher($doc_num);
my $searchable = $self->{searchables}[$i];
$doc_num -= $self->{starts}[$i];
return $searchable->fetch_doc($doc_num);
}
my %search_hit_collector_args = (
hit_collector => undef,
weight => undef,
filter => undef,
sort_spec => undef,
);
sub search_hit_collector {
my $self = shift;
confess kerror() unless verify_args( \%search_hit_collector_args, @_ );
my %args = ( %search_hit_collector_args, @_ );
my ( $searchables, $starts ) = @{$self}{qw( searchables starts )};
for my $i ( 0 .. $#$searchables ) {
my $searchable = $searchables->[$i];
my $start = $starts->[$i];
my $collector = KinoSearch1::Search::OffsetCollector->new(
hit_collector => $args{hit_collector},
offset => $start
);
$searchable->search_hit_collector( %args,
hit_collector => $collector );
}
}
sub rewrite {
my ( $self, $orig_query ) = @_;
# not necessary to rewrite until we add query types that need it
return $orig_query;
#my @queries = map { $_->rewrite($orig_query) } @{ $self->{searchables} };
#my $combined = $queries->[0]->combine(\@queries);
#return $combined;
}
sub create_weight {
my ( $self, $query ) = @_;
my $searchables = $self->{searchables};
my $rewritten_query = $self->rewrite($query);
# generate an array of unique terms
my @terms = $rewritten_query->extract_terms;
my %unique_terms;
for my $term (@terms) {
if ( a_isa_b( $term, "KinoSearch1::Index::Term" ) ) {
$unique_terms{ $term->to_string } = $term;
}
else {
# PhraseQuery returns an array of terms
$unique_terms{ $_->to_string } = $_ for @$term;
}
}
@terms = values %unique_terms;
my @stringified = keys %unique_terms;
# get an aggregated doc_freq for each term
my @aggregated_doc_freqs = (0) x scalar @terms;
for my $i ( 0 .. $#$searchables ) {
my $doc_freqs = $searchables->[$i]->doc_freqs( \@terms );
for my $j ( 0 .. $#terms ) {
$aggregated_doc_freqs[$j] += $doc_freqs->[$j];
}
}
# prepare a hashmap of stringified_term => doc_freq pairs.
my %doc_freq_map;
@doc_freq_map{@stringified} = @aggregated_doc_freqs;
my $cache_df_source = KinoSearch1::Search::CacheDFSource->new(
doc_freq_map => \%doc_freq_map,
max_doc => $self->max_doc,
similarity => $self->get_similarity,
);
return $rewritten_query->to_weight($cache_df_source);
}
package KinoSearch1::Search::CacheDFSource;
use strict;
use warnings;
use KinoSearch1::Util::ToolSet;
use base qw( KinoSearch1::Search::Searchable );
BEGIN {
__PACKAGE__->init_instance_vars(
doc_freq_map => {},
max_doc => undef,
);
__PACKAGE__->ready_get(qw( max_doc ));
}
sub init_instance { }
sub doc_freq {
my ( $self, $term ) = @_;
my $df = $self->{doc_freq_map}{ $term->to_string };
confess( "df for " . $term->to_string . " not available" )
unless defined $df;
}
sub doc_freqs {
my $self = shift;
my @doc_freqs = map { $self->doc_freq($_) } @_;
return \@doc_freqs;
}
sub max_doc { shift->{max_doc} }
sub rewrite {
return $_[1];
}
=for comment
Dummy class, only here to support initialization of Weights from Queries.
=cut
1;
__END__
=head1 NAME
KinoSearch1::Search::MultiSearcher - Aggregate results from multiple searchers.
=head1 SYNOPSIS
for my $server_name (@server_names) {
push @searchers, KinoSearch1::Search::SearchClient->new(
peer_address => "$server_name:$port",
analyzer => $analyzer,
password => $pass,
);
}
my $multi_searcher = KinoSearch1::Search::MultiSearcher->new(
searchables => \@searchers,
analyzer => $analyzer,
);
my $hits = $multi_searcher->search( query => $query );
=head1 DESCRIPTION
Aside from the arguments to its constructor, MultiSearcher looks and acts just
like a L<KinoSearch1::Searcher> object.
The primary use for MultiSearcher is to aggregate results from several remote
searchers via L<SearchClient|KinoSearch1::Search::SearchClient>, diffusing the
cost of searching a large corpus over multiple machines.
=head1 METHODS
=head2 new
Constructor. Takes two hash-style parameters, both of which are required.
=over
=item *
B<analyzer> - an item which subclasses L<KinoSearch1::Analysis::Analyzer>.
=item *
B<searchables> - a reference to an array of searchers.
=back
=head1 COPYRIGHT
Copyright 2006-2010 Marvin Humphrey
=head1 LICENSE, DISCLAIMER, BUGS, etc.
See L<KinoSearch1> version 1.00.
=cut
|