| 1 | package App::CSE::Lucy::Search::QueryPrefix; | ||
| 2 | $App::CSE::Lucy::Search::QueryPrefix::VERSION = '0.016'; | ||
| 3 | ## Copied from http://api.metacpan.org/source/CREAMYG/Lucy-0.3.3/sample/PrefixQuery.pm | ||
| 4 | |||
| 5 | # Licensed to the Apache Software Foundation (ASF) under one or more | ||
| 6 | # contributor license agreements. See the NOTICE file distributed with | ||
| 7 | # this work for additional information regarding copyright ownership. | ||
| 8 | # The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| 9 | # (the "License"); you may not use this file except in compliance with | ||
| 10 | # the License. You may obtain a copy of the License at | ||
| 11 | # | ||
| 12 | # http://www.apache.org/licenses/LICENSE-2.0 | ||
| 13 | # | ||
| 14 | # Unless required by applicable law or agreed to in writing, software | ||
| 15 | # distributed under the License is distributed on an "AS IS" BASIS, | ||
| 16 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 17 | # See the License for the specific language governing permissions and | ||
| 18 | # limitations under the License. | ||
| 19 | |||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | |||
| 23 | use base qw( Lucy::Search::Query ); | ||
| 24 | use Carp; | ||
| 25 | use Scalar::Util qw( blessed ); | ||
| 26 | |||
| 27 | |||
| 28 | # Inside-out member vars and hand-rolled accessors. | ||
| 29 | my %query_string; | ||
| 30 | my %field; | ||
| 31 | my %highlight_terms; | ||
| 32 | my %keep_case; | ||
| 33 | |||
| 34 | sub get_query_string { my $self = shift; return $query_string{$$self} } | ||
| 35 | sub get_field { my $self = shift; return $field{$$self} } | ||
| 36 | sub highlight_terms { my $self = shift; return $highlight_terms{$$self} } | ||
| 37 | sub keep_case { my $self = shift; return $keep_case{$$self} } | ||
| 38 | |||
| 39 | |||
| 40 | |||
| 41 | sub new { | ||
| 42 | my ( $class, %args ) = @_; | ||
| 43 | my $query_string = delete $args{query_string}; | ||
| 44 | my $field = delete $args{field}; | ||
| 45 | my $keep_case = delete $args{keep_case} || 0; | ||
| 46 | |||
| 47 | my $self = $class->SUPER::new(%args); | ||
| 48 | |||
| 49 | |||
| 50 | confess("'query_string' param is required") | ||
| 51 | unless defined $query_string; | ||
| 52 | confess("Invalid query_string: '$query_string'") | ||
| 53 | unless $query_string =~ /\*\s*$/; | ||
| 54 | confess("'field' param is required") | ||
| 55 | unless defined $field; | ||
| 56 | |||
| 57 | $query_string{$$self} = $query_string; | ||
| 58 | $field{$$self} = $field; | ||
| 59 | $highlight_terms{$$self} = []; | ||
| 60 | $keep_case{$$self} = $keep_case; | ||
| 61 | |||
| 62 | return $self; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub highlight_query{ | ||
| 66 | my ($self, $field) = @_; | ||
| 67 | my $query = Lucy::Search::ORQuery->new(); | ||
| 68 | foreach my $term ( @{$self->highlight_terms()} ){ | ||
| 69 | $query->add_child(Lucy::Search::TermQuery->new( | ||
| 70 | field => $field || $self->get_field(), | ||
| 71 | term => $term, | ||
| 72 | )); | ||
| 73 | } | ||
| 74 | return $query; | ||
| 75 | } | ||
| 76 | |||
| 77 | |||
| 78 | sub add_matching_term{ | ||
| 79 | my ($self, $term ) = @_; | ||
| 80 | push @{$self->highlight_terms()}, $term; | ||
| 81 | } | ||
| 82 | |||
| 83 | sub DESTROY { | ||
| 84 | my $self = shift; | ||
| 85 | delete $query_string{$$self}; | ||
| 86 | delete $field{$$self}; | ||
| 87 | delete $highlight_terms{$$self}; | ||
| 88 | $self->SUPER::DESTROY; | ||
| 89 | } | ||
| 90 | |||
| 91 | sub equals { | ||
| 92 | my ( $self, $other ) = @_; | ||
| 93 | return 0 unless blessed($other); | ||
| 94 | return 0 unless $other->isa("App::CSE::Lucy::Search::QueryPrefix"); | ||
| 95 | return 0 unless $field{$$self} eq $field{$$other}; | ||
| 96 | return 0 unless $query_string{$$self} eq $query_string{$$other}; | ||
| 97 | return 1; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub to_string { | ||
| 101 | my $self = shift; | ||
| 102 | return "$field{$$self}:$query_string{$$self}"; | ||
| 103 | } | ||
| 104 | |||
| 105 | sub make_compiler { | ||
| 106 | my ( $self, %args ) = @_; | ||
| 107 | my $subordinate = delete $args{subordinate}; | ||
| 108 | my $compiler = App::CSE::Lucy::Search::PrefixCompiler->new( %args, parent => $self ); | ||
| 109 | $compiler->normalize unless $subordinate; | ||
| 110 | return $compiler; | ||
| 111 | } | ||
| 112 | |||
| 113 | 1; | ||
| 114 | |||
| 115 | package App::CSE::Lucy::Search::PrefixCompiler; | ||
| 116 | $App::CSE::Lucy::Search::PrefixCompiler::VERSION = '0.016'; | ||
| 117 | use base qw( Lucy::Search::Compiler ); | ||
| 118 | |||
| 119 | sub make_matcher { | ||
| 120 | my ( $self, %args ) = @_; | ||
| 121 | my $seg_reader = $args{reader}; | ||
| 122 | |||
| 123 | # Retrieve low-level components LexiconReader and PostingListReader. | ||
| 124 | my $lex_reader | ||
| 125 | = $seg_reader->obtain("Lucy::Index::LexiconReader"); | ||
| 126 | my $plist_reader | ||
| 127 | = $seg_reader->obtain("Lucy::Index::PostingListReader"); | ||
| 128 | |||
| 129 | |||
| 130 | # Acquire a Lexicon and seek it to our query string. | ||
| 131 | my $substring = $self->get_parent->get_query_string; | ||
| 132 | $substring =~ s/\*\s*$//; | ||
| 133 | |||
| 134 | ## Making that case insensitive only if the parent says so | ||
| 135 | unless( $self->get_parent->keep_case() ){ | ||
| 136 | $substring = lc( $substring ); | ||
| 137 | } | ||
| 138 | |||
| 139 | my $field = $self->get_parent->get_field; | ||
| 140 | my $lexicon = $lex_reader->lexicon( field => $field ); | ||
| 141 | return unless $lexicon; | ||
| 142 | |||
| 143 | |||
| 144 | $lexicon->seek($substring); | ||
| 145 | |||
| 146 | # Accumulate PostingLists for each matching term. | ||
| 147 | my @posting_lists; | ||
| 148 | while ( defined( my $term = $lexicon->get_term ) ) { | ||
| 149 | last unless $term =~ /^\Q$substring/; | ||
| 150 | |||
| 151 | $self->get_parent()->add_matching_term($term); | ||
| 152 | |||
| 153 | my $posting_list = $plist_reader->posting_list( | ||
| 154 | field => $field, | ||
| 155 | term => $term, | ||
| 156 | ); | ||
| 157 | if ($posting_list) { | ||
| 158 | push @posting_lists, $posting_list; | ||
| 159 | } | ||
| 160 | last unless $lexicon->next; | ||
| 161 | } | ||
| 162 | return unless @posting_lists; | ||
| 163 | |||
| 164 | return App::CSE::Lucy::Search::PrefixMatcher->new( posting_lists => \@posting_lists ); | ||
| 165 | } | ||
| 166 | |||
| 167 | package App::CSE::Lucy::Search::PrefixMatcher; | ||
| 168 | $App::CSE::Lucy::Search::PrefixMatcher::VERSION = '0.016'; | ||
| 169 | use base qw( Lucy::Search::Matcher ); | ||
| 170 | |||
| 171 | # Inside-out member vars. | ||
| 172 | my %doc_ids; | ||
| 173 | my %tick; | ||
| 174 | |||
| 175 | sub score{ | ||
| 176 | return 1.0 ; # Fixed score of 1.0 | ||
| 177 | } | ||
| 178 | |||
| 179 | sub new { | ||
| 180 | my ( $class, %args ) = @_; | ||
| 181 | my $posting_lists = delete $args{posting_lists}; | ||
| 182 | my $self = $class->SUPER::new(%args); | ||
| 183 | |||
| 184 | # Cheesy but simple way of interleaving PostingList doc sets. | ||
| 185 | my %all_doc_ids; | ||
| 186 | for my $posting_list (@$posting_lists) { | ||
| 187 | while ( my $doc_id = $posting_list->next ) { | ||
| 188 | $all_doc_ids{$doc_id} = undef; | ||
| 189 | } | ||
| 190 | } | ||
| 191 | my @doc_ids = sort { $a <=> $b } keys %all_doc_ids; | ||
| 192 | $doc_ids{$$self} = \@doc_ids; | ||
| 193 | |||
| 194 | $tick{$$self} = -1; | ||
| 195 | |||
| 196 | return $self; | ||
| 197 | } | ||
| 198 | |||
| 199 | sub DESTROY { | ||
| 200 | my $self = shift; | ||
| 201 | delete $doc_ids{$$self}; | ||
| 202 | delete $tick{$$self}; | ||
| 203 | $self->SUPER::DESTROY; | ||
| 204 | } | ||
| 205 | |||
| 206 | sub next { | ||
| 207 | my $self = shift; | ||
| 208 | my $doc_ids = $doc_ids{$$self}; | ||
| 209 | my $tick = ++$tick{$$self}; | ||
| 210 | return 0 if $tick >= scalar @$doc_ids; | ||
| 211 | return $doc_ids->[$tick]; | ||
| 212 | } | ||
| 213 | |||
| 214 | sub get_doc_id { | ||
| 215 | my $self = shift; | ||
| 216 | my $tick = $tick{$$self}; | ||
| 217 | my $doc_ids = $doc_ids{$$self}; | ||
| 218 | return $tick < scalar @$doc_ids ? $doc_ids->[$tick] : 0; | ||
| 219 | } | ||
| 220 | |||
| 221 | 1; |