| 1 | package SWISH::Prog::Lucy::Results; | ||
| 2 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | our $VERSION = '0.25'; | ||
| 6 | |||
| 7 | 1 | ▶ | use base qw( SWISH::Prog::Results ); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 0 | ▶ | use SWISH::Prog::Lucy::Result; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 9 | |||
| 10 | __PACKAGE__->mk_accessors(qw( find_relevant_fields )); | ||
| 11 | __PACKAGE__->mk_ro_accessors(qw( id lucy_hits property_map )); | ||
| 12 | |||
| 13 | =head1 NAME | ||
| 14 | |||
| 15 | SWISH::Prog::Lucy::Results - search results for Swish3 Lucy backend | ||
| 16 | |||
| 17 | =head1 SYNOPSIS | ||
| 18 | |||
| 19 | my $results = $searcher->search($query); | ||
| 20 | $results->find_relevant_fields(1); | ||
| 21 | while ( my $result = $results->next ) { | ||
| 22 | my $fields = $result->relevant_fields; | ||
| 23 | for my $f (@$fields) { | ||
| 24 | printf("%s matched %s\n", $result->uri, $f); | ||
| 25 | } | ||
| 26 | } | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | SWISH::Prog::Lucy::Results is an Apache Lucy based Results | ||
| 31 | class for Swish3. | ||
| 32 | |||
| 33 | =head1 METHODS | ||
| 34 | |||
| 35 | Only new and overridden methods are documented here. See | ||
| 36 | the L<SWISH::Prog::Results> documentation. | ||
| 37 | |||
| 38 | =head2 find_relevant_fields I<1|0> | ||
| 39 | |||
| 40 | Set to true (1) to locate the fields the query matched | ||
| 41 | for each result. Default is false (0). | ||
| 42 | |||
| 43 | NOTE that the Indexer must have had highlightable_fields set | ||
| 44 | to true (1) in order for find_relevant_fields to work. | ||
| 45 | |||
| 46 | =head2 next | ||
| 47 | |||
| 48 | Returns the next SWISH::Prog::Lucy::Result object from the result set. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub next { | ||
| 53 | my $hit = $_[0]->lucy_hits->next or return; | ||
| 54 | |||
| 55 | # see http://markmail.org/message/xoqwxofwphlowqxf | ||
| 56 | my @relevant_fields; | ||
| 57 | if ( $_[0]->find_relevant_fields ) { | ||
| 58 | my $searcher = $_[0]->{_searcher}; | ||
| 59 | my $compiler = $_[0]->{_compiler}; | ||
| 60 | my $doc_vec = $searcher->fetch_doc_vec( $hit->get_doc_id ); | ||
| 61 | my $schema = $searcher->get_schema(); | ||
| 62 | for my $field ( @{ $schema->all_fields } ) { | ||
| 63 | my $spans = $compiler->highlight_spans( | ||
| 64 | searcher => $searcher, | ||
| 65 | doc_vec => $doc_vec, | ||
| 66 | field => $field, | ||
| 67 | ); | ||
| 68 | if (@$spans) { | ||
| 69 | push @relevant_fields, $field; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | } | ||
| 73 | return SWISH::Prog::Lucy::Result->new( | ||
| 74 | relevant_fields => \@relevant_fields, | ||
| 75 | doc => $hit, | ||
| 76 | property_map => $_[0]->{property_map}, | ||
| 77 | |||
| 78 | # scale like xapian, swish-e | ||
| 79 | score => ( int( $hit->get_score * 1000 ) || 1 ), | ||
| 80 | id => $_[0]->id, | ||
| 81 | ); | ||
| 82 | } | ||
| 83 | |||
| 84 | =head2 lucy_hits | ||
| 85 | |||
| 86 | Get the internal Lucy::Search::Hits object. | ||
| 87 | |||
| 88 | =head2 property_map | ||
| 89 | |||
| 90 | Get the read-only hashref of PropertyNameAlias to PropertyName | ||
| 91 | values. | ||
| 92 | |||
| 93 | =head2 id | ||
| 94 | |||
| 95 | Get the read-only unique id from the parent Searcher. | ||
| 96 | |||
| 97 | =cut | ||
| 98 | |||
| 99 | 1; | ||
| 100 | |||
| 101 | __END__ | ||