| 1 | package KinoSearch1::Search::MultiSearcher; | ||||||||||
| 2 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 3 | ▶ | use KinoSearch1::Util::ToolSet; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 3 | ▶ | use base qw( KinoSearch1::Searcher ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 3 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | __PACKAGE__->init_instance_vars( | |||||||||
| 9 | # members / constructor args | ||||||||||
| 10 | searchables => undef, | ||||||||||
| 11 | # members | ||||||||||
| 12 | starts => undef, | ||||||||||
| 13 | max_doc => undef, | ||||||||||
| 14 | ); | ||||||||||
| 15 | } | ||||||||||
| 16 | |||||||||||
| 17 | 3 | ▶ | use KinoSearch1::Search::Similarity; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 18 | |||||||||||
| 19 | 2 | ▶ | sub init_instance { | ||||||||
|
Subroutine
init_instance: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 20 | 2 | my $self = shift; | |||||||||
| 21 | 2 | $self->{field_sims} = {}; | |||||||||
| 22 | |||||||||||
| 23 | # derive max_doc, relative start offsets | ||||||||||
| 24 | 2 | my $max_doc = 0; | |||||||||
| 25 | 2 | my @starts; | |||||||||
| 26 | 2 | for my $searchable ( @{ $self->{searchables} } ) { | |||||||||
| 27 | 4 | push @starts, $max_doc; | |||||||||
| 28 | 4 | $max_doc += $searchable->max_doc; | |||||||||
| 29 | } | ||||||||||
| 30 | 2 | $self->{max_doc} = $max_doc; | |||||||||
| 31 | 2 | $self->{starts} = \@starts; | |||||||||
| 32 | |||||||||||
| 33 | # default similarity | ||||||||||
| 34 | $self->{similarity} = KinoSearch1::Search::Similarity->new | ||||||||||
| 35 | 2 | ▶ | unless defined $self->{similarity}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 4 | ▶ | sub get_field_names { | ||||||||
|
Subroutine
get_field_names: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 39 | 4 | my $self = shift; | |||||||||
| 40 | 4 | my %field_names; | |||||||||
| 41 | 4 | for my $searchable ( @{ $self->{searchables} } ) { | |||||||||
| 42 | 8 | my $sub_field_names = $searchable->get_field_names; | |||||||||
| 43 | 8 | @field_names{@$sub_field_names} = (1) x scalar @$sub_field_names; | |||||||||
| 44 | } | ||||||||||
| 45 | 4 | return [ keys %field_names ]; | |||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | 4 | ▶ | sub max_doc { shift->{max_doc} } | ||||||||
|
Subroutine
max_doc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 49 | |||||||||||
| 50 | 0 | ▶ | sub close { } | ||||||||
|
Subroutine
close: not called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 51 | |||||||||||
| 52 | 2 | ▶ | sub subsearcher { | ||||||||
|
Subroutine
subsearcher: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 53 | 2 | my ( $self, $doc_num ) = @_; | |||||||||
| 54 | 2 | my $i = -1; | |||||||||
| 55 | 2 | for ( @{ $self->{starts} } ) { | |||||||||
| 56 | 4 | ▶ | last if $_ > $doc_num; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 3 | $i++; | |||||||||
| 58 | } | ||||||||||
| 59 | 2 | return $i; | |||||||||
| 60 | } | ||||||||||
| 61 | |||||||||||
| 62 | 0 | ▶ | sub doc_freq { | ||||||||
|
Subroutine
doc_freq: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||
| 63 | 0 | my ( $self, $term ) = @_; | |||||||||
| 64 | 0 | my $doc_freq = 0; | |||||||||
| 65 | 0 | $doc_freq += $_->doc_freq($term) for @{ $self->{searchables} }; | |||||||||
| 66 | 0 | return $doc_freq; | |||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | 2 | ▶ | sub fetch_doc { | ||||||||
|
Subroutine
fetch_doc: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 70 | 2 | my ( $self, $doc_num ) = @_; | |||||||||
| 71 | 2 | my $i = $self->subsearcher($doc_num); | |||||||||
| 72 | 2 | my $searchable = $self->{searchables}[$i]; | |||||||||
| 73 | 2 | $doc_num -= $self->{starts}[$i]; | |||||||||
| 74 | 2 | return $searchable->fetch_doc($doc_num); | |||||||||
| 75 | } | ||||||||||
| 76 | |||||||||||
| 77 | 3 | my %search_hit_collector_args = ( | |||||||||
| 78 | hit_collector => undef, | ||||||||||
| 79 | weight => undef, | ||||||||||
| 80 | filter => undef, | ||||||||||
| 81 | sort_spec => undef, | ||||||||||
| 82 | ); | ||||||||||
| 83 | |||||||||||
| 84 | 4 | ▶ | sub search_hit_collector { | ||||||||
|
Subroutine
search_hit_collector: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 85 | 4 | my $self = shift; | |||||||||
| 86 | 4 | ▶ | confess kerror() unless verify_args( \%search_hit_collector_args, @_ ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 87 | 4 | my %args = ( %search_hit_collector_args, @_ ); | |||||||||
| 88 | 4 | my ( $searchables, $starts ) = @{$self}{qw( searchables starts )}; | |||||||||
| 89 | |||||||||||
| 90 | 4 | for my $i ( 0 .. $#$searchables ) { | |||||||||
| 91 | 8 | my $searchable = $searchables->[$i]; | |||||||||
| 92 | 8 | my $start = $starts->[$i]; | |||||||||
| 93 | my $collector = KinoSearch1::Search::OffsetCollector->new( | ||||||||||
| 94 | hit_collector => $args{hit_collector}, | ||||||||||
| 95 | 8 | offset => $start | |||||||||
| 96 | ); | ||||||||||
| 97 | 8 | $searchable->search_hit_collector( %args, | |||||||||
| 98 | hit_collector => $collector ); | ||||||||||
| 99 | } | ||||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 4 | ▶ | sub rewrite { | ||||||||
|
Subroutine
rewrite: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 103 | 4 | my ( $self, $orig_query ) = @_; | |||||||||
| 104 | |||||||||||
| 105 | # not necessary to rewrite until we add query types that need it | ||||||||||
| 106 | 4 | return $orig_query; | |||||||||
| 107 | |||||||||||
| 108 | #my @queries = map { $_->rewrite($orig_query) } @{ $self->{searchables} }; | ||||||||||
| 109 | #my $combined = $queries->[0]->combine(\@queries); | ||||||||||
| 110 | #return $combined; | ||||||||||
| 111 | } | ||||||||||
| 112 | |||||||||||
| 113 | 4 | ▶ | sub create_weight { | ||||||||
|
Subroutine
create_weight: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||
| 114 | 4 | my ( $self, $query ) = @_; | |||||||||
| 115 | 4 | my $searchables = $self->{searchables}; | |||||||||
| 116 | |||||||||||
| 117 | 4 | my $rewritten_query = $self->rewrite($query); | |||||||||
| 118 | |||||||||||
| 119 | # generate an array of unique terms | ||||||||||
| 120 | 4 | my @terms = $rewritten_query->extract_terms; | |||||||||
| 121 | 4 | my %unique_terms; | |||||||||
| 122 | 4 | for my $term (@terms) { | |||||||||
| 123 | 4 | ▶ | if ( a_isa_b( $term, "KinoSearch1::Index::Term" ) ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 124 | 4 | $unique_terms{ $term->to_string } = $term; | |||||||||
| 125 | } | ||||||||||
| 126 | else { | ||||||||||
| 127 | # PhraseQuery returns an array of terms | ||||||||||
| 128 | 0 | $unique_terms{ $_->to_string } = $_ for @$term; | |||||||||
| 129 | } | ||||||||||
| 130 | } | ||||||||||
| 131 | 4 | @terms = values %unique_terms; | |||||||||
| 132 | 4 | my @stringified = keys %unique_terms; | |||||||||
| 133 | |||||||||||
| 134 | # get an aggregated doc_freq for each term | ||||||||||
| 135 | 4 | my @aggregated_doc_freqs = (0) x scalar @terms; | |||||||||
| 136 | 4 | for my $i ( 0 .. $#$searchables ) { | |||||||||
| 137 | 8 | my $doc_freqs = $searchables->[$i]->doc_freqs( \@terms ); | |||||||||
| 138 | 8 | for my $j ( 0 .. $#terms ) { | |||||||||
| 139 | 8 | $aggregated_doc_freqs[$j] += $doc_freqs->[$j]; | |||||||||
| 140 | } | ||||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | # prepare a hashmap of stringified_term => doc_freq pairs. | ||||||||||
| 144 | 4 | my %doc_freq_map; | |||||||||
| 145 | 4 | @doc_freq_map{@stringified} = @aggregated_doc_freqs; | |||||||||
| 146 | |||||||||||
| 147 | 4 | my $cache_df_source = KinoSearch1::Search::CacheDFSource->new( | |||||||||
| 148 | doc_freq_map => \%doc_freq_map, | ||||||||||
| 149 | max_doc => $self->max_doc, | ||||||||||
| 150 | similarity => $self->get_similarity, | ||||||||||
| 151 | ); | ||||||||||
| 152 | |||||||||||
| 153 | 4 | return $rewritten_query->to_weight($cache_df_source); | |||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | package KinoSearch1::Search::CacheDFSource; | ||||||||||
| 157 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 158 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 159 | 3 | ▶ | use KinoSearch1::Util::ToolSet; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 160 | 3 | ▶ | use base qw( KinoSearch1::Search::Searchable ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 161 | |||||||||||
| 162 | 3 | ▶ | BEGIN { | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 163 | 3 | __PACKAGE__->init_instance_vars( | |||||||||
| 164 | doc_freq_map => {}, | ||||||||||
| 165 | max_doc => undef, | ||||||||||
| 166 | ); | ||||||||||
| 167 | 3 | __PACKAGE__->ready_get(qw( max_doc )); | |||||||||
| 168 | } | ||||||||||
| 169 | |||||||||||
| 170 | 4 | ▶ | sub init_instance { } | ||||||||
|
Subroutine
init_instance: called CC=1 SCAR=0.0
| |||||||||||
| 171 | |||||||||||
| 172 | 8 | ▶ | sub doc_freq { | ||||||||
|
Subroutine
doc_freq: called CC=2 SCAR=7.1
| |||||||||||
| 173 | 8 | my ( $self, $term ) = @_; | |||||||||
| 174 | 8 | my $df = $self->{doc_freq_map}{ $term->to_string }; | |||||||||
| 175 | 8 | ▶ | confess( "df for " . $term->to_string . " not available" ) | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | unless defined $df; | ||||||||||
| 177 | } | ||||||||||
| 178 | |||||||||||
| 179 | 0 | ▶ | sub doc_freqs { | ||||||||
|
Subroutine
doc_freqs: not called CC=1 SCAR=6.9
| |||||||||||
| 180 | 0 | my $self = shift; | |||||||||
| 181 | 0 | my @doc_freqs = map { $self->doc_freq($_) } @_; | |||||||||
| 182 | 0 | return \@doc_freqs; | |||||||||
| 183 | } | ||||||||||
| 184 | |||||||||||
| 185 | 4 | ▶ | sub max_doc { shift->{max_doc} } | ||||||||
|
Subroutine
max_doc: called CC=1 SCAR=0.0
| |||||||||||
| 186 | |||||||||||
| 187 | 4 | ▶ | sub rewrite { | ||||||||
|
Subroutine
rewrite: called CC=1 SCAR=0.0
| |||||||||||
| 188 | 4 | return $_[1]; | |||||||||
| 189 | } | ||||||||||
| 190 | |||||||||||
| 191 | =for comment | ||||||||||
| 192 | |||||||||||
| 193 | Dummy class, only here to support initialization of Weights from Queries. | ||||||||||
| 194 | |||||||||||
| 195 | =cut | ||||||||||
| 196 | |||||||||||
| 197 | 3 | 1; | |||||||||
| 198 | |||||||||||
| 199 | __END__ | ||||||||||