| 1 | package KinoSearch1::Search::PhraseQuery; | ||||||||||
| 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 | slop => 0, | ||||||||||
| 11 | # members | ||||||||||
| 12 | field => undef, | ||||||||||
| 13 | terms => undef, | ||||||||||
| 14 | positions => undef, | ||||||||||
| 15 | ); | ||||||||||
| 16 | 18 | __PACKAGE__->ready_get_set(qw( slop )); | |||||||||
| 17 | 18 | __PACKAGE__->ready_get(qw( terms )); | |||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | 18 | ▶ | use KinoSearch1::Search::TermQuery; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 21 | 18 | ▶ | use KinoSearch1::Document::Field; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 22 | 18 | ▶ | use KinoSearch1::Util::ToStringUtils qw( boost_to_string ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 23 | |||||||||||
| 24 | 67 | ▶ | sub init_instance { | ||||||||
|
Subroutine
init_instance: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 25 | 67 | my $self = shift; | |||||||||
| 26 | 67 | $self->{terms} = []; | |||||||||
| 27 | 67 | $self->{positions} = []; | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | # Add a term/position combo to the query. The position is specified | ||||||||||
| 31 | # explicitly in order to allow for phrases with gaps, two terms at the same | ||||||||||
| 32 | # position, etc. | ||||||||||
| 33 | 142 | ▶ | sub add_term { | ||||||||
|
Subroutine
add_term: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||
| 34 | 142 | my ( $self, $term, $position ) = @_; | |||||||||
| 35 | 142 | my $field = $term->get_field; | |||||||||
| 36 | 142 | ▶ | $self->{field} = $field unless defined $self->{field}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 37 | croak("Mismatched fields in phrase query: '$self->{field}' '$field'") | ||||||||||
| 38 | 142 | ▶ | unless ( $field eq $self->{field} ); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 39 | 142 | ▶ | if ( !defined $position ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 40 | $position | ||||||||||
| 41 | 142 | = @{ $self->{positions} } | |||||||||
| 42 | 142 | ▶ | ? $self->{positions}[-1] + 1 | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | : 0; | ||||||||||
| 44 | } | ||||||||||
| 45 | 142 | push @{ $self->{terms} }, $term; | |||||||||
| 46 | 142 | push @{ $self->{positions} }, $position; | |||||||||
| 47 | } | ||||||||||
| 48 | |||||||||||
| 49 | 68 | ▶ | sub create_weight { | ||||||||
|
Subroutine
create_weight: called CC=2 SCAR=7.9
Pod
undocumented
| |||||||||||
| 50 | 68 | my ( $self, $searcher ) = @_; | |||||||||
| 51 | |||||||||||
| 52 | # optimize for one-term phrases | ||||||||||
| 53 | 68 | ▶ | if ( @{ $self->{terms} } == 1 ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | my $term_query | ||||||||||
| 55 | 0 | = KinoSearch1::Search::TermQuery->new( term => $self->{terms}[0], | |||||||||
| 56 | ); | ||||||||||
| 57 | 0 | return $term_query->create_weight($searcher); | |||||||||
| 58 | } | ||||||||||
| 59 | else { | ||||||||||
| 60 | 68 | return KinoSearch1::Search::PhraseWeight->new( | |||||||||
| 61 | parent => $self, | ||||||||||
| 62 | searcher => $searcher, | ||||||||||
| 63 | ); | ||||||||||
| 64 | } | ||||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | 2 | ▶ | sub extract_terms { shift->{terms} } | ||||||||
|
Subroutine
extract_terms: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 68 | |||||||||||
| 69 | 0 | ▶ | sub to_string { | ||||||||
|
Subroutine
to_string: not called CC=4 SCAR=30.0
Pod
undocumented
| |||||||||||
| 70 | 0 | my ( $self, $proposed_field ) = @_; | |||||||||
| 71 | my $string | ||||||||||
| 72 | = $proposed_field eq $self->{field} | ||||||||||
| 73 | 0 | ▶ | ? qq(") | ||||||||
|
Branch: 1 branch
| |||||||||||
| 74 | : qq($proposed_field:"); | ||||||||||
| 75 | 0 | $string .= ( $_->get_text . ' ' ) for @{ $self->{terms} }; | |||||||||
| 76 | 0 | $string .= qq("); | |||||||||
| 77 | 0 | ▶ | $string .= qq(~$self->{slop}) if $self->{slop}; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 0 | $string .= boost_to_string( $self->get_boost ); | |||||||||
| 79 | 0 | return $string; | |||||||||
| 80 | } | ||||||||||
| 81 | |||||||||||
| 82 | package KinoSearch1::Search::PhraseWeight; | ||||||||||
| 83 | 18 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 84 | 18 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 85 | 18 | ▶ | use KinoSearch1::Util::ToolSet; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 86 | 18 | ▶ | use base qw( KinoSearch1::Search::Weight ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 87 | |||||||||||
| 88 | 18 | ▶ | BEGIN { __PACKAGE__->init_instance_vars(); } | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 89 | |||||||||||
| 90 | 18 | ▶ | use KinoSearch1::Search::PhraseScorer; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 91 | |||||||||||
| 92 | 68 | ▶ | sub init_instance { | ||||||||
|
Subroutine
init_instance: called CC=1 SCAR=0.0
| |||||||||||
| 93 | 68 | my $self = shift; | |||||||||
| 94 | $self->{similarity} | ||||||||||
| 95 | 68 | = $self->{parent}->get_similarity( $self->{searcher} ); | |||||||||
| 96 | $self->{idf} = $self->{similarity} | ||||||||||
| 97 | 68 | ->idf( $self->{parent}->get_terms, $self->{searcher} ); | |||||||||
| 98 | |||||||||||
| 99 | 68 | undef $self->{searcher}; # don't want the baggage | |||||||||
| 100 | } | ||||||||||
| 101 | |||||||||||
| 102 | 68 | ▶ | sub scorer { | ||||||||
|
Subroutine
scorer: called CC=4 SCAR=13.9
| |||||||||||
| 103 | 68 | my ( $self, $reader ) = @_; | |||||||||
| 104 | 68 | my $query = $self->{parent}; | |||||||||
| 105 | |||||||||||
| 106 | # look up each term | ||||||||||
| 107 | 68 | my @term_docs; | |||||||||
| 108 | 68 | for my $term ( @{ $query->{terms} } ) { | |||||||||
| 109 | |||||||||||
| 110 | # bail if any one of the terms isn't in the index | ||||||||||
| 111 | 136 | ▶ | return unless $reader->doc_freq($term); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 112 | |||||||||||
| 113 | 128 | my $td = $reader->term_docs($term); | |||||||||
| 114 | 128 | push @term_docs, $td; | |||||||||
| 115 | |||||||||||
| 116 | # turn on positions | ||||||||||
| 117 | 128 | $td->set_read_positions(1); | |||||||||
| 118 | } | ||||||||||
| 119 | |||||||||||
| 120 | # bail if there are no terms | ||||||||||
| 121 | 60 | ▶ | return unless @term_docs; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 122 | |||||||||||
| 123 | 60 | my $norms_reader = $reader->norms_reader( $query->{field} ); | |||||||||
| 124 | return KinoSearch1::Search::PhraseScorer->new( | ||||||||||
| 125 | weight => $self, | ||||||||||
| 126 | slop => $query->{slop}, | ||||||||||
| 127 | similarity => $self->{similarity}, | ||||||||||
| 128 | norms_reader => $norms_reader, | ||||||||||
| 129 | term_docs => \@term_docs, | ||||||||||
| 130 | phrase_offsets => $query->{positions}, | ||||||||||
| 131 | 60 | ); | |||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | 18 | 1; | |||||||||
| 135 | |||||||||||
| 136 | __END__ | ||||||||||