| 1 | package KinoSearch1::Search::BooleanQuery; | ||||||||||||||
| 2 | 18 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 18 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 18 | ▶ | use KinoSearch1::Util::ToolSet; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 18 | ▶ | use base qw( KinoSearch1::Search::Query ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 18 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 18 | __PACKAGE__->init_instance_vars( | |||||||||||||
| 9 | # constructor args / members | ||||||||||||||
| 10 | disable_coord => 0, | ||||||||||||||
| 11 | # members | ||||||||||||||
| 12 | clauses => undef, | ||||||||||||||
| 13 | max_clause_count => 1024, | ||||||||||||||
| 14 | ); | ||||||||||||||
| 15 | 18 | __PACKAGE__->ready_get(qw( clauses )); | |||||||||||||
| 16 | } | ||||||||||||||
| 17 | |||||||||||||||
| 18 | 18 | ▶ | use KinoSearch1::Search::BooleanClause; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | |||||||||||||||
| 20 | 245 | ▶ | sub init_instance { | ||||||||||||
|
Subroutine
init_instance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 21 | 245 | my $self = shift; | |||||||||||||
| 22 | 245 | $self->{clauses} = []; | |||||||||||||
| 23 | } | ||||||||||||||
| 24 | |||||||||||||||
| 25 | # Add an subquery tagged with boolean characteristics. | ||||||||||||||
| 26 | 513 | ▶ | sub add_clause { | ||||||||||||
|
Subroutine
add_clause: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||
| 27 | 513 | my $self = shift; | |||||||||||||
| 28 | 513 | ▶ | my $clause | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | = @_ == 1 | ||||||||||||||
| 30 | ? shift | ||||||||||||||
| 31 | : KinoSearch1::Search::BooleanClause->new(@_); | ||||||||||||||
| 32 | 513 | push @{ $self->{clauses} }, $clause; | |||||||||||||
| 33 | 513 | ▶ | confess("not a BooleanClause") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 34 | unless a_isa_b( $clause, 'KinoSearch1::Search::BooleanClause' ); | ||||||||||||||
| 35 | confess("Too many clauses") | ||||||||||||||
| 36 | 513 | ▶ | if @{ $self->{clauses} } > $self->{max_clause_count}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 436 | ▶ | sub get_similarity { | ||||||||||||
|
Subroutine
get_similarity: called CC=2 SCAR=7.6
Pod
undocumented
| |||||||||||||||
| 40 | 436 | my ( $self, $searcher ) = @_; | |||||||||||||
| 41 | 436 | ▶ | if ( $self->{disable_coord} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 42 | 0 | confess "disable_coord not implemented yet"; | |||||||||||||
| 43 | } | ||||||||||||||
| 44 | 436 | return $searcher->get_similarity; | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | |||||||||||||||
| 47 | 3 | ▶ | sub extract_terms { | ||||||||||||
|
Subroutine
extract_terms: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 48 | 3 | my $self = shift; | |||||||||||||
| 49 | 3 | my @terms; | |||||||||||||
| 50 | 3 | for my $clause ( @{ $self->{clauses} } ) { | |||||||||||||
| 51 | 6 | push @terms, $clause->get_query()->extract_terms; | |||||||||||||
| 52 | } | ||||||||||||||
| 53 | 3 | return @terms; | |||||||||||||
| 54 | } | ||||||||||||||
| 55 | |||||||||||||||
| 56 | 248 | ▶ | sub create_weight { | ||||||||||||
|
Subroutine
create_weight: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 57 | 248 | my ( $self, $searcher ) = @_; | |||||||||||||
| 58 | 248 | return KinoSearch1::Search::BooleanWeight->new( | |||||||||||||
| 59 | parent => $self, | ||||||||||||||
| 60 | searcher => $searcher, | ||||||||||||||
| 61 | ); | ||||||||||||||
| 62 | |||||||||||||||
| 63 | } | ||||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | ▶ | sub clone { shift->todo_death } | ||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 66 | |||||||||||||||
| 67 | package KinoSearch1::Search::BooleanWeight; | ||||||||||||||
| 68 | 18 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 69 | 18 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 70 | 18 | ▶ | use KinoSearch1::Util::ToolSet; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 71 | 18 | ▶ | use base qw( KinoSearch1::Search::Weight ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 72 | |||||||||||||||
| 73 | 18 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 74 | 18 | __PACKAGE__->init_instance_vars( | |||||||||||||
| 75 | # members | ||||||||||||||
| 76 | weights => undef, | ||||||||||||||
| 77 | ); | ||||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | 18 | ▶ | use KinoSearch1::Search::BooleanScorer; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 81 | |||||||||||||||
| 82 | 248 | ▶ | sub init_instance { | ||||||||||||
|
Subroutine
init_instance: called CC=2 SCAR=6.9
| |||||||||||||||
| 83 | 248 | my $self = shift; | |||||||||||||
| 84 | 248 | $self->{weights} = []; | |||||||||||||
| 85 | 248 | my ( $weights, $searcher ) = @{$self}{ 'weights', 'searcher' }; | |||||||||||||
| 86 | |||||||||||||||
| 87 | 248 | $self->{similarity} = $self->{parent}->get_similarity($searcher); | |||||||||||||
| 88 | |||||||||||||||
| 89 | 248 | for my $clause ( @{ $self->{parent}{clauses} } ) { | |||||||||||||
| 90 | 518 | my $query = $clause->get_query; | |||||||||||||
| 91 | 518 | push @$weights, $query->create_weight($searcher); | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | 248 | undef $self->{searcher}; # don't want the baggage | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 0 | ▶ | sub get_value { shift->{parent}->get_boost } | ||||||||||||
|
Subroutine
get_value: not called CC=1 SCAR=6.9
| |||||||||||||||
| 98 | |||||||||||||||
| 99 | 248 | ▶ | sub sum_of_squared_weights { | ||||||||||||
|
Subroutine
sum_of_squared_weights: called CC=2 SCAR=6.9
| |||||||||||||||
| 100 | 248 | my $self = shift; | |||||||||||||
| 101 | |||||||||||||||
| 102 | 248 | my $sum = 0; | |||||||||||||
| 103 | 248 | $sum += $_->sum_of_squared_weights for @{ $self->{weights} }; | |||||||||||||
| 104 | |||||||||||||||
| 105 | # compound the weight of each sub-Weight | ||||||||||||||
| 106 | 248 | $sum *= $self->{parent}->get_boost**2; | |||||||||||||
| 107 | |||||||||||||||
| 108 | 248 | return $sum; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | 248 | ▶ | sub normalize { | ||||||||||||
|
Subroutine
normalize: called CC=2 SCAR=6.9
| |||||||||||||||
| 112 | 248 | my ( $self, $query_norm ) = @_; | |||||||||||||
| 113 | 248 | $_->normalize($query_norm) for @{ $self->{weights} }; | |||||||||||||
| 114 | } | ||||||||||||||
| 115 | |||||||||||||||
| 116 | 248 | ▶ | sub scorer { | ||||||||||||
|
Subroutine
scorer: called CC=4 SCAR=13.9
| |||||||||||||||
| 117 | 248 | my ( $self, $reader ) = @_; | |||||||||||||
| 118 | |||||||||||||||
| 119 | my $scorer = KinoSearch1::Search::BooleanScorer->new( | ||||||||||||||
| 120 | 248 | similarity => $self->{similarity}, ); | |||||||||||||
| 121 | |||||||||||||||
| 122 | # add all the subscorers one by one | ||||||||||||||
| 123 | 248 | my $clauses = $self->{parent}{clauses}; | |||||||||||||
| 124 | 248 | my $i = 0; | |||||||||||||
| 125 | 248 | for my $weight ( @{ $self->{weights} } ) { | |||||||||||||
| 126 | 514 | my $clause = $clauses->[ $i++ ]; | |||||||||||||
| 127 | 514 | my $subscorer = $weight->scorer($reader); | |||||||||||||
| 128 | 514 | ▶ | if ( defined $subscorer ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 129 | 463 | $scorer->add_subscorer( $subscorer, $clause->get_occur ); | |||||||||||||
| 130 | } | ||||||||||||||
| 131 | elsif ( $clause->is_required ) { | ||||||||||||||
| 132 | # if any required clause fails, the whole thing fails | ||||||||||||||
| 133 | 10 | return undef; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | } | ||||||||||||||
| 136 | 238 | return $scorer; | |||||||||||||
| 137 | } | ||||||||||||||
| 138 | |||||||||||||||
| 139 | 18 | 1; | |||||||||||||
| 140 | |||||||||||||||
| 141 | __END__ | ||||||||||||||