Xapian/Searcher.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« Xapian/Indexer.pm ↑ Summary Xapian.pm »
1 package SWISH::Prog::Xapian::Searcher;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use base qw( SWISH::Prog::Searcher );
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Sort::SQL;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 0 ▶use SWISH::Prog::Xapian::Results;
Subroutine BEGIN: called CC=1 SCAR=2.6
8 use Search::Xapian::MultiValueSorter;
9 use SWISH::3 ':constants';
10 use Search::Query;
11  
12 __PACKAGE__->mk_ro_accessors(qw( prop_id_map ));
13  
14 our $VERSION = '0.09';
15  
16 =head1 NAME
17  
18 SWISH::Prog::Xapian::Searcher - Swish3 Xapian backend Searcher
19  
20 =head1 SYNOPSIS
21  
22 # see SWISH::Prog::Searcher
23
24 =cut
25  
26 =head1 DESCRIPTION
27  
28 SWISH::Prog::Xapian::Searcher is not made to replace the more fully-featured
29 Search::Xapian. Instead, SWISH::Prog::Xapian::Searcher
30 provides a simple API similar to other SWISH::Prog::Searcher-based backends
31 so that you can experiment with alternate
32 storage engines without needing to change much code.
33 When your search application requirements become more complex, the author
34 recommends the switch to using Search::Xapian directly.
35  
36 =head1 METHODS
37  
38 Only new and overridden methods are documented here. See
39 the L<SWISH::Prog::Searcher> documentation.
40  
41 =cut
42  
43 =head2 init( I<params> )
44  
45 Overrides superclass to build map of PropertyNames to ids, since
46 Xapian stores values by id not name.
47  
48 =cut
49  
50 sub init {
51 my $self = shift;
52 $self->SUPER::init(@_);
53 $self->{prop_id_map} = $self->_build_prop_id_map();
54 return $self;
55 }
56  
57 sub _build_prop_id_map {
58 my $self = shift;
59  
60 # load meta from the first invindex
61 my $invindex = $self->invindex->[0];
62 my $config = $invindex->meta;
63 my $props = $config->PropertyNames;
64  
65 # initialize with built-ins
66 my %map = ();
67 my %fields = %{ SWISH_DOC_FIELDS_MAP() };
68 my %props = %{ SWISH_DOC_PROP_MAP() };
69 for my $p ( keys %props ) {
70 my $field = $props{$p};
71 my $id = $fields{$field};
72 $map{$p} = $id;
73 }
74  
75 # add custom defined
76 for my $name ( keys %$props ) {
77 $map{$name} = $props->{$name}->{id};
78 }
79 return \%map;
80 }
81  
82 =head2 prop_id_map
83  
84 Get the read-only internal map for PropertyNames to id values.
85  
86 =head2 search( I<query> [, I<opts> ] )
87  
88 Returns a SWISH::Prog::Xapian::Results object.
89  
90 I<opts> is an optional hashref with the following supported
91 key/values:
92  
93 =over
94  
95 =item start
96  
97 The starting position. Default is 0.
98  
99 =item max
100  
101 The ending position. Default is max_hits().
102  
103 =item order
104  
105 The sort order. Default is by score.
106 B<This feature is not yet supported.>
107  
108 =item get_facets
109  
110 If set to an array ref of field names, then the Results object
111 will contain a hash ref of facet counts for those fields.
112  
113 =item facet_sample
114  
115 How many results to examine when counting facets. Default is all
116 of them.
117  
118 =back
119  
120 =cut
121  
122 sub search {
123 my $self = shift;
124 my $query = shift;
125 croak "query required" unless defined $query;
126 my $opts = shift || {};
127  
128 my $start = $opts->{start} || 0;
129 my $max = $opts->{max} || $self->max_hits;
130 my $order = $opts->{order} || 'score';
131 my $get_facets = $opts->{get_facets} || 0;
132 my $facet_sample = $opts->{facet_sample} || 0;
133  
134 #warn Data::Dump::dump $self;
135  
136 # we enquire on one db but can span multiple.
137 my $db1 = $self->{invindex}->[0]->{xdb};
138 for my $xdb ( map { $_->{xdb} } ( @{ $self->{invindex} } )[ 1 .. -1 ] ) {
139 $db1->add_database($xdb);
140 }
141 my $parsed_query = Search::Query->parser->parse($query);
142 my $enq = $db1->enquire($query);
143  
144 # sorting
145 if ($order) {
146 if ( ref $order ) {
147  
148 # assume it is a MultiValueSorter object
149 $enq->set_sort_by_key( $order, 1 );
150 }
151 else {
152  
153 my $sorter = Search::Xapian::MultiValueSorter->new();
154 my $pmap = $self->{prop_id_map};
155 my $sort_array = Sort::SQL->parse($order);
156 my @rules;
157 for my $pair (@$sort_array) {
158 my ( $field, $dir ) = @$pair;
159 next if $field eq 'score';
160 my $prop_id = $pmap->{$field};
161 if ( !defined $prop_id ) {
162 croak "Invalid PropertyName in sort: $field";
163 }
164 $sorter->add( $prop_id, ( uc($dir) eq 'ASC' ? 0 : 1 ) );
165 }
166 $enq->set_sort_by_key( $sorter, 1 );
167 }
168 }
169  
170 my $mset;
171 my %facets;
172 if ($get_facets) {
173 my $pmap = $self->{prop_id_map};
174 my $i = 0;
175 $mset = $enq->get_mset(
176 $start, $max,
177 sub {
178  
179 my ($doc) = @_;
180 return $doc if $facet_sample and $facet_sample > ++$i;
181  
182 for my $facet (@$get_facets) {
183 for my $value (
184 split( /\003/, $doc->get_value( $pmap->{$facet} ) ) )
185 {
186 $facets{$facet}->{$value}++;
187 }
188 }
189 return $doc;
190 }
191 );
192 }
193 else {
194 $mset = $enq->get_mset( $start, $max );
195 }
196 my $results = SWISH::Prog::Xapian::Results->new(
197 hits => $mset->size(),
198 mset => $mset,
199 query => $parsed_query,
200 prop_id_map => $self->{prop_id_map},
201 facets => \%facets,
202 );
203 $results->{_i} = 0;
204 return $results;
205 }
206  
207 1;
208  
209 __END__
« Xapian/Indexer.pm ↑ Summary Xapian.pm »