| 1 | use strict; |
|---|
| 2 | use CGI::Wiki::Setup::SQLite; |
|---|
| 3 | use OpenGuides::Config; |
|---|
| 4 | use OpenGuides::SuperSearch; |
|---|
| 5 | use Test::More; |
|---|
| 6 | |
|---|
| 7 | eval { require DBD::SQLite; }; |
|---|
| 8 | if ( $@ ) { |
|---|
| 9 | plan skip_all => "DBD::SQLite not installed"; |
|---|
| 10 | exit 0; |
|---|
| 11 | } |
|---|
| 12 | |
|---|
| 13 | eval { require CGI::Wiki::Search::Plucene; }; |
|---|
| 14 | if ( $@ ) { |
|---|
| 15 | plan skip_all => "Plucene not installed"; |
|---|
| 16 | exit 0; |
|---|
| 17 | } |
|---|
| 18 | |
|---|
| 19 | plan tests => 9; |
|---|
| 20 | |
|---|
| 21 | # Clear out the database from any previous runs. |
|---|
| 22 | unlink "t/node.db"; |
|---|
| 23 | unlink <t/indexes/*>; |
|---|
| 24 | |
|---|
| 25 | CGI::Wiki::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 26 | my $config = OpenGuides::Config->new( |
|---|
| 27 | vars => { |
|---|
| 28 | dbtype => "sqlite", |
|---|
| 29 | dbname => "t/node.db", |
|---|
| 30 | indexing_directory => "t/indexes", |
|---|
| 31 | script_name => "wiki.cgi", |
|---|
| 32 | script_url => "http://example.com/", |
|---|
| 33 | site_name => "Test Site", |
|---|
| 34 | template_path => "./templates", |
|---|
| 35 | use_plucene => 1 |
|---|
| 36 | } |
|---|
| 37 | ); |
|---|
| 38 | |
|---|
| 39 | my $search = OpenGuides::SuperSearch->new( config => $config ); |
|---|
| 40 | isa_ok( $search, "OpenGuides::SuperSearch" ); |
|---|
| 41 | |
|---|
| 42 | # Write some data. |
|---|
| 43 | my $wiki = $search->{wiki}; |
|---|
| 44 | $wiki->write_node( "Parks", "A page about parks." ) |
|---|
| 45 | or die "Can't write node"; |
|---|
| 46 | $wiki->write_node( "Wandsworth Common", "A common.", undef, |
|---|
| 47 | { category => "Parks" } ) |
|---|
| 48 | or die "Can't write node"; |
|---|
| 49 | $wiki->write_node( "Kake", "I like walking in parks." ) |
|---|
| 50 | or die "Can't write node"; |
|---|
| 51 | |
|---|
| 52 | my %tt_vars = $search->run( |
|---|
| 53 | return_tt_vars => 1, |
|---|
| 54 | vars => { search => "parks" }, |
|---|
| 55 | ); |
|---|
| 56 | foreach my $result ( @{ $tt_vars{results} || [] } ) { |
|---|
| 57 | print "# $result->{name} scores $result->{score}\n"; |
|---|
| 58 | } |
|---|
| 59 | my %scores = map { $_->{name} => $_->{score} } @{$tt_vars{results} || []}; |
|---|
| 60 | ok( $scores{Kake} < $scores{'Wandsworth Common'}, |
|---|
| 61 | "content match scores less than category match" ); |
|---|
| 62 | ok( $scores{'Wandsworth Common'} < $scores{Parks}, |
|---|
| 63 | "title match scores more than category match" ); |
|---|
| 64 | |
|---|
| 65 | # Now test locales. |
|---|
| 66 | $wiki->write_node( "Hammersmith", "A page about Hammersmith." ) |
|---|
| 67 | or die "Can't write node"; |
|---|
| 68 | $wiki->write_node( "The Gate", "A restaurant.", undef, |
|---|
| 69 | { locale => "Hammersmith" } ) |
|---|
| 70 | or die "Can't write node"; |
|---|
| 71 | $wiki->write_node( "Kake Pugh", "I live in Hammersmith." ) |
|---|
| 72 | or die "Can't write node"; |
|---|
| 73 | |
|---|
| 74 | %tt_vars = $search->run( |
|---|
| 75 | return_tt_vars => 1, |
|---|
| 76 | vars => { search => "hammersmith" }, |
|---|
| 77 | ); |
|---|
| 78 | foreach my $result ( @{ $tt_vars{results} || [] } ) { |
|---|
| 79 | print "# $result->{name} scores $result->{score}\n"; |
|---|
| 80 | } |
|---|
| 81 | %scores = map { $_->{name} => $_->{score} } @{$tt_vars{results} || []}; |
|---|
| 82 | ok( $scores{'Kake Pugh'} < $scores{'The Gate'}, |
|---|
| 83 | "content match scores less than locale match" ); |
|---|
| 84 | ok( $scores{'The Gate'} < $scores{Hammersmith}, |
|---|
| 85 | "locale match scores less than title match" ); |
|---|
| 86 | |
|---|
| 87 | # Check that two words in the title beats one in the title and |
|---|
| 88 | # one in the content. |
|---|
| 89 | $wiki->write_node( "Putney Tandoori", "Indian food" ) |
|---|
| 90 | or die "Couldn't write node"; |
|---|
| 91 | $wiki->write_node( "Putney", "There is a tandoori restaurant here" ) |
|---|
| 92 | or die "Couldn't write node"; |
|---|
| 93 | |
|---|
| 94 | %tt_vars = $search->run( |
|---|
| 95 | return_tt_vars => 1, |
|---|
| 96 | vars => { search => "putney tandoori" }, |
|---|
| 97 | ); |
|---|
| 98 | foreach my $result ( @{ $tt_vars{results} || [] } ) { |
|---|
| 99 | print "# $result->{name} scores $result->{score}\n"; |
|---|
| 100 | } |
|---|
| 101 | %scores = map { $_->{name} => $_->{score} } @{$tt_vars{results} || []}; |
|---|
| 102 | ok( $scores{Putney} < $scores{'Putney Tandoori'}, |
|---|
| 103 | "two words in title beats one in title and one in content" ); |
|---|
| 104 | |
|---|
| 105 | SKIP: { |
|---|
| 106 | skip "Word proximity not yet taken into account", 1; |
|---|
| 107 | # Check that in an AND match words closer together get higher priority. |
|---|
| 108 | $wiki->write_node( "Spitalfields Market", |
|---|
| 109 | "Mango juice from the Indian stall" ) |
|---|
| 110 | or die "Can't write node"; |
|---|
| 111 | $wiki->write_node( "Borough Market", "dried mango and real apple juice" ) |
|---|
| 112 | or die "Can't write node"; |
|---|
| 113 | |
|---|
| 114 | %tt_vars = $search->run( |
|---|
| 115 | return_tt_vars => 1, |
|---|
| 116 | vars => { search => "mango juice" }, |
|---|
| 117 | ); |
|---|
| 118 | foreach my $result ( @{ $tt_vars{results} || [] } ) { |
|---|
| 119 | print "# $result->{name} scores $result->{score}\n"; |
|---|
| 120 | } |
|---|
| 121 | %scores = map { $_->{name} => $_->{score} } @{$tt_vars{results} || []}; |
|---|
| 122 | ok( $scores{'Borough Market'} < $scores{'Spitalfields Market'}, |
|---|
| 123 | "words closer together gives higher score" ); |
|---|
| 124 | } # end of SKIP |
|---|
| 125 | |
|---|
| 126 | # Check that the number of occurrences of the search term is significant. |
|---|
| 127 | |
|---|
| 128 | $wiki->write_node( "Pub Crawls", "The basic premise of the pub crawl is to visit a succession of pubs, rather than spending the entire evening or day in a single establishment. London offers an excellent choice of themes for your pub crawl.", undef, { category => "Pubs" } ) or die "Can't write node"; |
|---|
| 129 | $wiki->write_node( "The Pub", "A pub.", undef, { category => "Pubs" } ) or die "Can't write node"; |
|---|
| 130 | |
|---|
| 131 | %tt_vars = $search->run( |
|---|
| 132 | return_tt_vars => 1, |
|---|
| 133 | vars => { search => "pub" } |
|---|
| 134 | ); |
|---|
| 135 | foreach my $result ( @{ $tt_vars{results} || [] } ) { |
|---|
| 136 | print "# $result->{name} scores $result->{score}\n"; |
|---|
| 137 | } |
|---|
| 138 | is( $tt_vars{results}[0]{name}, "Pub Crawls", |
|---|
| 139 | "node with two mentions of search term comes top" ); |
|---|
| 140 | ok( $tt_vars{results}[0]{score} > $tt_vars{results}[1]{score}, |
|---|
| 141 | "...with a score strictly greater than node with one mention" ); |
|---|