| 1 | package Wiki::Toolkit::Search::SII; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use Search::InvertedIndex; | ||
| 5 | use Carp "croak"; | ||
| 6 | use base 'Wiki::Toolkit::Search::Base'; | ||
| 7 | |||
| 8 | use vars qw( @ISA $VERSION ); | ||
| 9 | |||
| 10 | $VERSION = 0.09; | ||
| 11 | |||
| 12 | =head1 NAME | ||
| 13 | |||
| 14 | Wiki::Toolkit::Search::SII - Search::InvertedIndex plugin for Wiki::Toolkit. | ||
| 15 | |||
| 16 | =head1 SYNOPSIS | ||
| 17 | |||
| 18 | my $indexdb = Search::InvertedIndex::DB::Mysql->new( ... ); | ||
| 19 | my $search = Wiki::Toolkit::Search::SII->new( indexdb => $indexdb ); | ||
| 20 | my %wombat_nodes = $search->search_nodes("wombat"); | ||
| 21 | |||
| 22 | Provides search-related methods for L<Wiki::Toolkit>. | ||
| 23 | |||
| 24 | See also L<Wiki::Toolkit::Search::Base>, for methods not documented here. | ||
| 25 | |||
| 26 | =cut | ||
| 27 | |||
| 28 | =head1 METHODS | ||
| 29 | |||
| 30 | =over 4 | ||
| 31 | |||
| 32 | =item B<new> | ||
| 33 | |||
| 34 | # EITHER | ||
| 35 | |||
| 36 | my $indexdb = Search::InvertedIndex::DB::Mysql->new( | ||
| 37 | -db_name => $dbname, | ||
| 38 | -username => $dbuser, | ||
| 39 | -password => $dbpass, | ||
| 40 | -hostname => '', | ||
| 41 | -table_name => 'siindex', | ||
| 42 | -lock_mode => 'EX' ); | ||
| 43 | |||
| 44 | # OR | ||
| 45 | |||
| 46 | my $indexdb = Search::InvertedIndex::DB::DB_File_SplitHash->new( | ||
| 47 | -map_name => "/home/wiki/indexes.db", | ||
| 48 | -lock_mode => "EX" ); | ||
| 49 | |||
| 50 | # THEN | ||
| 51 | |||
| 52 | my $search = Wiki::Toolkit::Search::SII->new( indexdb => $indexdb ); | ||
| 53 | |||
| 54 | Takes only one parameter, which is mandatory. C<indexdb> must be a | ||
| 55 | C<Search::InvertedIndex::DB::*> object. | ||
| 56 | |||
| 57 | =cut | ||
| 58 | |||
| 59 | sub _init { | ||
| 60 | my ($self, %args) = @_; | ||
| 61 | my $indexdb = $args{indexdb}; | ||
| 62 | |||
| 63 | my $map = Search::InvertedIndex->new( -database => $indexdb ) | ||
| 64 | or croak "Couldn't set up Search::InvertedIndex map"; | ||
| 65 | $map->add_group( -group => "nodes" ); | ||
| 66 | $map->add_group( -group => "fuzzy_titles" ); | ||
| 67 | |||
| 68 | $self->{_map} = $map; | ||
| 69 | |||
| 70 | return $self; | ||
| 71 | } | ||
| 72 | |||
| 73 | sub _do_search { | ||
| 74 | my ($self, $and_or, $terms) = @_; | ||
| 75 | |||
| 76 | # Create a leaf for each search term. | ||
| 77 | my @leaves; | ||
| 78 | foreach my $term ( @$terms ) { | ||
| 79 | my $leaf = Search::InvertedIndex::Query::Leaf->new(-key => $term, | ||
| 80 | -group => "nodes" ); | ||
| 81 | push @leaves, $leaf; | ||
| 82 | } | ||
| 83 | |||
| 84 | # Collate the leaves. | ||
| 85 | my $query = Search::InvertedIndex::Query->new( -logic => $and_or, | ||
| 86 | -leafs => \@leaves ); | ||
| 87 | |||
| 88 | # Perform the search and extract the results. | ||
| 89 | my $result = $self->{_map}->search( -query => $query ); | ||
| 90 | |||
| 91 | my $num_results = $result->number_of_index_entries || 0; | ||
| 92 | my %results; | ||
| 93 | for my $i ( 1 .. $num_results ) { | ||
| 94 | my ($index, $data, $ranking) = $result->entry( -number => $i - 1 ); | ||
| 95 | $results{$index} = $ranking; | ||
| 96 | } | ||
| 97 | return %results; | ||
| 98 | } | ||
| 99 | |||
| 100 | sub _fuzzy_match { | ||
| 101 | my ($self, $string, $canonical) = @_; | ||
| 102 | my $leaf = Search::InvertedIndex::Query::Leaf->new( | ||
| 103 | -key => $canonical, | ||
| 104 | -group => "fuzzy_titles" ); | ||
| 105 | |||
| 106 | my $query = Search::InvertedIndex::Query->new( -leafs => [ $leaf ] ); | ||
| 107 | |||
| 108 | my $result = $self->{_map}->search( -query => $query ); | ||
| 109 | |||
| 110 | my $num_results = $result->number_of_index_entries || 0; | ||
| 111 | my %results; | ||
| 112 | for my $i ( 1 .. $num_results ) { | ||
| 113 | my ($index, $data) = $result->entry( -number => $i - 1 ); | ||
| 114 | $results{$data} = $data eq $string ? 2 : 1; | ||
| 115 | } | ||
| 116 | return %results; | ||
| 117 | } | ||
| 118 | |||
| 119 | sub _index_node { | ||
| 120 | my ($self, $node, $content, $keys) = @_; | ||
| 121 | my $update = Search::InvertedIndex::Update->new( | ||
| 122 | -group => "nodes", | ||
| 123 | -index => $node, | ||
| 124 | -data => $content, | ||
| 125 | -keys => { map { $_ => 1 } @$keys } | ||
| 126 | ); | ||
| 127 | $self->{_map}->update( -update => $update ); | ||
| 128 | } | ||
| 129 | |||
| 130 | sub _index_fuzzy { | ||
| 131 | my ($self, $node, $canonical) = @_; | ||
| 132 | my $update = Search::InvertedIndex::Update->new( | ||
| 133 | -group => "fuzzy_titles", | ||
| 134 | -index => $node . "_fuzzy_title", | ||
| 135 | -data => $node, | ||
| 136 | -keys => { $canonical => 1 } | ||
| 137 | ); | ||
| 138 | $self->{_map}->update( -update => $update ); | ||
| 139 | } | ||
| 140 | |||
| 141 | sub _delete_node { | ||
| 142 | my ($self, $node) = @_; | ||
| 143 | $self->{_map}->remove_index_from_all({ -index => $node }); | ||
| 144 | } | ||
| 145 | |||
| 146 | sub supports_phrase_searches { return 0; } | ||
| 147 | sub supports_fuzzy_searches { return 1; } | ||
| 148 | |||
| 149 | =back | ||
| 150 | |||
| 151 | =head1 SEE ALSO | ||
| 152 | |||
| 153 | L<Wiki::Toolkit>, L<Wiki::Toolkit::Search::Base>. | ||
| 154 | |||
| 155 | =cut | ||
| 156 | |||
| 157 | 1; |