/usr/lib/x86_64-linux-gnu/perl5/5.24/LucyX/Search/Filter.pm is in liblucy-perl 0.3.3-7+b1.
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 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 | # Licensed to the Apache Software Foundation (ASF) under one or more
# contributor license agreements. See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You under the Apache License, Version 2.0
# (the "License"); you may not use this file except in compliance with
# the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
use strict;
use warnings;
package LucyX::Search::Filter;
BEGIN { our @ISA = qw( Lucy::Search::Query ) }
our $VERSION = '0.003003';
$VERSION = eval $VERSION;
use Carp;
use Storable qw( nfreeze thaw );
use Scalar::Util qw( blessed weaken );
use bytes;
no bytes;
# Inside-out member vars.
our %query;
our %cached_bits;
sub new {
my ( $either, %args ) = @_;
my $query = delete $args{query};
confess("required parameter query is not a Lucy::Search::Query")
unless ( blessed($query)
&& $query->isa('Lucy::Search::Query') );
my $self = $either->SUPER::new(%args);
$self->_init_cache;
$query{$$self} = $query;
$self->set_boost(0);
return $self;
}
sub DESTROY {
my $self = shift;
delete $query{$$self};
delete $cached_bits{$$self};
$self->SUPER::DESTROY;
}
sub make_compiler {
my ( $self, %args ) = @_;
my $subordinate = delete $args{subordinate};
my $compiler
= LucyX::Search::FilterCompiler->new( %args, parent => $self );
$compiler->normalize unless $subordinate;
return $compiler;
}
sub serialize {
my ( $self, $outstream ) = @_;
$self->SUPER::serialize($outstream);
my $frozen = nfreeze( $query{$$self} );
$outstream->write_c32( bytes::length($frozen) );
$outstream->print($frozen);
}
sub deserialize {
my ( $self, $instream ) = @_;
$self->SUPER::deserialize($instream);
my $len = $instream->read_c32;
my $frozen;
$instream->read( $frozen, $len );
$query{$$self} = thaw($frozen);
return $self;
}
sub equals {
my ( $self, $other ) = @_;
return 0 unless $other->isa(__PACKAGE__);
return 0 unless $query{$$self}->equals( $query{$$other} );
return 0 unless $self->get_boost == $other->get_boost;
return 1;
}
sub to_string {
my $self = shift;
return 'Filter(' . $query{$$self}->to_string . ')';
}
sub _bits {
my ( $self, $seg_reader ) = @_;
my $cached_bits = $self->_fetch_cached_bits($seg_reader);
# Fill the cache.
if ( !defined $cached_bits ) {
$cached_bits = Lucy::Object::BitVector->new(
capacity => $seg_reader->doc_max + 1 );
$self->_store_cached_bits( $seg_reader, $cached_bits );
my $collector = Lucy::Search::Collector::BitCollector->new(
bit_vector => $cached_bits );
my $polyreader = Lucy::Index::PolyReader->new(
schema => $seg_reader->get_schema,
folder => $seg_reader->get_folder,
snapshot => $seg_reader->get_snapshot,
sub_readers => [$seg_reader],
);
my $searcher
= Lucy::Search::IndexSearcher->new( index => $polyreader );
# Perform the search.
$searcher->collect(
query => $query{$$self},
collector => $collector,
);
}
return $cached_bits;
}
# Store a cached BitVector associated with a particular SegReader. Store a
# weak reference to the SegReader as an indicator of cache validity.
sub _store_cached_bits {
my ( $self, $seg_reader, $bits ) = @_;
my $pair = { seg_reader => $seg_reader, bits => $bits };
weaken( $pair->{seg_reader} );
$cached_bits{$$self}{ $seg_reader->hash_sum } = $pair;
}
# Retrieve a cached BitVector associated with a particular SegReader. As a
# side effect, clear away any BitVectors which are no longer valid because
# their SegReaders have gone away.
sub _fetch_cached_bits {
my ( $self, $seg_reader ) = @_;
my $cached_bits = $cached_bits{$$self};
# Sweep.
while ( my ( $hash_sum, $pair ) = each %$cached_bits ) {
# If weak ref has decomposed into undef, SegReader is gone... so
# delete.
next if defined $pair->{seg_reader};
delete $cached_bits->{$hash_sum};
}
# Fetch.
my $pair = $cached_bits->{ $seg_reader->hash_sum };
return $pair->{bits} if defined $pair;
return;
}
# Kill any existing cached filters.
sub _init_cache {
my $self = shift;
$cached_bits{$$self} = {};
}
# Testing only.
sub _cached_count {
my $self = shift;
return scalar grep { defined $cached_bits{$$self}{$_}{seg_reader} }
keys %{ $cached_bits{$$self} };
}
package LucyX::Search::FilterCompiler;
our $VERSION = '0.003003';
$VERSION = eval $VERSION;
BEGIN { our @ISA = qw( Lucy::Search::Compiler ) }
sub new {
my ( $class, %args ) = @_;
$args{similarity} ||= $args{searcher}->get_schema->get_similarity;
return $class->SUPER::new(%args);
}
sub make_matcher {
my ( $self, %args ) = @_;
my $seg_reader = $args{reader};
my $bits = $self->get_parent->_bits($seg_reader);
return LucyX::Search::FilterMatcher->new(
bits => $bits,
doc_max => $seg_reader->doc_max,
);
}
package LucyX::Search::FilterMatcher;
our $VERSION = '0.003003';
$VERSION = eval $VERSION;
BEGIN { our @ISA = qw( Lucy::Search::Matcher ) }
1;
__END__
__BINDING__
Clownfish::CFC::Binding::Perl::Class->register(
parcel => "Lucy",
class_name => "LucyX::Search::FilterMatcher",
bind_constructors => ["new"],
);
__POD__
=head1 NAME
LucyX::Search::Filter - Build a caching filter based on results of a Query.
=head1 SYNOPSIS
my %category_filters;
for my $category (qw( sweet sour salty bitter )) {
my $cat_query = Lucy::Search::TermQuery->new(
field => 'category',
term => $category,
);
$category_filters{$category} = LucyX::Search::Filter->new(
query => $cat_query,
);
}
while ( my $cgi = CGI::Fast->new ) {
my $user_query = $cgi->param('q');
my $filter = $category_filters{ $cgi->param('category') };
my $and_query = Lucy::Search::ANDQuery->new;
$and_query->add_child($user_query);
$and_query->add_child($filter);
my $hits = $searcher->hits( query => $and_query );
...
=head1 DESCRIPTION
A Filter is a L<Lucy::Search::Query> subclass that can be used to filter
the results of another Query. The effect is very similar to simply using the
wrapped inner query, but there are two important differences:
=over
=item
A Filter does not contribute to the score of the documents it matches.
=item
A Filter caches its results, so it is more efficient if you use it more than
once.
=back
To obtain logically equivalent results to the Filter but avoid the caching,
substitute the wrapped query but use set_boost() to set its C<boost> to 0.
=head1 METHODS
=head2 new
my $filter = LucyX::Search::Filter->new(
query => $query;
);
Constructor. Takes one hash-style parameter, C<query>, which must be an
object belonging to a subclass of L<Lucy::Search::Query>.
=head1 BUGS
Filters do not cache when used in a search cluster with LucyX::Remote's
SearchServer and SearchClient.
=cut
|