1#!/usr/bin/perl 2 3use lib 'buildlib'; 4use Test::More tests => 5; 5 6BEGIN { use_ok('KinoSearch1::Search::PhraseQuery') } 7 8use KinoSearch1::Test::TestUtils qw( create_index ); 9use KinoSearch1::Index::Term; 10use KinoSearch1::Searcher; 11 12my $best_match = 'x a b c d a b c d'; 13 14my @docs = ( 15 1 .. 20, 16 'a b c a b c a b c d', 17 'a b c d x x a', 18 'a c b d', 'a x x x b x x x c x x x x x x d x', 19 $best_match, 'a' .. 'z', 20); 21 22my $invindex = create_index(@docs); 23my $searcher = KinoSearch1::Searcher->new( invindex => $invindex ); 24 25my $phrase_query = KinoSearch1::Search::PhraseQuery->new( slop => 0 ); 26for (qw( a b c d )) { 27 my $term = KinoSearch1::Index::Term->new( 'content', $_ ); 28 $phrase_query->add_term($term); 29} 30 31my $hits = $searcher->search( query => $phrase_query ); 32$hits->seek( 0, 50 ); 33is( $hits->total_hits, 3, "correct number of hits" ); 34my $first_hit = $hits->fetch_hit_hashref; 35is( $first_hit->{content}, $best_match, 'best match appears first' ); 36 37my $second_hit = $hits->fetch_hit_hashref; 38ok( $first_hit->{score} > $second_hit->{score}, 39 "best match scores higher: $first_hit->{score} > $second_hit->{score}" ); 40 41$phrase_query = KinoSearch1::Search::PhraseQuery->new( slop => 0 ); 42for (qw( c a )) { 43 my $term = KinoSearch1::Index::Term->new( 'content', $_ ); 44 $phrase_query->add_term($term); 45} 46$hits = $searcher->search( query => $phrase_query ); 47is( $hits->total_hits, 1, 'avoid underflow when subtracting offset' ); 48 49